题
我有一个带有嵌入式PDF的网页。我的代码看起来像这样:
<embed
type="application/pdf"
src="path_to_pdf_document.pdf"
id="pdfDocument"
width="100%"
height="100%">
</embed>
我有此JavaScript代码以打印我的PDF:
function printDocument(documentId) {
//Wait until PDF is ready to print
if (typeof document.getElementById(documentId).print == 'undefined') {
setTimeout(function(){printDocument(documentId);}, 1000);
} else {
var x = document.getElementById(documentId);
x.print();
}
}
执行此代码时,Acrobat插件将打开众所周知的打印对话框。这样的事情:
两个问题:
- 如何改善检测PDF已加载并准备好打印的方法?
- 如何避免显示打印对话框?
有关我的系统的更多信息:
OS: Windows XP
浏览器: Internet Explorer 7
PDF插件: Acrobat Reader 9
解决方案
您将无法使用普通的旧JavaScript静静地打印。您希望打印机如何开始打印全黑的100000000页。不是一件好事。如果您想静静地打印并仅适用于Internet Explorer,则有ActiveX控件可以做到。这需要您的页面和用户真正信任您的网站的更高安全设置。
其他提示
This is possible in a trusted, Intranet environment.
<object id="pdfDoc" style="position:absolute;z-index:-1;" name="pdfDoc" classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="900px" height="100%">
<param name="SRC" value="yourdoc.pdf" />
</object>
<input type="button" ... onclick="pdfDoc.printAll();" />
This will bypass the print dialog and send directly to the default printer.
I wonder if you actually need to wait before printing -- won't the print job handle that for you? And I truly hope no modern browser will allow you (or any website for that matter) to print without that confirmation dialog (some old browsers used to do that, a long time ago).
You can do this in Firefox by changing about:config
. Add print.always_print_silent
and set it to true
.