1

I dynamically create a pdf in a iframe:

<iframe name="output" id="output"></iframe>

and try to print from it:

window.frames["output"].focus();
window.frames["output"].print();

But how you can see in this example:

http://jsfiddle.net/FEvq6/78/

It prints a blank site.

John Smith
  • 6,105
  • 16
  • 58
  • 109
  • 1
    I thought I could add a setTimeout but Chrome gives me `Uncaught SecurityError: Blocked a frame with origin "http://fiddle.jshell.net" from accessing a frame with origin "null". The frame requesting access has a protocol of "http", the frame being accessed has a protocol of "data". Protocols must match.` – mplungjan Aug 20 '14 at 08:36
  • If you can add the pdf javascript to the file itself to print itself - http://stackoverflow.com/a/13992297/295783 – mplungjan Aug 20 '14 at 10:59

2 Answers2

2

It prints blank page because you're printing the iframe itself. Here is what you should do:

1. The iframe should contain EMBED tag which will load the PDF (for some reason stack overflow did not formatted code well, please see paste bin link below):

< embed src="path_to_script_which_generates.pdf" type="application/pdf" id="pdf"
width="100%" height="100%">
< /embed>

2. Then you should call the EMBED object to print the document. But since it may require some time for it to load you would need a timeout:

var doPrinting = (id){
    var pdfObject = document.getElementById(id);
    if (typeof(pdfObject.print) === 'undefined') {    
         setTimeout(function(){ doPrinting(id); }, 1000);
    } else {
        pdfObject.print();
    }
 }

 doPrinting('pdf');

Here is paste bin of the above: http://pastebin.com/s6qSTE8t

Htmlin.com
  • 121
  • 3
0
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<strike>funny</strike>.

    <iframe src="file_to_print.pdf" class="frameSet" type="application/pdf" runat="server" name="I5" scrolling="yes" width="100%" height="400">  </iframe>

</body>
</html>