繁体   English   中英

使用 javascript 静默打印 PDF 文件

[英]Silently print a PDF file using javascript

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

我有 PDF 作为从提取 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 应用程序中执行此操作吗?

试试这个print-js是 npm package

安装 npm package

npm install print-js --save

添加以下代码

import print from "print-js";

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

const handlePrint = (e) => {

    e.preventDefault();
    print(fileURL);

};

类似的问题在这里

暂无
暂无

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

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