1

I have a page with button and IFRAME. Inside IFRAME, I am loading a PDF file dynamically which is coming from different domain.

when I try to print file using the button action I am getting the following error.

Uncaught SecurityError: Blocked a frame with origin "http://localhost:8080" from accessing a frame with origin "http://www.cplusplus.com". Protocols, domains, and ports must match.

If I load the pdf file from my local system I am to print it .

I used the below code to print the iframe Pdf

var iframe = document.querySelector("#unofficialtranscript");
iframe.focus();
iframe.contentWindow.print();

Any idea how I can print the file which is loaded into the iframe which is on another domain .

mscdex
  • 104,356
  • 15
  • 192
  • 153

2 Answers2

0

I think you would need to have a look at the same origin policy.Javascript only calls a window or an iframe only if the policy is accepted.

You may need to use Postmessage API instead.

Please have a look here.

Community
  • 1
  • 1
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26
0

If both the parent DOM and child DOM (i.e., iframe) are from same domain then it will work fine.

If not, then use libraries like Porthole for cross-communication between different domains.

Demo site: http://sandbox.ternarylabs.com/porthole/

Note: You should have access to both the domains being used in your code.

Manoj Namodurai
  • 529
  • 2
  • 7