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>

然后就可以了

相关推荐
JienDa5 小时前
JienDa聊PHP:小红书仿站实战深度架构全解析
开发语言·架构·php
执笔论英雄10 小时前
Slime异步原理(单例设计模式)4
开发语言·python·设计模式
e***749511 小时前
Modbus报文详解
服务器·开发语言·php
lly20240611 小时前
ASP 发送电子邮件详解
开发语言
小徐敲java11 小时前
python使用s7协议与plc进行数据通讯(HslCommunication模拟)
开发语言·python
likuolei11 小时前
XSL-FO 软件
java·开发语言·前端·数据库
6***379411 小时前
PHP在电商中的BigCommerce
开发语言·php
正一品程序员11 小时前
vue项目引入GoogleMap API进行网格区域圈选
前端·javascript·vue.js
Dev7z11 小时前
基于Matlab的多制式条形码识别与图形界面(GUI)系统设计与实现
开发语言·matlab
合作小小程序员小小店11 小时前
桌面开发,在线%信息管理%系统,基于vs2022,c#,winform,sql server数据。
开发语言·数据库·sql·microsoft·c#