This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
pixels-to-pdf-unpriv
executable file
·139 lines (123 loc) · 4.2 KB
/
pixels-to-pdf-unpriv
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
#!/usr/bin/env python3
import glob
import os
import sys
import subprocess
def print_flush(s=""):
print(s)
sys.stdout.flush()
def main():
num_pages = len(glob.glob("/dangerzone/page-*.rgb"))
print_flush(f"Document has {num_pages} pages")
# Convert RGB files to PDF files
for page in range(1, num_pages + 1):
filename_base = f"/dangerzone/page-{page}"
rgb_filename = f"{filename_base}.rgb"
width_filename = f"{filename_base}.width"
height_filename = f"{filename_base}.height"
png_filename = f"/tmp/page-{page}.png"
ocr_filename = f"/tmp/page-{page}"
pdf_filename = f"/tmp/page-{page}.pdf"
with open(width_filename) as f:
width = f.read().strip()
with open(height_filename) as f:
height = f.read().strip()
if os.environ.get("OCR") == "1":
# OCR the document
print_flush(f"Converting page {page} from pixels to searchable PDF")
args = [
"gm",
"convert",
"-size",
f"{width}x{height}",
"-depth",
"8",
f"rgb:{rgb_filename}",
f"png:{png_filename}",
]
try:
p = subprocess.run(args, timeout=60)
except subprocess.TimeoutExpired:
print_flush(
"Error converting pixels to PNG, convert timed out after 60 seconds"
)
sys.exit(1)
if p.returncode != 0:
print_flush(f"Page {page} conversion failed: {p.stdout}")
sys.exit(1)
args = [
"tesseract",
png_filename,
ocr_filename,
"-l",
os.environ.get("OCR_LANGUAGE"),
"--dpi",
"70",
"pdf"
]
try:
p = subprocess.run(args, timeout=60)
except subprocess.TimeoutExpired:
print_flush(
"Error converting PNG to searchable PDF, tesseract timed out after 60 seconds"
)
sys.exit(1)
if p.returncode != 0:
print_flush(f"Page {page} conversion failed: {p.stdout}")
sys.exit(1)
else:
# Don't OCR
print_flush(f"Converting page {page} from pixels to PDF")
args = [
"gm",
"convert",
"-size",
f"{width}x{height}",
"-depth",
"8",
f"rgb:{rgb_filename}",
f"pdf:{pdf_filename}",
]
try:
p = subprocess.run(args, timeout=60)
except subprocess.TimeoutExpired:
print_flush(
"Error converting RGB to PDF, convert timed out after 60 seconds"
)
sys.exit(1)
if p.returncode != 0:
print_flush(f"Page {page} conversion failed: {p.stdout}")
sys.exit(1)
print_flush()
# Merge pages into a single PDF
print_flush(f"Merging {num_pages} pages into a single PDF")
args = ["pdfunite"]
for page in range(1, num_pages + 1):
args.append(f"/tmp/page-{page}.pdf")
args.append(f"/tmp/safe-output.pdf")
try:
p = subprocess.run(args, timeout=60)
except subprocess.TimeoutExpired:
print_flush(
"Error merging pages into a single PDF, pdfunite timed out after 60 seconds"
)
sys.exit(1)
if p.returncode != 0:
print_flush(f"Merge failed: {p.stdout}")
sys.exit(1)
# Compress
print_flush("Compressing PDF")
compress_timeout = num_pages * 3
try:
p = subprocess.run(
["ps2pdf", "/tmp/safe-output.pdf", "/tmp/safe-output-compressed.pdf"],
timeout=compress_timeout,
)
except subprocess.TimeoutExpired:
print_flush(f"Error compressing PDF, ps2pdf timed out after {compress_timeout} seconds")
sys.exit(1)
if p.returncode != 0:
print_flush(f"Compression failed: {p.stdout}")
sys.exit(1)
if __name__ == "__main__":
main()