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?
10 Answers
I usually do something similar to the approach given by How to Use JavaScript to Print a PDF (eHow.com), using an iframe
.
a function to house the print trigger...
function printTrigger(elementId) { var getMyFrame = document.getElementById(elementId); getMyFrame.focus(); getMyFrame.contentWindow.print(); }
an button to give the user access...
(an
onClick
on ana
orbutton
orinput
or whatever you wish)<input type="button" value="Print" onclick="printTrigger('iFramePdf');" />
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!
-
2contentWindow doesn't seem to be defined when loading a PDF in Google Chrome– JeroenCommented Jul 18, 2011 at 14:02
-
@jtietema I believe that if you're loading the PDF straight into the browser as the entire and only document (ie, not in an iframe), then
contentWindow
is undefined. But as long as you're loading the PDF inside of an iframe AND from the same domain as your parent DOM, then this should work fine. Commented Aug 4, 2011 at 9:04 -
4This answer no longer works in Firefox (currently v25) stackoverflow.com/questions/15011799/… See the bug report: bugzilla.mozilla.org/show_bug.cgi?id=911444 Commented Dec 1, 2013 at 23:29
-
1This is giving an error
Uncaught DOMException: Blocked a frame with origin "http://localhost:8080" from accessing a cross-origin frame.
Commented Feb 28, 2018 at 10:34
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:
Just figured out how to do this within the PDF itself - if you have acrobat pro, go to your pages tab, right click on the thumbnail for the first page, and click page properties. Click on the actions tab at the top of the window and under select trigger choose page open. Under select action choose "run a javascript". Then in the javascript window, type this:
this.print({bUI: false, bSilent: true, bShrinkToFit: true});
This will print your document without a dialogue to the default printer on your machine. If you want the print dialog, just change bUI to true, bSilent to false, and optionally, remove the shrink to fit parameter.
Auto-printing PDF!
-
2This helped me, but it should be noted that according to documentation this could also be achieved through using the printParams object, which might come in handy because I read somewhere else that print method ignores all other arguments if printParams is passed (I needed it for NumCopies parameter)– alh84001Commented Sep 26, 2011 at 11:50
-
The Chrome PDF plugin does not bring up the print dialog if you use the
printParams
object but it does with the javascript shown in this answer. Either works if Acrobat is doing PDF display. Neither works for FireFox 20 and 21's PDFJS display code. Commented May 22, 2013 at 18:39 -
Just for the record, because it drove me crazy; the print parameters (bUI, bSilent, bShrinkToFit) are documented in "Developing Acrobat Applications Using JavaScript" - "Printing PDF Documents": adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/…– OmarCommented Aug 28, 2014 at 1:32
I use named action
instead of javascript because javascript often is disabled, and if it isn't it gives a warning.
My web application creates a postscript file that then is converted with ghostscript to a pdf. I want it to print automatically because the user has already clicked on print inside my application. With the information about named actions from @DSimon above, I researched how to solve this. It all boils down to insert the string /Type /Action /S /Named /N /Print
at the right place in the pdf.
I was thinking of writing a small utility, but it has to parse the pdf to find the root node, insert /OpenAction
with a reference an object with the action, and recalculate the byte-offsets in xref.
But then I found out about pdfmark which is an extension to postscript to express, in postscript syntax, idioms that are converted to pdf by Adobes distiller or by ghostscript.
Since I'm already using ghostscript, all I have to do is append the following to the end of my postscript file:
%AUTOPRINT
[ /_objdef {PrintAction} /type /dict /OBJ pdfmark
[ {PrintAction} << /Type /Action /S /Named /N /Print >> /PUT pdfmark
[ {Catalog} << /OpenAction {PrintAction} >> /PUT pdfmark
and ghostscript will create the action, link it, and calculate the xref offsets. (In postscript %
is a comment and PrintAction
is my name for the object)
By looking at the PDF I see that it has created this:
1 0 obj
<</Type /Catalog /Pages 3 0 R
/OpenAction 9 0 R
/Metadata 10 0 R
>>
endobj
9 0 obj
<</S/Named
/Type/Action
/N/Print>>endobj
1 0
is object 1, revision 0, and 9 0
is object 9, revision 0. In the pdf-trailer is says that it is object 1 that is the root node. As you can see there is a reference from object 1, /OpenAction
to run object 9 revision 0.
With ghostscript it's possible to convert a pdf to postscript (pdf2ps), append the text above, and convert it back to pdf with ps2pdf
. It should be noted that meta-information about the pdf is lost in this conversion. I haven't searched more into this.
Embed code example:
<object type="application/pdf" data="example.pdf" width="100%" height="100%" id="examplePDF" name="examplePDF"><param name='src' value='example.pdf'/></object>
<script>
examplePDF.printWithDialog();
</script>
May have to fool around with the ids/names. Using adobe reader...
-
-
1For people reading this: the correct function name is "Print()" with a capital P. In this example the correct call would be
examplePDF.Print()
– ErwinCommented Apr 25, 2014 at 12:59
If you know how PDF files are structured (or are willing to spend a little while reading the spec), you can do it this way.
Use the Named Action "Print" in the OpenAction field of the Catalog object; the "Print" action is undocumented, but Acrobat Reader and most of the other major readers understand it. A nice benefit of this approach is that you don't get any JavaScript warnings. See here for details: http://www.gnostice.com/nl_article.asp?id=157
To make it even shinier, I added a second Action, URI, directing the reader to go back to the page that originated the request. Then I attached this Action to the first Named action using its Next field. With content disposition set to "inline", this makes it so that when the user clicks on the print link:
- It opens up Adobe Reader in the same tab and loads the file
- It immediately shows the print dialog
- As soon as the Print dialog is closed (whether they hit "OK" or "cancel"), the browser tab goes back to the webpage
I was able to do all these changes in Ruby easily enough using only the File and IO modules; I opened the PDF I had generated with an external tool, followed the xref to the existing Catalog section, then appended a new section onto the PDF with an updated Catalog object containing my special OpenAction line, and also the new Action objects.
Because of PDF's incremental revision features, you don't have to make any changes to the existing data to do this, just append an additional section to the end.
-
1Thank you for your post, that led me into the right direction. It works perfectly!– someCommented Jul 3, 2012 at 9:05
Why not use the Actions menu option to set this?
Do the following: If you have Acrobat Pro, go to your pages tab, right click on the thumbnail for the first page, and click page properties. Click on the actions tab at the top of the window and under select trigger choose page open. Under select action choose 'Execute a menu item'. Click the Add button then select 'File > Print' then OK. Click OK again and save the PDF.
If you are using the prawn gem for Ruby on Rails to generate your PDF, you can use the following additional gem to active the print dialog:
Another solution:
<input type="button" value="Print" onclick="document.getElementById('PDFtoPrint').focus(); document.getElementById('PDFtoPrint').contentWindow.print();">
if you embed the pdf in your webpage and reference the object id, you should be able to do it.
eg. in your HTML:
<object ID="examplePDF" type="application/pdf" data="example.pdf" width="500" height="500">
in your javascript:
<script>
var pdf = document.getElementById("examplePDF");
pdf.print();
</script>
I hope that helps.
-
1Did you test this code? I get an error that says "Object doesn't support this property of method".– ichibanCommented May 11, 2009 at 19:35