You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 07:57:31