简体   繁体   English

使用 javascript 静默打印 PDF 文件

[英]Silently print a PDF file using javascript

The thing I want to build is that by clicking a button I want to trigger the direct print of a PDF file, but without opening or viewing it.我想要构建的是,通过单击一个按钮,我想触发 PDF 文件的直接打印,但无需打开或查看它。

I have PDF as blob file returned from fetch API.我有 PDF 作为从提取 API 返回的 blob 文件。

I tried a lot of examples but don't know exactly how to do it我尝试了很多例子,但不知道具体怎么做

Some examples tried:尝试了一些例子:

 // 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>

Also tried library, Print.js: http://printjs.crabbly.com/ .还尝试了库 Print.js: http://printjs.crabbly.com/

Is there way to print the pdf without visually opening it to the user?有没有办法打印 pdf 而无需向用户视觉打开它? We should support only Chrome browser.我们应该只支持 Chrome 浏览器。

Can someone provide example how to do it in React, Redux application?有人可以提供示例如何在 React Redux 应用程序中执行此操作吗?

try this print-js and this is npm package试试这个print-js是 npm package

install npm package安装 npm package

npm install print-js --save

Add following code添加以下代码

import print from "print-js";

const fileURL = "someurl.com/document.pdf"; 

const handlePrint = (e) => {

    e.preventDefault();
    print(fileURL);

};

similar question is here类似的问题在这里

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

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