javascript - Print pdf from iframe - Stack Overflow

I dynamically create a pdf in a iframe:<iframe name="output" id="output"><

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:

/

It prints a blank site.

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/FEvq6/78/

It prints a blank site.

Share Improve this question asked Aug 20, 2014 at 8:19 John SmithJohn Smith 6,27919 gold badges61 silver badges113 bronze badges 2
  • 1 I thought I could add a setTimeout but Chrome gives me Uncaught SecurityError: Blocked a frame with origin "http://fiddle.jshell" 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 Commented Aug 20, 2014 at 8:36
  • If you can add the pdf javascript to the file itself to print itself - stackoverflow./a/13992297/295783 – mplungjan Commented Aug 20, 2014 at 10:59
Add a ment  | 

2 Answers 2

Reset to default 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./s6qSTE8t

<!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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745221815a4617282.html

相关推荐

  • javascript - Print pdf from iframe - Stack Overflow

    I dynamically create a pdf in a iframe:<iframe name="output" id="output"><

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信