Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Pdf from javascript embed tag

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?

like image 659
mmaceachran Avatar asked Sep 16 '13 19:09

mmaceachran


People also ask

How do I print a PDF in JavaScript?

Print PDF using a Dynamic iframe I'll create an iframe dynamically using a JavaScript code. I'll pass the document's source as a parameter to the function, and provide the source to the dynamically created iframe and print its contents. In this method, you will print the PDF document without opening it.

Can I iframe a PDF?

Since the path accepts an embed parameter, you can embed the PDF viewer in your document with iframes. Your embedded file now displays in your IT Glue document with Google Drive viewing tools.

How do I download a PDF embedded in a Web page?

You can locate this by right-clicking on the embedded document on the website then select “inspect” or inspect elements. Then once the codes are revealed, find the iframe and the src, which represents the PDF embedded, and copy the link inside the iframe code. Paste these codes on another web browser and viola!


2 Answers

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

like image 113
philhan Avatar answered Nov 11 '22 12:11

philhan


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.

like image 23
Rama Kathare Avatar answered Nov 11 '22 13:11

Rama Kathare