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>

然后就可以了

相关推荐
岁忧5 小时前
GoLang五种字符串拼接方式详解
开发语言·爬虫·golang
tyatyatya5 小时前
MATLAB基础数据类型教程:数值型/字符型/逻辑型/结构体/元胞数组全解析
开发语言·matlab
心无旁骛~5 小时前
python多进程和多线程问题
开发语言·python
星云数灵5 小时前
使用Anaconda管理Python环境:安装与验证Pandas、NumPy、Matplotlib
开发语言·python·数据分析·pandas·教程·环境配置·anaconda
kaikaile19956 小时前
基于遗传算法的车辆路径问题(VRP)解决方案MATLAB实现
开发语言·人工智能·matlab
四问四不知6 小时前
Rust语言进阶(结构体)
开发语言·后端·rust
q***9946 小时前
index.php 和 php
开发语言·php
oioihoii6 小时前
C++网络编程:从Socket混乱到优雅Reactor的蜕变之路
开发语言·网络·c++
Aerelin6 小时前
爬虫playwright入门讲解
前端·javascript·html·playwright
笙年7 小时前
JavaScript Promise,包括构造函数、对象方法和类方法
开发语言·javascript·ecmascript