Skip to content

Instantly share code, notes, and snippets.

@yk2kus
Forked from rms1000watt/python-printJS-in-pdf.py
Created February 13, 2019 16:11
Show Gist options
  • Save yk2kus/b56823ad8a39ee7492023f428b589ba8 to your computer and use it in GitHub Desktop.
Save yk2kus/b56823ad8a39ee7492023f428b589ba8 to your computer and use it in GitHub Desktop.
Python script to add JS to pdf so the pdf will print immediately when opened
# IE users need: https://get.adobe.com/reader/
from PyPDF2 import PdfFileWriter, PdfFileReader
output = PdfFileWriter()
ipdf = PdfFileReader(open('old.pdf', 'rb'))
for i in xrange(ipdf.getNumPages()):
page = ipdf.getPage(i)
output.addPage(page)
with open('new.pdf', 'wb') as f:
output.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
output.write(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment