Created
January 11, 2017 12:53
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
.