-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimgs-to-pdf
executable file
·40 lines (31 loc) · 1001 Bytes
/
imgs-to-pdf
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
#!/usr/bin/env python3
# Merge multiple image files into one PDF
# requires:
# convert (part of ImageMagick)
# pdfunite (part of poppler)
import sys
import os
import subprocess
import tempfile
def print_usage_exit():
sys.stderr.write('%s IMAGE_FILE [IMAGE_FILE]... PDF_FILE\n' %
(program_name(),))
sys.exit(2)
def program_name():
return os.path.basename(sys.argv[0])
def main(args):
if len(args) < 2:
print_usage_exit()
out_fname = args[-1]
if os.path.splitext(out_fname)[1].lower() != '.pdf':
sys.stderr.write("ERROR: output file \"%s\" doesn't end in .pdf\n" % (out_fname,))
print_usage_exit()
temp_pdfs = []
for i in args[:-1]:
(tfd, tfname) = tempfile.mkstemp(suffix='.pdf')
subprocess.check_call(['convert', i, tfname])
os.close(tfd)
temp_pdfs.append(tfname)
subprocess.check_call(['pdfunite'] + temp_pdfs + [ args[-1] ])
if __name__ == '__main__':
main(sys.argv[1:])