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>

然后就可以了

相关推荐
枫叶林FYL12 分钟前
项目十:事件溯源仓储管理系统(WMS)仿真实现
开发语言·python
繁华落尽,倾城殇?28 分钟前
[C++11] : atomic,nullptr,default/delete,enum class
开发语言·c++·c++11·nullptr·atomic·enum class·default/delete
01_ice1 小时前
C语言数据在内存中的存储
c语言·开发语言
代码村新手1 小时前
C++-二叉搜索树
开发语言·c++
她说人狗殊途1 小时前
基于 vue-cli 创建
前端·javascript·vue.js
大家的林语冰2 小时前
Deno 2.8 正式发布,再次超越 Bun,史上最大的次版本升级诞生!
前端·javascript·node.js
吃好睡好便好3 小时前
创建魔方矩阵和单位矩阵
开发语言·人工智能·学习·线性代数·matlab·矩阵
影寂ldy3 小时前
C#数组的属性和方法(Clear / Copy / IndexOf )
开发语言·javascript·c#
i7i8i9com3 小时前
Hermes Agent 安装记录
开发语言·bash·hermes
Brave & Real3 小时前
小程序 const 在js中以及与同类的var和let之间的差异
javascript·微信小程序·小程序