从iframe打印pdf [英] Print pdf from iframe

查看:461
本文介绍了从iframe打印pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iframe中动态创建pdf:

I dynamically create a pdf in a iframe:

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

并尝试从中打印:

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

但是在这个例子中你怎么看:

But how you can see in this example:

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

它打印一个空白网站。

推荐答案

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

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

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

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。然后你应该调用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');

这是上面的粘贴箱: http://pastebin.com/s6qSTE8t

这篇关于从iframe打印pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆