Last active
March 29, 2024 11:24
-
-
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
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
# 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) |
what does javascript does
Updated version for python 3.x
from PyPDF2 import PdfWriter, PdfReader
output = PdfWriter()
ipdf = PdfReader(open('old.pdf', 'rb'))
for i in range(len(ipdf.pages)):
page = ipdf.pages[i]
output.add_page(page)
with open('new.pdf', 'wb') as f:
output.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
output.write(f)
Hi @RaymonDev,
This not working for Chrome, the PDF is opened without any action
what is the expected output for this.
I honestly have no idea. I just translated the code to 3.x, working on it though!!
Ok, Did you try it in Chrome ?
Yes, still not working
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Greate. I was trying to customize the solution to add a copy to clipboard function with file opening and it is not working. Any idea of how to do it?