Unable to Print PDF when loaded in iFrame
See original GitHub issueI 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:
- Created 9 years ago
- Reactions:8
- Comments:24
Top GitHub Comments
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 thatdocument.domain
ispdf.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.)So far, no solution has been proposed