如何使用 JavaScript 将 pdf 文件直接发送到打印机?
如何使用 JavaScript 将 PDF 文件直接发送到打印机?
How to send a PDF file directly to the printer using JavaScript?
我在论坛中找到了两个答案:
I found two answers in a forum:
<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.
But my problem is that it just works on IE, and doesnt work in Firefox or Chrome.
有什么解决办法吗?
我认为这个 JavaScript 库可能对您有所帮助:
I think this Library of JavaScript might Help you:
它叫做Print.js
It's called Print.js
首先包含
<script src="print.js"></script>
<link rel="stylesheet" type="text/css" href="print.css">
它的基本用法是调用printJS()
并传入一个PDF文档url:printJS('docs/PrintJS.pdf')
It's basic usage is to call printJS()
and just pass in a PDF document url: printJS('docs/PrintJS.pdf')
我所做的是这样的,如果PDF文档太大,这也会显示正在加载....".
What I did was something like this, this will also show "Loading...." if PDF document is too large.
<button type="button" onclick="printJS({printable:'docs/xx_large_printjs.pdf', type:'pdf', showModal:true})">
Print PDF with Message
</button>
但是请记住:
Firefox 目前不允许使用 iframe 打印 PDF 文档.Mozilla 的网站上有一个关于此的开放错误.使用 Firefox 时,Print.js 会在新标签页中打开 PDF 文件.
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.