如何使用 JavaScript 将 PDF 文件直接发送到打印机?
我在论坛上找到了两个答案:
<embed src="vehinvc.pdf" id = "Pdf1" name="Pdf1" hidden>
<a onClick="document.getElementById('Pdf1').printWithDialog()" style="cursor:hand;">Print file</a>
和
<OBJECT id = "Pdf2" name="Pdf2" CLASSID="clsid:CA8A9780-280D-11CF-A24D-444553540000" WIDTH="364" HEIGHT="290">
<PARAM NAME='SRC' VALUE="file.pdf">
</OBJECT>
<a onClick="document.Pdf2.printWithDialog()">Print file</a>
但我的问题是它只能在 IE 上运行,而不能在 Firefox 或 Chrome 上运行。
有什么解决办法吗?
最佳答案
我认为这个 JavaScript 库可以帮助您:
它叫做 Print.js
首先包含
<script src="print.js"></script>
<link rel="stylesheet" type="text/css" href="print.css">
它的基本用法是调用 printJS()
并传入一个 PDF 文档 url:printJS('docs/PrintJS.pdf')
我所做的是这样的,如果 PDF 文档太大,这也会显示“正在加载...”。
<button type="button" onclick="printJS({printable:'docs/xx_large_printjs.pdf', type:'pdf', showModal:true})">
Print PDF with Message
</button>
但是请记住:
Firefox currently doesn't allow printing PDF documents using iframes. There is an open bug in Mozilla's website about this. When using Firefox, Print.js will open the PDF file into a new tab.
关于javascript - 如何使用 JavaScript 将 pdf 文件直接发送到打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7074070/