Javascript 如果 src = pdf,如何从 IFRAME 打印 PDF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13656271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:35:47  来源:igfitidea点击:

How to print PDF from IFRAME if src = pdf?

javascriptpdfiframeprinting

提问by Asu

I have an iframe which needs point directly to a PDF file (not a page with a PDF):

我有一个 iframe,它需要直接指向 PDF 文件(不是带有 PDF 的页面):

<iframe id="ecard-pdf" name="ecard-pdf" style="position: absolute;" src="/profile.pdf">
</iframe>

I want to be able to print the PDF in this iFrame

我希望能够在此 iFrame 中打印 PDF

I have found several solutions in other questions that do not fit my needs:

我在其他不符合我需求的问题中找到了几个解决方案:

  1. Require to have a function in the iframe ( https://stackoverflow.com/a/473270/1246369)
  2. Suggest focusing the frame and then performing print action on it ( https://stackoverflow.com/a/9616706/1246369)
  3. Access contentWindow of the iframe and print it ( https://stackoverflow.com/a/9617566/1246369)
  4. Variations of those
  1. 要求在 iframe 中有一个函数( https://stackoverflow.com/a/473270/1246369
  2. 建议聚焦框架,然后对其执行打印操作( https://stackoverflow.com/a/9616706/1246369
  3. 访问 iframe 的 contentWindow 并打印它( https://stackoverflow.com/a/9617566/1246369
  4. 那些的变化

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.

但是,如果 iframe 的 src 直接指向 PDF 而不是环绕 PDF 的页面,则 FireFox 和 IE 似乎无法执行此操作。

Firefox:

火狐

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.

它没有打印,而是显示此对话框:“防止此页面创建其他对话框”,带有“确定”和“取消”按钮,这两个按钮都不会打印 PDF。

IE:

浏览器

just ignores my attempts to print using the above methods.

只是忽略了我使用上述方法打印的尝试。

Question:

问题

How can I allow users to print the PDF in the iFrame no matter what browser they are using?

无论用户使用什么浏览器,如何允许用户在 iFrame 中打印 PDF?

回答by Svela

I have struggled a bit to find a solution that works for both IE and Chrome. This works for me:

我一直在努力寻找适用于 IE 和 Chrome 的解决方案。这对我有用:

$(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);
});

...cheers.

...干杯。

回答by bdadam

As suggested in this answer for a similar question, you could do this:

正如this answer for a similar question中所建议的那样,您可以这样做:

window.frames.pdfFrame.print();

This should solve your problem.

这应该可以解决您的问题。

回答by Neutrino

This seems to be working:

这似乎有效:

   <style type="text/css" media="print">
   body *{display:none}
   iframe{display:block}
   </style>

So then just the pdf is displayed?

那么只显示pdf?

回答by Android334

Option 1:

选项1:

I haven't tested this but I found another answer here: https://stackoverflow.com/a/4096547/2528978

我还没有测试过,但我在这里找到了另一个答案:https: //stackoverflow.com/a/4096547/2528978

Assuming that you could use the following:

假设您可以使用以下内容:

<style type="text/css" media="print">
   body *{display:none}
   iframe{display:block}
</style>

So then just the pdf is displayed?

那么只显示pdf?

Option 2:

选项 2:

Make a hyperlink to the pdf file that says "Print me"

制作指向“打印我”的 pdf 文件的超链接

<a href='Path/To/PDF'>Print Me</a>

Hope this helps...

希望这可以帮助...

-Andrew

-安德鲁

回答by Imran khan

You can use directly window print option. use onclick option

您可以直接使用窗口打印选项。使用点击选项

onclick="javascript:window.print();"

onclick="javascript:window.print();"