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
In Chrome you can run:
var toolbar = document.querySelector('#toolbar');
toolbar.shadowRoot.querySelector('#print').click();
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.
We can use iTextSharp to simulate above behavior.
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
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.