简体   繁体   English

用于打印编码为数据 URL 的 PDF 的跨浏览器解决方案

[英]Cross-browser solution for printing a PDF that's encoded as a data URL

On Firefox, const printWindow = window.open(pdfDataUrl) opens a new window with the PDF loaded and ready to print.在 Firefox 上, const printWindow = window.open(pdfDataUrl)打开一个新的 window,其中 ZBCD1B686177B59AD1 加载了 ZBCD1B686177B59AD1。 However following it with printWindow.print() fails to wait long enough for the PDF to be ready before printing, and printWindow.onload = printWindow.print never fires for some reason.然而,在它之后使用printWindow.print()未能等待足够长的时间让 PDF 在打印之前准备好,并且printWindow.onload = printWindow.print由于某种原因永远不会触发。

On Chrome, const printWindow = window.open(pdfDataUrl) doesn't work.在 Chrome 上, const printWindow = window.open(pdfDataUrl)不起作用。 Answers to other Stack Overflow questions recommend wrapping the data URL in an iframe.对其他 Stack Overflow 问题的回答建议将数据 URL 包装在 iframe 中。 That works, but then calling iframeElement.contentWindow.print() does nothing.这行得通,但随后调用iframeElement.contentWindow.print()什么也不做。 As a potential workaround, the user can right-click on the frame contents and click print.作为一种潜在的解决方法,用户可以右键单击框架内容并单击打印。

Is there a cross-browser solution to this issue?这个问题有跨浏览器的解决方案吗?

You can just execute this on load:您可以在加载时执行此操作:

window.onload => function(){
    setTimeout(print,1000);
}
function print(){
    window.print();
}

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

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