简体   繁体   English

JavaScript-如何手动打印PDF?

[英]JavaScript - How to print PDF manually?

In my application I have a pdf displayed in an <object> tag like this. 在我的应用程序中,这样在<object>标记中显示了一个pdf。 Below the pdf I have a print button. 在pdf下方,我有一个打印按钮。 How can I print the pdf when the button is clicked. 单击按钮时如何打印pdf。 ?

<object data="data:application/pdf;base64,JVBERi0xLjQNCiX15Pb8DQoNCjkgMCBvYmoNCjw8....
<button onclick="printPdf()">Print</button>

I tried using window.print, but it is printing the entire page and not the pdf. 我尝试使用window.print,但它正在打印整个页面,而不是pdf。

How can I print the pdf when the button is clicked. 单击按钮时如何打印pdf。 ?

You can't... at least not when you are displaying the PDF using the object tag. 使用对象标签显示PDF时,您不能……至少不能。

When you use the object tag, you are relying on the OS level pdf viewer or the viewer built into the browser to display the PDF. 使用object标签时,您依赖于操作系统级别的pdf查看器或浏览器中内置的查看器来显示PDF。 Different viewers and different browsers have different APIs... or none at all. 不同的查看器和不同的浏览器具有不同的API……或根本没有。 There is no consistent method to trigger a print that will work across the various OS/Browser/Device combinations that are available. 没有一致的方法来触发可在各种可用的OS /浏览器/设备组合中使用的打印。

You might look into this though. 您可能会对此进行调查。 http://printjs.crabbly.com/ http://printjs.crabbly.com/

It claims it can print PDF without a viewer. 它声称它可以在没有查看器的情况下打印PDF。 You'd still be able to display the PDF using the object tag but have the print button elsewhere on the page. 您仍然可以使用object标签显示PDF,但在页面其他位置具有打印按钮。

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

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