question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to Print PDF when loaded in iFrame

See original GitHub issue

I am attempting to use Javascript to focus and print a PDF file that is loaded within a iframe I dynamically placed into the DOM. This issue is specifically occurring for me in Firefox.

My code resembles the following:

<iframe name="printer_frame" id="printer_frame" src="http://domain.com/media/eparcel_label_1413020567.pdf"></iframe>
window.frames['printer_frame'].window.focus();
window.frames['printer_frame'].window.print();

I receive the following error:

Error: Permission denied to access property ‘print’

My research is telling me that this should work, further reading has lead me to believe that this may be a bug. Any help would be appreciated.

Edit

I tested the functionality by replacing the PDF file with a screen shot in PNG format of the first page within it and the print functionality worked.

Edit

Further testing. Added pdfjs to my chrome install, and attempted to print with the same code above. same error:

SecurityError: Blocked a frame with origin “http://domain.com” from accessing a cross-origin frame.

code: 18
message: "Blocked a frame with origin "http://domain.com" from accessing a cross-origin frame."
name: "SecurityError"
stack:
"Error: Blocked a frame with origin "http://domain.com" from accessing a cross-origin frame.
    at Error (native)
    at <anonymous>:2:34
    at Object.InjectedScript._evaluateOn (<anonymous>:730:39)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:669:52)
    at Object.InjectedScript.evaluate (<anonymous>:581:21)"

I should make it clear that the PDF file is being loaded on the same domain.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:8
  • Comments:24

github_iconTop GitHub Comments

15reactions
dahjellecommented, May 8, 2015

I think I know the reason for this, at least in Firefox. In a simple example such as the above, if you use the Firefox DevTools to examine document.domain in both the main page and in the iframe, I found that document.domain is pdf.js for the PDF.js renderer—so the browser’s cross-origin restrictions are kicking in.

Additionally, if I built PDF.js from source, and set my iframe to web/viewer.html?file=, then calling telling the iframe to print (as in the OP), it worked fine.

Is there a way for PDF.js (at least when running in Firefox) to accept messages from other windows, perhaps via something like window.postMessage? (I’ve never used it, so I don’t know if it is appropriate for this use-case or not.)

3reactions
ghostman-gitcommented, Aug 15, 2018

So far, no solution has been proposed

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to print the pdf loaded in iframe - Stack Overflow
If I load the pdf file from my local system I am to print it . I used the below code to print...
Read more >
Printing PDF file in iframe with window.print() results in a blank ...
When I loaded a PDF file to an iframe and called “print()“ from javascript the print preview shows a blank page. Even the...
Read more >
PDF in an iframe can't be printed with JavaScript in Firefox 4.
Reproducible: Always Steps to Reproduce: 1. Go to the test page at http://bertilow.com/div/pdftest/index.html 2. Wait for the PDF to load, then click ...
Read more >
Empty print on iframe | JavaScript - EJ 2 Forums - Syncfusion
I am using ej2 pdf viewer to show user a pdf inside of an iframe and using the print module to print said...
Read more >
Print.js - Javascript library for HTML elements, PDF and image ...
PDF files must be served from the same domain as your app is hosted under. Print.js uses iframe to load files before printing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found