2

I have a project that uses jQuery and I need to print a PDF file that is stored online (Ex. http://www2.hawaii.edu/~freeman/courses/phil360/16.%20Myth%20of%20Sisyphus.pdf). While the print() function works just fine if the PDF is in the local directory, the print preview is showing blank whenever the iframe's src is a url link.

My iframe:

<iframe id="print-pdf" hidden></iframe>

My jquery:

 var ifrm = document.getElementById("print-pdf");
 ifrm.src = 'http://www2.hawaii.edu/~freeman/courses/phil360/16.%20Myth%20of%20Sisyphus.pdf';
 console.log(ifrm.src);
 ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
 ifrm.document.open();
 ifrm.document.close();

 $('#print-pdf').load(function () {
     if (navigator.userAgent.indexOf("Firefox") > 0) {
             document.getElementById("print-pdf").contentWindow.print();
     }
     else {
             document.getElementById("print-pdf").contentWindow.document.execCommand('print', false, null);
     }
 );

I haven't found any solution regarding specific problem anywhere so any suggestion/s would be wonderful!

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
user3140026
  • 135
  • 10
  • 1
    Is this script located in the same domain (http://www2.hawaii.edu)? Or are they different? There might be a permission-problem (cross-origin). [This](https://stackoverflow.com/a/38244824/2401386) will help you. – Blauharley Jan 01 '18 at 10:52

0 Answers0