从iframe打印pdf

时间:2014-08-20 08:19:38

标签: javascript jquery iframe

我在iframe中动态创建pdf:

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

并尝试从中打印:

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

但是你可以在这个例子中看到:

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

打印一个空白网站。

2 个答案:

答案 0 :(得分:1)

它打印空白页面,因为您正在打印iframe本身。这是你应该做的:

1。 iframe应该包含将加载PDF的EMBED标记(由于某种原因,堆栈溢出没有很好地格式化代码,请参阅下面的粘贴bin链接):

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

2. 然后你应该调用EMBED对象来打印文档。但由于加载可能需要一些时间,因此需要超时:

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

 doPrinting('pdf');

以上是上面的粘贴框:http://pastebin.com/s6qSTE8t

答案 1 :(得分: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>
相关问题