使用javascript静默打印PDF文件

时间:2017-11-30 09:25:53

标签: javascript reactjs pdf printing blob

我想要构建的是通过单击按钮我想触发PDF文件的直接打印,但不打开或查看它。

我将PDF作为从fetch API返回的blob文件。

我尝试了很多例子,但不知道该怎么做

尝试了一些例子:

// In my case, I had only blobData from PDF, but you can ignore this and set fileURL directly if it is not yours.
const file = new Blob([blobData], { type: 'application/pdf' });
const fileURL = window.URL.createObjectURL(file);

// As the URL is dynamic, we must set src here, but if it's not, just leave it direct on the HTML. 
// Also, be careful with React literals. If you do this in a <iframe> defined in JSX, it won't work 
// as React will keep src property undefined.
window.frames["my-frame"].src = fileURL;

// Then print:
window.frames["my-frame"].print();
<iframe name="my-frame" id="my-frame" title="my-frame" style="display: none"></iframe>

还试过了库,Print.js:http://printjs.crabbly.com/

有没有方法可以打印pdf而无需用户直观地打开它? 我们应该只支持Chrome浏览器。

有人可以提供示例如何在React,Redux应用程序中执行此操作吗?

0 个答案:

没有答案