How to print a PDF from the browser

In a Web application, is it possible to force a PDF file to be printed on the client? If the browser is configured to open the PDF inside the window, I guess that calling window.print() will work, but some browsers (like mine) are configured to open the PDF externally.

asked Oct 15, 2008 at 15:31 Antoine Aubry Antoine Aubry 12.4k 10 10 gold badges 46 46 silver badges 76 76 bronze badges

8 Answers 8

The way google docs does it is by embedding JavaScript into the PDF that tells Acrobat Reader or any other compliant reader to print it.

You would need a PDF toolkit to do this with a random PDF.

answered Oct 15, 2008 at 15:32 Lou Franco Lou Franco 88.8k 14 14 gold badges 136 136 silver badges 197 197 bronze badges

You don't need PDF toolkit if you can use iTextSharp. Here is a link to a page tells you how to add javascript to the PDF using iTextSharp that will print the pdf. itextsharp.sourceforge.net/tutorial/ch11.html

Commented Dec 3, 2009 at 19:21 iTextSharp is a PDF toolkit. Commented Dec 4, 2009 at 19:03 checkout FPDF for PHP and this addon for FPDF: fpdf.de/downloads/addons/36 Commented Jan 21, 2012 at 0:26

When using TCPDF adding $pdf->IncludeJS('print(true);'); to your code opens print dialog (tested on Chrome and FF), see Example 53 of TCPDF.

Commented Apr 14, 2017 at 18:06
      
answered Jan 11, 2011 at 8:04 65 1 1 silver badge 1 1 bronze badge

This doesn't work in a Firefox 9 with Adobe reader plugin (on Ubuntu). ( exPDF.print is not a function ). Where did you get it to work?

Commented Feb 3, 2012 at 19:50 Paulo, exPDF is the ID of the item you're printing to PDF. Commented May 17, 2013 at 12:49

-1; in (my installation of) Chrome, an element doesn't have a .print() method, and so this fails. Maybe there is (or was) some platform that this works for, but it's not useful to me without some hint about what that platform is.

Commented Apr 26, 2017 at 10:27 This is an IE-only solution. Commented Feb 22, 2019 at 2:50

similarly to taeyoung's suggestion you can use an iframe to render the pdf and then use contentWindow.print();

answered Jul 27, 2011 at 22:28 Aaron Renoir Aaron Renoir 4,373 1 1 gold badge 43 43 silver badges 62 62 bronze badges

Like for taeyoung's solution, this doesn't work in a Firefox 9 with Adobe reader plugin (on Ubuntu). ( exPDF.print is not a function ). Where did you get it to work?

Commented Feb 3, 2012 at 19:55 I think this only works with webkit. The other browsers will just download it. Commented Feb 3, 2012 at 20:59

Actually, Firefox shows the PDF in the iframe, and ignores the method call (the error message is visible on the JS console in Firebug).

Commented Feb 3, 2012 at 22:52 this seems to work in ff9 on os x, $('#exPDF')[0].focus(); $('#exPDF')[0].contentWindow.print(); Commented Feb 3, 2012 at 23:03

If you want to print a PDF via the browser's print dialog, you can render the PDF inside an iframe on which you call the print() method.

// create iframe element const iframe = document.createElement('iframe'); // create object URL for your blob or file and set it as the iframe's src iframe.src = window.URL.createObjectURL(fileOrBlob); iframe.name = 'pdf'; // call the print method in the iframe onload handler iframe.onload = () => < const pdfFrame = window.frames["pdf"]; pdfFrame.focus(); pdfFrame.print(); >document.body.appendChild(iframe); 

You can also set iframe.hidden = true; to do all of this in a hidden iframe, which the user won't notice.

This solution should work with all major browser's except Legacy Microsoft Edge. If you need to be compatible with Legacy Microsoft Edge you can fall back to the npm package print-js: https://www.npmjs.com/package/print-js.

answered Jun 7, 2021 at 19:49 66 4 4 bronze badges

This answer actualy saved my life :) It is the only solution that worked for me for printing PDF-s. I'm trying to print PDF document unattended from Chrome or/and Firefox. Combined with kiosk mode (Chrome) or Firefoxes setting "print.always_print_silent" it simply works. Thank you, konnic :)

Commented Dec 20, 2022 at 14:43

you can set a http header to application/pdf and then force a new window open with javascript and print that way. but who would really do that? i mean come on now.

answered Dec 12, 2008 at 21:30 theman_on_vista theman_on_vista

you can use the simple amazing library printjs "http://printjs.crabbly.com" it takes PDF file and print it without showing print dialog if you need , a simple way to do it below :

   
answered Dec 17, 2017 at 13:01 1,210 17 17 silver badges 22 22 bronze badges

i was stoked on this one until i got a nice surprise in the console while testing on staging: "PrintJS currently doesn't support PDF printing in Firefox, Internet Explorer and Edge." lol

Commented Jul 5, 2018 at 17:19 This Package is very helpful. Compatible with NPM as well. Thanks Commented Jul 16 at 1:48

Do you mean that you want to force the file to be sent to a printer? Are you thinking of the Law of Unintended Consequences -- the user's device isn't connected to a printer? Could be a BlackBerry, could be a laptop on wi-fi. What if the user doesn't want it to go to the default printer?

answered Oct 15, 2008 at 16:35 32.7k 8 8 gold badges 62 62 silver badges 93 93 bronze badges My only printer at home is PDFCreator. Commented Dec 12, 2008 at 21:31

Spot on. Whenever I ask this kind of question then go ahead and implement it, it turns out I really should have wished for someone to point out what DOK pointed out.

Commented Dec 12, 2008 at 21:52

Yeah, but then your boss, who knows that only ten employees at his office will ever use this web page, and knows they have printers, will be baffled by the fact that you can make a computer do almost anything, but you cannot make it open a print dialog.

Commented Feb 3, 2010 at 21:02

You can't print a PDF document directly from browser using Javascript. The Javascript function window.print() use the browser printing function and this is not what you need. You can achieve your aim starting the print through Java Web Start. Put your PDF document directly into the jnlp so you can run a Java program that recieve the raw PDF document as argument. Now you're running in the system and no longer in the browser, so you can directly interface with printing driver through JAVA API. This seem quite simple but really it's not because the JAVA printing API doesn't accept a file as input but a particular data structure that implements the ava.awt.print.Pageable interface.

Exist a web service at www.pdfprint.it that do all the work for you. Here a snippet taken from the official documentation.

You only need to get the jnlp file, put in your PDF document and send the jnlp to the browser. The JAVA program that run the printing will be dowloaded directly from the web service. You can also set some printing options as copies, sides, and so on