如果src = pdf,如何从IFRAME打印PDF? [英] How to print PDF from IFRAME if src = pdf?
问题描述
我有一个iframe需要直接指向PDF文件(不是带PDF的页面):
I have an iframe which needs point directly to a PDF file (not a page with a PDF):
<iframe id="ecard-pdf" name="ecard-pdf" style="position: absolute;" src="/profile.pdf">
</iframe>
我希望能够在此iFrame中打印PDF
I want to be able to print the PDF in this iFrame
我在其他问题中找到了几个不符合我需求的解决方案:
I have found several solutions in other questions that do not fit my needs:
- 要求在iframe(
https://stackoverflow.com/a/473270/1246369 ) - 建议聚焦框架,然后对其执行打印操作(
https://stackoverflow.com/a / 9616706/1246369 ) - 访问iframe的contentWindow并将其打印出来(
https://stackoverflow.com/a/9617566/1246369 ) - 这些的变化
- Require to have a function in the iframe ( https://stackoverflow.com/a/473270/1246369 )
- Suggest focusing the frame and then performing print action on it ( https://stackoverflow.com/a/9616706/1246369 )
- Access contentWindow of the iframe and print it ( https://stackoverflow.com/a/9617566/1246369 )
- Variations of those
然而,如果iframe的src直接指向PDF而不是指向围绕PDF的页面,似乎FireFox和IE无法执行此操作。
However, it seems that FireFox and IE can't do this if the iframe's src points directly to a PDF and not to a page wrapped around the PDF.
Firefox :
而不是打印时,它会显示以下对话框:使用确定和取消按钮显示此页面防止此页面创建其他对话,这两个按钮都不打印PDF。
Instead of printing, it displays this dialog: "Prevent this page from creating additional dialogues" with "OK" and "Cancel" buttons, neither of which prints the PDF.
IE :
只是忽略了我使用上述方法进行打印的尝试。
just ignores my attempts to print using the above methods.
问题:
我怎样才能让用户在iFrame中打印PDF,无论他们使用什么浏览器?
How can I allow users to print the PDF in the iFrame no matter what browser they are using?
推荐答案
我一直在努力寻找适用于IE和Chrome的解决方案。这对我有用:
I have struggled a bit to find a solution that works for both IE and Chrome. This works for me:
$(function() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
var trident = ua.indexOf('Trident/');
var edge = ua.indexOf('Edge/');
var url = '/url/to/file.pdf';
var pdf ='';
var style = 'position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden;';
if(msie > 0 || trident > 0 || edge > 0){
pdf = '<object data="' + url + '" name="print_frame" id="print_frame" style="' + style + '" type="application/pdf">';
}
else{
pdf ='<iframe src="' + url + '" name="print_frame" id="print_frame" style="' + style + '"></iframe>';
}
$(document.body).append(pdf);
setTimeout(function(){
window.frames["print_frame"].focus();
window.frames["print_frame"].print();
},2000);
});
...干杯。
这篇关于如果src = pdf,如何从IFRAME打印PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!