简体   繁体   English

Javascript .print() 使用 IE11 失败,用于类型 application/pdf

[英]Javascript .print() Failing with IE11 for type application/pdf

I've made a sample of the code I've been using on my site for a while now that is now giving issues when running from IE11 and Windows 8.1.我制作了一个我在我的网站上使用了一段时间的代码示例,现在在从 IE11 和 Windows 8.1 运行时出现问题。

The problem is that the Print Window does not pop up when the javascript method .print() is called anymore.问题是当 javascript 方法 .print() 不再被调用时,打印窗口不会弹出。

<html>
<head></head>

<body onload="window.document.getElementById('PDFDoc').print();">
    <object id='PDFDoc' name='PDFDoc' type='application/pdf' data='Hello World.pdf' height="369" width="266" style="border-width:thin;border:#CCCCCC" >
</object>
</body>
</html>

I found this article that talks about something similar but was not able to use it as a fix myself.我发现这篇文章讨论了类似的事情,但我自己无法将其用作修复程序。

PS: I would've uploaded the Hello World pdf but stackoverflow does not allow me to so just use any pdf to replicate the error. PS:我会上传Hello World pdf但stackoverflow不允许我所以只使用任何pdf来复制错误。

In Debug mode I get the error object doesn't support property or method 'print'在调试模式下,我得到错误object doesn't support property or method 'print'

在此处输入图片说明

I know that self.print() works but I'm not sure how to adapt it into this code to print the pdf and not the whole html page.我知道self.print()可以工作,但我不确定如何将其调整到此代码中以打印 pdf 而不是整个 html 页面。 Are there any other ways around this?有没有其他方法可以解决这个问题?

Correct me if I am wrong, but an <object> element does not seem to have a print() method.如果我错了,请纠正我,但<object>元素似乎没有print()方法。 The reason why it worked in eariler IE I guess is because they did not give too much about standards but nowadays they tend to get closer to them, thus they might have dropped their custom print method.我猜它在早期 IE 中工作的原因是因为他们没有给出太多关于标准的信息,但现在他们倾向于更接近他们,因此他们可能已经放弃了他们的自定义打印方法。

You have plenty of alternatives however:但是,您有很多选择:

  1. Hide everything that you don't want to print and call window.print()隐藏您不想打印的所有内容并调用 window.print()
  2. Create a print.css stylesheet创建一个 print.css 样式表
  3. You might be able to create an <iframe> element and append your element there, and call print on that iframe.您也许可以创建一个<iframe>元素并将您的元素附加到那里,然后在该 iframe 上调用 print 。

Also have a look at these discussions .也看看这些讨论

Although this is a quite old issue, here is an excellent solution which really works.虽然这是一个很老的问题,但这里有一个非常有效的优秀解决方案。 Tested with IE11 v11.1039.17763.0 (in IE10 mode), Google Chrome v80.0.3987.132, MS Edge v44.17763.831.0 and MS Edge (on Chromium, beta) v81.0.416.20, all under Win10.使用 IE11 v11.1039.17763.0(在 IE10 模式下)、Google Chrome v80.0.3987.132、MS Edge v44.17763.831.0 和 MS Edge(在 Chromium 上,测试版)v81.0.416.20 进行测试,均在 Win10 下。

This solution directly opens the print dialog of the browser with the given pdf file.此解决方案使用给定的 pdf 文件直接打开浏览器的打印对话框。

<object id="objectPdf" data="my.pdf" type="application/pdf" width="1" height="1">
  alt : <a href="my.pdf">my.pdf</a>
</object>

<iframe id="iFramePdf" src="my.pdf" style="display:none;"></iframe>

<input type="button" value="Print PDF" onclick="printTrigger();" />

<script type="text/javascript">
 function printTrigger() {
    try {
        document.getElementById('objectPdf').printWithDialog();
    } catch(e) {
        document.getElementById('iFramePdf').contentWindow.print();
    }
 }
</script>

Be aware, the provided solution has bad performance as it initiates three instead of just one HTTP requests!请注意,提供的解决方案性能不佳,因为它发起了三个而不是一个 HTTP 请求!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM