最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Print pdf from iframe - Stack Overflow

programmeradmin1浏览0评论

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>
发布评论

评论列表(0)

  1. 暂无评论