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>

然后就可以了

相关推荐
GISer_Jing7 小时前
Three.js着色器编译机制深度解析
javascript·webgl·着色器
丷丩8 小时前
MapLibre GL JS第22课:查看本地GeoJSON
前端·javascript·map·mapbox·maplibre gl js
AI玫瑰助手8 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车8 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋8 小时前
C++14特性
开发语言·c++·c++14特性
JAVA社区9 小时前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展
弥树子9 小时前
踩坑记录:服务器内网调用接口,真实请求URL与官方公开URL不一致问题排查
开发语言·php
z落落10 小时前
C# ToCharArray + foreach遍历 + String与StringBuilder
开发语言·c#
学代码的真由酱10 小时前
Java多用户一对一网页聊天室-测试报告
java·开发语言·功能测试·测试
人道领域10 小时前
【LeetCode刷题日记】669.修剪二叉搜索树
开发语言·python·算法