13

I have a PDF file and I am trying to print it via Javascript. I have tried this embed trick: Silent print a embedded PDF however, the print function never becomes available, it is always undefined.

I have tried the Iframe trick with this code:

function printPDF() {
if(document.getElementById("pdfDocument").contentWindow.document.readyState === "complete") {   
    document.getElementById("pdfDocument").focus();
    document.getElementById("pdfDocument").contentWindow.print();
} else {
    setInterval(printPDF(), 1000);
}
}

(pdfDocument is the ID of the iframe) This does pop up the print dialogue, but printing a blank page. I would love the embed tag way to work. But why is the print function never becoming available?

Most of the posts on this subject are quite old. What is the best HTML5/jQuery way to do it? (or just regular JS at this point)

EDIT:

here is the JS code for the embed tag:

function printPDF() {
alert(document.getElementById("pdfDocument").print);
//Wait until PDF is ready to print    
 if (typeof document.getElementById("pdfDocument").print == 'undefined') {
     setTimeout(function(){printPDF();}, 1000);
 } else {
     var x = document.getElementById("pdfDocument");
     x.print();
 }
}

This keeps altering "undefined" every second. The Print option is never available. Any Ideas?

Community
  • 1
  • 1
mmaceachran
  • 3,178
  • 7
  • 53
  • 102
  • To clarify: you want to print an iframe, you want to print silently, or both? – UIlrvnd Sep 16 '13 at 19:24
  • Either/ all LOL, anything at this point! – mmaceachran Sep 16 '13 at 19:40
  • :S how do you know print always returns undefined, you don't have any return statements? – UIlrvnd Sep 16 '13 at 19:42
  • Oh, and a [jsFiddle](http://jsfiddle.net/) would help... – UIlrvnd Sep 16 '13 at 19:53
  • seems like browser won't allow that way to be used - security etc. as I understand it could be done within PDF - https://acrobatusers.com/tutorials/javascript_console – nettutvikler Nov 12 '14 at 06:09
  • is the pdf file hosted on the same server or coud this be caused by CORS? – Philip G Nov 17 '14 at 17:17
  • It's been a few years since any activity on this question...has anyone figured it out? Chrome now displays the PDF perfectly in the browser, and if you right click > Print it shows the print preview perfectly. But if you do a `window.print()` it shows up blank. It has to be possible to trigger the result of right click > Print ... – jtate Nov 07 '16 at 13:30
  • 1
    I know this is very old, but... did anyone else notice that `setInterval(printPDF(), 1000);` is setting the interval to the return result of printPDF()? It happens to work since you've created a recursive function, but it's not actually utilizing the interval... – Dawson Toth Aug 01 '17 at 10:16

4 Answers4

16

I put a bounty on this questions a week or so ago and it's expired. I'm going to post what I learned here after a lot of research for anyone in the future who might find this.

PDF's are displayed differently based on browser, browser version, browser configuration, and Operating System. There are a lot of variables so I'll talk about the most common situations here.

  • On all browsers I was unable to call any sort of print() method through Javascript, I was only able to use PdfActions. The OPENACTION would call print. I embedded these into the PDF using iText.

  • Chrome uses Adobe's viewer, which doesn't give access to any sort of print() method but does execute PdfActions embedded in the PDF. So you can embed an 'OpenAction' in a PDF and have the PDF call print whenever it's opened from any application that looks at those actions.

  • Firefox (above a certain version, all recent versions though) uses the Adobe viewer in Windows, which also recognizes PdfActions. However, in OSX it loses support for the Adobe viewer and switches to the baked in Firefox viewer (pdf.js). Which does not support PdfActions.

  • IE: I didn't really test much on IE. Mostly because I gave up on printing PDF's from Javascript after Firefox didn't work on OSX (a req. for me).

My PDF's were being generated by a server that I control so I ended up making service changes in my server and adding a get PNG service that generated a PNG based on the same markup that the PDF generation uses. Browsers handle images much better than PDFs, which I knew going in, but hoped that I would just be able to re-use the PDF generation service since it's used elsewhere in my code.

It doesn't answer the question, but it's all the information I have. My suggestion to anyone who might find this in the future: ditch PDF if possible in this case and go simpler. Otherwise, please update this question if you know how to call print() through Javascript in FF preview pdf viewer in OSX.

-Phil

philhan
  • 590
  • 1
  • 6
  • 19
2

With Javascript, I am not sure we can do this. However can be achieved using script injection into the pdf file. If my understanding is correct this is what Google does.

For example.

  1. Open the url : https://drive.google.com/viewerng/viewer?url=http://www.energy.umich.edu/sites/default/files/pdf-sample.pdf
  2. Now click on print icon.
  3. As you can see a new window with print command injected into the pdf is opened. Once the pdf is loaded the built in print command is triggered. You can see the print triggered whenever you refresh the page. That means the print behavior is attached to the document load event.

We can use iTextSharp to simulate above behavior.

Rama Kathare
  • 920
  • 9
  • 29
  • Two comments - first of all this trick doesn't work on my Mac; it's dependent on operating system and browser (and even browser version / configuration). But secondly, you are correct to assume it's a PDF trick, the PDF here contains an "OpenAction" with a bit of Javascript to silently print the PDF file. – David van Driessche Nov 18 '14 at 08:01
2

There is a way to render the whole pdf in a browser (instead of embedding an external application), which gives you full access to browser APIs in regard to the pdf.

This is Mozilla's pdf implementation in JavaScript: https://github.com/mozilla/pdf.js/
And this is the showcase: http://mozilla.github.io/pdf.js/web/viewer.html (notice the print button on upper right).

Check out the viewer code here for the details on how it works: https://github.com/mozilla/pdf.js/blob/master/web/viewer.js

On the minus side — it's going to be way harder, than just embedding.
On the plus side, it will actually work.

Arseny Smoogly
  • 600
  • 2
  • 7
  • While I appreciate the idea, I think the 2nd to last line really sums up my feelings. Doing this would be a crazy amount of work just to access a print() function in Javascript. – philhan Nov 20 '14 at 19:00
  • I use PDF.JS for other purposes. Unfortunately, its print rendering is not as high quality as Adobe Reader's... hopefully it will be fixed in the future. – speedplane Mar 29 '15 at 21:34
0

In Chrome you can run:

var toolbar = document.querySelector('#toolbar');
toolbar.shadowRoot.querySelector('#print').click();
stomy
  • 1,778
  • 1
  • 15
  • 16