Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox 19 print PDF from JavaScript

I have a "Print" button which invokes the following JavaScript

window.frames.myPdfFrame.print();

where "myPdfFrame" refers to an iframe with a PDF for the src.

In Chrome and Firefox 18 (and below) this opens the print dialog as expected, but as of Firefox 19 I get the following error

Error: Permission denied to access property 'print'

I assume this is related to using the embedded PDF viewer released with Firefox 19 rather than the Adobe plugin. Using the print icon in the toolbar of the PDF plugin works as expected.

Is there a way to invoke the print dialog on an inline PDF in Firefox 19 from Javascript?

like image 430
Gareth Bowen Avatar asked Feb 21 '13 20:02

Gareth Bowen


People also ask

How do I print directly from PDF in Firefox?

Choose File > Print. Choose Adobe PDF as the printer in the Print dialog box. To customize the Adobe PDF printer setting, click the Properties (or Preferences) button. ... Click Print.


3 Answers

I tried the provided JSFiddle demo, with Firefox 22 and NoScript. Interestingly, when I first opened the fiddle, the iframe's content was blocked by NoScript, but the print dialog readily popped up! I clicked "Ok", and I got a taskbar toast saying the document about.blank was sent to the printer. Sure enough, I checked the printer and it spit out a nice white page. Not useful...

Now, after telling NoScript to "temporarily allow" the domain of the PDF (irs.gov), I reproduce the bug :

Error: Permission denied to access property 'print'

This other question made me wonder if it was a problem of same origin policy. So I tested it on my own webserver, with a PDF document hosted on the same machine. And... no dice !

I then tried my test page in IE 9 and Chrome. Both of them do show the print dialog, but when I print the document, in the case of IE 9, it's actually printing about:blank, and Chrome prints a grey rectangle saying "Loading"... looks like it's printing the whole page, not just the document in the iframe.

So, I agree with Jason Sperske that this doesn't seem possible.

UPDATE : this answer offers a solution that "just works", but only for PHP files generated by the Fpdf library...

like image 70
Miklos Aubert Avatar answered Nov 14 '22 00:11

Miklos Aubert


On recent versions of Firefox (since 19), you have to disable the bugged and native PDF viewer (pdf.js) in about:config. Set the pdfjs.disabled property to true and you will see the print window appearing using your script.

If there's a download starting, set the plugin.disable_full_page_plugin_for_types property to application/pdf.

like image 31
Epoc Avatar answered Nov 14 '22 00:11

Epoc


Please try the following code, it's working:

window.frames.myPdfFrame.focus();
window.print();
like image 1
Ankit Zalani Avatar answered Nov 14 '22 02:11

Ankit Zalani