-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-scan-pdf.py
executable file
·411 lines (347 loc) · 15.1 KB
/
format-scan-pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python3
#
# Copyright (C) 2023-2024 Joelle Maslak
# All Rights Reserved - See License
#
# USAGE: format-scan-pdf.py <source.pdf> <destination.pdf>
#
# DEPENDENCIES (all must be in your PATH):
#
# deskew -> Available via https://galfar.vevb.net/wp/projects/deskew/
# exiftool -> Available on Ubuntu in the libimage-exiftool-perl package
# gm --> Available on Ubuntu in the graphicsmagick package
# mutool --> Available on Ubuntu in the mupdf-tools package
# ocrmypdf --> Available on Ubuntu in the ocrmypdf package
# parallel --> Available on Ubuntu in the parallel package
# pdftk --> Available on Ubuntu in the pdftk package
# pdftoppm --> Available on Ubuntu in the poppler-utils package
# prompt_toolkit --> Available on Ubuntu in the python3-prompt-toolkit package
# qpdf - Available on Ubuntu in the qpdf package
#
import argparse
import os
import os.path
import re
import shutil
import subprocess
import sys
import tempfile
from prompt_toolkit.shortcuts import radiolist_dialog, yes_no_dialog
def parse_arguments():
"""Get arguments from command line."""
parser = argparse.ArgumentParser(description="Make scanned PDFs more usable")
parser.add_argument('--runfile', help="Run (config) filename")
parser.add_argument('infile', help="Input filename")
parser.add_argument('outfile', help="Output filename")
args = parser.parse_args()
runfile = {}
if args.runfile is not None:
with open(args.runfile, "r") as f:
for line in f:
line = line.strip()
eles = line.lower().split(" ", 1)
if len(eles) == 2:
runfile[eles[0]] = eles[1]
return args, runfile
def rotate(fn_in, fn_out, runfile):
"""Prompt user for rotation info and rotate document."""
if "rotate" in runfile:
if runfile["rotate"] not in ("none", "clockwise", "anticlockwise", "180"):
choice = "none"
else:
choice = runfile["rotate"]
else:
choice = radiolist_dialog(
title="File Rotation",
text="Indicate how the document should be rotated",
values=[
("None", "None"),
("1-endeast", "Clockwise"),
("1-endwest", "Anti-Clockwise"),
("1-endsouth", "180 Degrees"),
],
).run()
if choice is None:
print("Exiting without changes.")
sys.exit()
elif choice.lower() == "none":
shutil.copy(fn_in, fn_out)
else:
subprocess.check_call(["pdftk", fn_in, "cat", choice, "output", fn_out])
def split_pages(fn_in, fn_out, tmpdir, runfile):
"""Split pages in scan."""
if "split" in runfile:
if runfile["split"] not in ("no", "all", "skipfirst", "skiplast", "skipfirstlast"):
choice = "no"
else:
choice = runfile["split"]
else:
choice = radiolist_dialog(
title="Page Split",
text="Do you want to split each input page into two output pages?",
values=[
("no", "No"),
("all", "Split all pages"),
("skipfirst", "Split all but FIRST page"),
("skiplast", "Split all but LAST page"),
("skipfirstlast", "Split all but FIRST and LAST page"),
],
).run()
if not choice:
print("Exiting without changes.")
sys.exit()
elif choice == "no":
shutil.copy(fn_in, fn_out)
elif choice == "all":
subprocess.check_call(["mutool", "poster", "-x", "2", fn_in, fn_out])
elif choice == "skipfirst":
fn_first = os.path.join(tmpdir, "work-first.pdf")
fn_middle = os.path.join(tmpdir, "work-middle.pdf")
fn_split = os.path.join(tmpdir, "work-split.pdf")
subprocess.check_call(["pdftk", fn_in, "cat", "1", "output", fn_first])
subprocess.check_call(["pdftk", fn_in, "cat", "2-end", "output", fn_middle])
subprocess.check_call(["mutool", "poster", "-x", "2", fn_middle, fn_split])
subprocess.check_call(["pdftk", fn_first, fn_split, "cat", "output", fn_out])
elif choice == "skiplast":
fn_middle = os.path.join(tmpdir, "work-middle.pdf")
fn_last = os.path.join(tmpdir, "work-last.pdf")
fn_split = os.path.join(tmpdir, "work-split.pdf")
subprocess.check_call(["pdftk", fn_in, "cat", "1-r2", "output", fn_middle])
subprocess.check_call(["pdftk", fn_in, "cat", "r1", "output", fn_last])
subprocess.check_call(["mutool", "poster", "-x", "2", fn_middle, fn_split])
subprocess.check_call(["pdftk", fn_split, fn_last, "cat", "output", fn_out])
elif choice == "skipfirstlast":
fn_first = os.path.join(tmpdir, "work-first.pdf")
fn_middle = os.path.join(tmpdir, "work-middle.pdf")
fn_last = os.path.join(tmpdir, "work-last.pdf")
fn_split = os.path.join(tmpdir, "work-split.pdf")
subprocess.check_call(["pdftk", fn_in, "cat", "1", "output", fn_first])
subprocess.check_call(["pdftk", fn_in, "cat", "2-r2", "output", fn_middle])
subprocess.check_call(["pdftk", fn_in, "cat", "r1", "output", fn_last])
subprocess.check_call(["mutool", "poster", "-x", "2", fn_middle, fn_split])
subprocess.check_call(["pdftk", fn_first, fn_split, fn_last, "cat", "output", fn_out])
def remove_pages(fn_in, fn_out, runfile):
"""Remove pages in scan."""
if "remove_pages" in runfile:
if runfile["remove_pages"] not in ("none", "first", "last", "firstlast"):
choice = "None"
elif runfile["remove_pages"] == "none":
choice = "None"
elif runfile["remove_pages"] == "first":
choice = "2-end"
elif runfile["remove_pages"] == "last":
choice = "1-r2"
elif runfile["remove_pages"] == "firstlast":
choice = "2-r2"
else:
choice = radiolist_dialog(
title="Remove Pages",
text="Indicate which pages should be removed from the output",
values=[
("None", "None"),
("2-end", "First Page"),
("1-r2", "Last Page"),
("2-r2", "First and Last Page"),
],
).run()
if not choice:
print("Exiting without changes.")
sys.exit()
elif choice == "None":
shutil.copy(fn_in, fn_out)
else:
subprocess.check_call(["pdftk", fn_in, "cat", choice, "output", fn_out])
def crop(fn_in, fn_out, tmpdir, runfile):
"""Prompt user to remove some right margin."""
if "crop" in runfile:
match = re.match(r"^(\d+)\s*(left|right|center)$", runfile["crop"])
if match is None:
choice = ["100", "Center"]
elif match.group(2) == "left":
choice = [match.group(1), "Left"]
elif match.group(2) == "right":
choice = [match.group(1), "Right"]
elif match.group(2) == "center":
choice = [match.group(1), "Center"]
else:
choice = radiolist_dialog(
title="Remove Right Margin",
text="Do you want to remove some margin from the document?\n" +
"(note this causes loss of everything but the image of te PDF)",
values=[
(["100", "Center"], "No"),
(["90", "East"], "Remove left 10%"),
(["80", "East"], "Remove left 20%"),
(["90", "West"], "Remove right 10%"),
(["80", "West"], "Remove right 20%"),
(["80", "Center"], "Remove both left and right 10%"),
(["60", "Center"], "Remove both left and right 20%"),
],
).run()
if choice is None:
print("Exiting without changes.")
sys.exit()
keep = choice[0]
gravity = choice[1]
if keep == "100":
shutil.copy(fn_in, fn_out)
return
base = os.path.join(tmpdir, "images")
subprocess.check_call([f"pdftoppm -r 300 -cropbox -jpeg {fn_in} {base}"], shell=True)
subprocess.check_call(["parallel gm convert {} -gravity " + gravity +
" -crop " + keep + "%x100% {}.new.jpg ::: " +
f"{base}-*[0-9].jpg"], shell=True)
subprocess.check_call([f"gm convert {base}-*.new.jpg {fn_out}"], shell=True)
def deskew(fn_in, fn_out, tmpdir, runfile):
"""Prompt user to determine if they want deskewing and, if so, deskew it."""
if "deskew" in runfile:
if runfile["deskew"] == "no":
choice = "no"
elif runfile["deskew"] == "standard":
choice = "standard"
elif runfile["deskew"] == "standardskipfirst":
choice = "standard-skip-first"
elif runfile["deskew"] == "100":
choice = "100"
elif runfile["deskew"] == "100skipfirst":
choice = "100-skip-first"
elif runfile["deskew"] == "200":
choice = "200"
elif runfile["deskew"] == "200skipfirst":
choice = "200-skip-first"
else:
choice = radiolist_dialog(
title="Deskew",
text="Do you want to deskew the document?\n" +
"(note this causes loss of everything but the image of te PDF)",
values=[
("no", "No"),
("standard", "Standard Deskew"),
("standard-skip-first", "Standard Deskew (don't deskew first page)"),
("100", "100 Pixel Margin Deskew"),
("100-skip-first", "100 Pixel Margin Deskew (don't deskew first page)"),
("200", "200 Pixel Margin Deskew"),
("200-skip-first", "200 Pixel Margin Deskew (don't deskew first page)"),
],
).run()
if choice is None:
print("Exiting without changes.")
sys.exit()
elif choice == "no":
shutil.copy(fn_in, fn_out)
else:
base = os.path.join(tmpdir, "images")
if "-skip-first" in choice:
fn_first = os.path.join(tmpdir, "work-first.pdf")
fn_middle = os.path.join(tmpdir, "work-middle.pdf")
fn_deskew = os.path.join(tmpdir, "work-deskew.pdf")
# Deal with converting first page to/from image (so future
# OCR doesn't balk)
subprocess.check_call(["pdftk", fn_in, "cat", "1", "output", fn_first])
firstbase = os.path.join(tmpdir, "first-images")
subprocess.check_call([f"pdftoppm -cropbox -jpeg {fn_first} {firstbase}"], shell=True)
subprocess.check_call([f"gm convert {firstbase}-*.jpg {fn_first}"], shell=True)
# And we need the rest.
subprocess.check_call(["pdftk", fn_in, "cat", "2-end", "output", fn_middle])
choice = choice.replace("-skip-first", "")
else:
fn_first = None
fn_middle = fn_in
fn_deskew = fn_out
subprocess.check_call([f"pdftoppm -r 300 -cropbox -jpeg {fn_middle} {base}"], shell=True)
if choice == "standard":
subprocess.check_call(["parallel deskew -b ffffff -a 20 -m 100 -o {}.new.jpg {} ::: " +
f"{base}-*[0-9].jpg"], shell=True)
else:
margins = f"{choice},{choice},{choice},{choice}"
subprocess.check_call(["parallel deskew -b ffffff -a 20 -m 100 -r " + margins +
" -o {}.new.jpg {} ::: " + f"{base}-*[0-9].jpg"], shell=True)
subprocess.check_call([f"gm convert {base}-*.new.jpg {fn_deskew}"], shell=True)
if fn_first is not None:
subprocess.check_call(["pdftk", fn_first, fn_deskew, "cat", "output", fn_out])
def remove_hidden(fn_in, fn_out, tmpdir, runfile):
"""Prompt user to remove hidden layers, metadata, etc."""
if "remove_metadata" in runfile:
choice = runfile["remove_metadata"]
else:
choice = radiolist_dialog(
title="Remove Metadata",
text="Do you want to remove/redact everything invisible from this file?\n\nNote:\n" +
" - This will remove all text layers (OCR can re-add SOME of that).\n" +
" - This functions by converting pages to images and back to PDF.\n" +
" - You may still leak some data, such as the use of this tool.\n" +
" - If you are doing something sensitive, verify this worked successfully!",
values=[
("no", "No"),
("yes", "Yes"),
],
).run()
if choice is None:
print("Exiting without changes.")
sys.exit()
elif choice == "no":
shutil.copy(fn_in, fn_out)
return False
else:
base = os.path.join(tmpdir, "images")
subprocess.check_call([f"pdftoppm -r 300 -cropbox -jpeg {fn_in} {base}"], shell=True)
subprocess.check_call([f"gm convert {base}-*.jpg {fn_out}"], shell=True)
return True
def ocr(fn_in, fn_out, runfile):
"""Prompt user to determine if they want OCR and, if so, OCR it."""
if "ocr" in runfile:
if runfile["ocr"] == "yes":
choice = True
else:
choice = False
else:
choice = yes_no_dialog(
title="OCR",
text="Perform Optical Character Recognition?",
).run()
if not choice:
shutil.copy(fn_in, fn_out)
else:
subprocess.check_call(["ocrmypdf", "--force-ocr", fn_in, fn_out])
def restore_metadata(fn_in, fn_out):
"""Reset metadata in PDF file."""
author = subprocess.check_output(["exiftool", fn_in, "-Author", "-s"]).decode()
publisher = subprocess.check_output(["exiftool", fn_in, "-Publisher", "-s"]).decode()
title = subprocess.check_output(["exiftool", fn_in, "-Title", "-s"]).decode()
subprocess.check_call(["exiftool", fn_out, f"-Author={author}", "-overwrite_original"])
subprocess.check_call(["exiftool", fn_out, f"-Publisher={publisher}", "-overwrite_original"])
subprocess.check_call(["exiftool", fn_out, f"-Title={title}", "-overwrite_original"])
def remove_metadata(fn_in, fn_out):
"""Remove metadata in PDF file."""
subprocess.check_call(["exiftool", fn_in, "-all:all=", "-overwrite_original"])
subprocess.call(["qpdf", "--linearize", fn_in, fn_out])
def main():
"""Main application function."""
args, runfile = parse_arguments()
tmpdir = tempfile.TemporaryDirectory()
fn_in = args.infile
fn_tmp1 = os.path.join(tmpdir.name, "work1.pdf")
fn_tmp2 = os.path.join(tmpdir.name, "work2.pdf")
fn_out = args.outfile
shutil.copy(fn_in, fn_tmp1)
hide_metadata = remove_hidden(fn_tmp1, fn_tmp2, tmpdir.name, runfile)
shutil.copy(fn_tmp2, fn_tmp1)
rotate(fn_tmp1, fn_tmp2, runfile)
shutil.copy(fn_tmp2, fn_tmp1)
crop(fn_tmp1, fn_tmp2, tmpdir.name, runfile)
subprocess.check_call(["pdftk", fn_tmp2, "cat", "output", fn_tmp1])
split_pages(fn_tmp1, fn_tmp2, tmpdir.name, runfile)
shutil.copy(fn_tmp2, fn_tmp1)
remove_pages(fn_tmp1, fn_tmp2, runfile)
shutil.copy(fn_tmp2, fn_tmp1)
deskew(fn_tmp1, fn_tmp2, tmpdir.name, runfile)
shutil.copy(fn_tmp2, fn_tmp1)
ocr(fn_tmp1, fn_tmp2, runfile)
shutil.copy(fn_tmp2, fn_tmp1)
if hide_metadata:
remove_metadata(fn_tmp1, fn_out)
else:
shutil.copy(fn_tmp1, fn_out)
restore_metadata(fn_in, fn_out)
if __name__ == "__main__":
main()