I know how to open a webpage in a new window and add javascript so the print dialog pops up. Is there a way to do a similar thing with a PDF file?
param=dialy it will open the generated pdf in a popup, and then I can press the print button to open the printer dialog and print it.
NitroPDF and Adobe Acrobat definitely support JavaScript in PDF files.
To enable JavaScript in Adobe Acrobat Reader Open Adobe Acrobat Reader. Go to Edit > Preferences. In Categories, click JavaScript. In the JavaScript section, check Enable Acrobat JavaScript.
Yes you can...
PDFs have Javascript support. I needed to have auto print capabilities when a PHP-generated PDF was created and I was able to use FPDF to get it to work:
http://www.fpdf.org/en/script/script36.php
I usually do something similar to the approach given by How to Use JavaScript to Print a PDF (eHow.com), using an iframe
.
function printTrigger(elementId) { var getMyFrame = document.getElementById(elementId); getMyFrame.focus(); getMyFrame.contentWindow.print(); }
(an onClick
on an a
or button
or input
or whatever you wish)
<input type="button" value="Print" onclick="printTrigger('iFramePdf');" />
<iframe id="iFramePdf" src="myPdfUrl.pdf" style="display:none;"></iframe>
Bonus Idea #1 - Create the iframe
and add it to your page within the printTrigger();
so that the PDF isn't loaded until the user clicks your "Print" button, then the javascript can attack! the iframe and trigger the print dialog.
Bonus Idea #2 - Extra credit if you disable your "Print" button and give the user a little loading spinner or something after they click it, so that they know something's in process instead of clicking it repeatedly!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With