Skip to content

Instantly share code, notes, and snippets.

@runjak
Created January 11, 2017 12:53
Show Gist options
  • Save runjak/639640be69e5194190eb097bc0db6fe5 to your computer and use it in GitHub Desktop.
Save runjak/639640be69e5194190eb097bc0db6fe5 to your computer and use it in GitHub Desktop.
A short python script to concatenate several .pdf files into one so that they can be printed duplex. Uses gs and pdfinfo.
import subprocess
fileStack = []
with open('./printMe') as f:
files = f.read().split('\n')
for file in files:
if file == '':
continue
fileStack.append(file)
proc = subprocess.run(args=['pdfinfo', file],
stdout=subprocess.PIPE)
for output in proc.stdout.splitlines():
if output.startswith(b'Pages'):
pageCount = int(output.split(b' ')[-1])
if pageCount % 2 == 1:
fileStack.append('empty.pdf')
gsCommand = ['gs', '-dBATCH', '-dNOPAUSE', '-q',
'-sDEVICE=pdfwrite',
'-dPDFSETTINGS=/prepress',
'-sOutputFile=printMe.pdf'] + fileStack
subprocess.run(args=gsCommand)
@runjak
Copy link
Author

runjak commented Jan 11, 2017

Run with python printMe.py.
The script expects a file named printMe containing the paths to all .pdf files that shall be concatenated.
For concatenation a blank page may need to be inserted.
The blank page is expected to be available as empty.pdf.
The produced file will be named printMe.pdf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment