Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a PDF file's print dialog be opened with Javascript?

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?

like image 353
user83358 Avatar asked Mar 26 '09 21:03

user83358


People also ask

How do I open print dialog after PDF?

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.

Can you use JavaScript in a PDF?

NitroPDF and Adobe Acrobat definitely support JavaScript in PDF files.

How do I open a JavaScript PDF?

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.


2 Answers

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

like image 56
Edward Avatar answered Oct 25 '22 08:10

Edward


I usually do something similar to the approach given by How to Use JavaScript to Print a PDF (eHow.com), using an iframe.

  1. a function to house the print trigger...

    function printTrigger(elementId) {     var getMyFrame = document.getElementById(elementId);     getMyFrame.focus();     getMyFrame.contentWindow.print(); } 
  2. an button to give the user access...

    (an onClick on an a or button or input or whatever you wish)

    <input type="button" value="Print" onclick="printTrigger('iFramePdf');" /> 
  3. an iframe pointing to your PDF...

    <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!

like image 29
brandonjp Avatar answered Oct 25 '22 10:10

brandonjp