pdf.js 预览pdf的时候发票数据缺失显示不全:字体加载出错(缺失)导致部分缺失

首先,排除后端返回的PDF文件流是没有问题的:

但是在vue项目中是这样的:

明显是显示不全,F12查看报错信息,有以下警告:

pdf.js:2153 Warning: Error during font loading: The CMap "baseUrl" parameter must be specified, ensure that the "cMapUrl" and "cMapPacked" API parameters are provided.

翻译过来就是:
pdf.js:2153警告:字体加载过程中出错:必须指定CMap"base url"参数,请确保提供cMap url"和"cMapPacked"API参数。


原因:PDF文档中包含有pdf.js不支持的字体格式。


问题找到了,就开始找关键字:cMapUrl 把字体添加进来就好

先看成果:

具体操作:

1 下载

下载地址:https://mozilla.github.io/pdf.js/getting_started/

我用的版本:pdfjs-4.8.69-dist

2 放入项目

放在static文件夹下面或者public 下面:

找到以下代码并注释掉:

目的是屏蔽跨域错误,允许跨域

js 复制代码
try {
      const viewerOrigin = new URL(window.location.href).origin || "null";
      if (HOSTED_VIEWER_ORIGINS.includes(viewerOrigin)) {
        return;
      }
      const fileOrigin = new URL(file, window.location.href).origin;
      // 屏蔽跨域错误,允许跨域
      // if (fileOrigin !== viewerOrigin) {
      //   throw new Error("file origin does not match viewer's");
      // }
    } catch (ex) {
      PDFViewerApplication._documentError("pdfjs-loading-error", {
        message: ex.message
      });
      throw ex;
    }

3 使用

js 复制代码
 <iframe id="pdf_container" :src="`/pdf/web/viewer.html?file=${encodeURIComponent(pdfUrl)}`"></iframe>

然后就可以了

相关推荐
swordbob6 分钟前
Spring 单例 Bean 是线程安全的吗?
java·开发语言
学Linux的语莫13 分钟前
Vue 3 入门教程
前端·javascript·vue.js
怕浪猫40 分钟前
第一章、Chrome DevTools Protocol (CDP) 详解
前端·javascript·chrome
小小编程路1 小时前
C++ 异常 完整讲解
开发语言·c++
AI科技星1 小时前
数术工坊 · 第四卷 橡皮泥江湖(拓扑学)【完整定稿】
c语言·开发语言·汇编·electron·概率论·拓扑学
张忠琳2 小时前
【Go 1.26.4】Golang Select 深度解析
开发语言·后端·golang
沉默王二2 小时前
LlamaIndex 开源 LiteParse,零云依赖搞定扫描件PDF
pdf·开源
AC赳赳老秦3 小时前
OpenClaw+Power Apps 实战:自动生成 Power Apps 应用、连接 Excel 数据源
大数据·开发语言·python·serverless·excel·deepseek·openclaw
提笔了无痕3 小时前
如何用Go实现整套RAG流程
开发语言·后端·golang