簡體   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