Skip to content

Instantly share code, notes, and snippets.

@rms1000watt
Last active March 29, 2024 11:24
Show Gist options
  • Save rms1000watt/177970ce9fb0970e765700f12695231d to your computer and use it in GitHub Desktop.
Save rms1000watt/177970ce9fb0970e765700f12695231d 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)
@khaledesmail
Copy link

Hi @RaymonDev,
This not working for Chrome, the PDF is opened without any action
what is the expected output for this.

@RaymonDev
Copy link

I honestly have no idea. I just translated the code to 3.x, working on it though!!

@khaledesmail
Copy link

Ok, Did you try it in Chrome ?

@RaymonDev
Copy link

Yes, still not working

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