我希望通过点击按钮来触发直接打印PDF文件,但不需要打开或查看它。
我从fetch API中获取了PDF二进制大对象文件。
我尝试了很多示例,但不确定如何实现。
尝试过的一些示例:
// 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应用程序中如何实现的示例吗?