使用 javascript 静默打印 PDF 文件

use*_*990 5 javascript printing pdf blob reactjs

我想要构建的是,通过单击一个按钮,我想触发 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();
Run Code Online (Sandbox Code Playgroud)
<iframe name="my-frame" id="my-frame" title="my-frame" style="display: none"></iframe>
Run Code Online (Sandbox Code Playgroud)

还尝试了库 Print.js:http ://printjs.crabbly.com/ 。

有没有办法在不向用户直观地打开它的情况下打印 pdf?我们应该只支持 Chrome 浏览器。

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

Raj*_*wal 1

试试这个print-js这是npm 包

安装 npm 包

npm install print-js --save
Run Code Online (Sandbox Code Playgroud)

添加以下代码

import print from "print-js";

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

const handlePrint = (e) => {

    e.preventDefault();
    print(fileURL);

};
Run Code Online (Sandbox Code Playgroud)

类似的问题在这里