Take the 2-minute tour ×

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?

share|improve this question
There is no need to use javascript for this. Use a named action instead, that will work even if javascript is disabled. –  some Nov 11 '13 at 22:07
add comment

9 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:


share|improve this answer
At a minimum, just add <script type="text/javascript">print();</script> to your PDF. (I'm using dompdf) –  Mark Nov 9 '11 at 18:57
add comment

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

share|improve this answer
contentWindow doesn't seem to be defined when loading a PDF in Google Chrome –  jtietema Jul 18 '11 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. –  brandonjp Aug 4 '11 at 9:04
This answer no longer works in Firefox (currently v25) stackoverflow.com/questions/15011799/… See the bug report: bugzilla.mozilla.org/show_bug.cgi?id=911444 –  chrishiestand Dec 1 '13 at 23:29
add comment

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!

share|improve this answer
This 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) –  alh84001 Sep 26 '11 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. –  Lee Meador May 22 '13 at 18:39
add comment

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:

[ /_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

9 0 obj

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.

share|improve this answer
add comment

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>


May have to fool around with the ids/names. Using adobe reader...

share|improve this answer
TypeError: Object has no method 'printWithDialog' –  Salizar Marxx Jan 14 '13 at 21:00
For people reading this: the correct function name is "Print()" with a capital P. In this example the correct call would be examplePDF.Print() –  Erwin Apr 25 at 12:59
add comment

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:

  1. It opens up Adobe Reader in the same tab and loads the file
  2. It immediately shows the print dialog
  3. 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.

share|improve this answer
Thank you for your post, that led me into the right direction. It works perfectly! –  some Jul 3 '12 at 9:05
add comment

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.

share|improve this answer
add comment

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:


share|improve this answer
add comment

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:


var pdf = document.getElementById("examplePDF");



I hope that helps.

share|improve this answer
Did you test this code? I get an error that says "Object doesn't support this property of method". –  ichiban May 11 '09 at 19:35
add comment

Your Answer


