File pdfbooklet.py of Package jw-env
#! /usr/bin/python
# This reorders the pages, so that you can print them as a booklet with okular
#
# 2013-06-31, jw, V0.1 -- initial draught, calling pdfinfo and pdftk.
# 2013-06-31, jw, V0.2 -- Need to insert blank pages. Rewrite using pyPDF
# 2013-06-31, jw, V0.3 -- Long and short side duplex flipping is hidous.
# We have two levels of switches.
import argparse
from pyPdf import PdfFileWriter, PdfFileReader, generic as Pdf
__VERSION__ = '0.3'
def main():
argp = argparse.ArgumentParser(epilog="version: "+__VERSION__, description="Reorder the pages, so that the document can be printed as a booklet. 2 pages on each side of an A4 sheet folded.")
argp.def_output = 'output.pdf'
argp.def_pages = None
argp.def_decrypt_key = ''
argp.add_argument("infile", metavar="INFILE", help="the input file")
argp.add_argument("-o", "--output", metavar="OUTFILE", default=argp.def_output,
help="write output to FILE; default: "+argp.def_output)
argp.add_argument("-d", "--decrypt-key", metavar="DECRYPT_KEY", default=argp.def_decrypt_key,
help="open an encrypted PDF; default: KEY='"+argp.def_decrypt_key+"'")
argp.add_argument("-F", "--first-page", metavar="FIRST_PAGE",
help="skip some pages at start of document; see also -L; default: all pages")
argp.add_argument("-L", "--last-page", metavar="LAST_PAGE",
help="limit pages processed; this counts pages, it does not use document \
page numbers; see also -F; default: all pages")
args = argp.parse_args() # --help is automatic
shuffle = []
pat = [4, 1, 2, 3]
input = PdfFileReader(file(args.infile, "rb"))
output = PdfFileWriter()
if input.getIsEncrypted():
if input.decrypt(args.decrypt_key):
if len(args.decrypt_key):
print("Decrypted using key='%s'." % args.decrypt_key)
else:
parser.exit("decrypt(key='%s') failed." % args.decrypt_key)
# last_page,first_page start counting at 0,
# args.last_page, args.first_page start counting at 1.
last_page = input.getNumPages()
first_page = 1
if args.last_page and int(args.last_page) < last_page:
last_page = int(args.last_page)
if args.first_page:
first_page = int(args.first_page)
if first_page > last_page:
first_page = last_page
print("input pages: %d-%d" % (first_page, last_page))
plist = {}
for i in range(first_page,last_page+1):
plist[i] = True
pat_idx = 0
pat_count = 0
while len(plist):
#print "plist todo=",plist
pnr = pat[pat_idx]+pat_count * len(pat)
pat_idx += 1
if pat_idx >= len(pat):
pat_idx = 0
pat_count += 1
#print "pnr=",pnr
if pnr <= last_page:
output.addPage(input.getPage(pnr-1)) # starts counting at 0
del(plist[pnr])
else:
output.addBlankPage()
print "writing ", args.output
outputStream = file(args.output, "wb")
output.write(outputStream)
print("Printing instructions - *Note*: The KDE printer dialoge has two places for Duplex!\n\n okular "+args.output)
print(" ->File->Print->Options")
print(" ->Options: Duplex Printing: (*) Short side")
print(" ->Pages: Pages Per Sheet: (*) 2")
print(" ->Properties->Advanced->Finishing
print(" ->Duplex: Off")
print(" ->[OK]")
print(" ->[Print]")
if __name__ == "__main__": main()