使用react-pdf生成PDF后无法打印,控制台出现异常错误
React + Vite + TypeScript 中 react-pdf 结合 printJS 打印PDF异常问题
项目基础信息
- 技术栈:Vite + React + TypeScript
- 核心依赖:
react-pdf(生成适配热敏打印机的自定义尺寸收据PDF)、printJS(实现打印功能) - 现有组件:
- ThermalPrinterDialog:包含PDF预览区域和「Imprimir(打印)」按钮的弹窗组件
- ThermalPrinterPdf:基于
react-pdf的PDF内容生成组件
问题现象
PDF可正常生成并渲染至页面,但点击打印按钮触发打印逻辑时,控制台抛出异常,无法弹出浏览器打印窗口加载目标PDF。
打印逻辑代码
触发打印的handlePrint函数实现:
const handlePrint = async () => { const pdfBlob = await pdf( <ThermalPrinterPdf seller={seller} order={order} /> ).toBlob(); const pdfUrl = URL.createObjectURL(pdfBlob); console.log("result:", pdfUrl); printJS(pdfUrl); };
已尝试的解决方向
- 遵循
react-pdf文档指引,通过toBlob()将PDF转为Blob对象后生成URL传入printJS - 尝试将PDF转为base64格式传入
printJS,问题未解决
内容的提问来源于stack exchange,提问作者Athos Franco
相关产品推荐
相关产品推荐

