在VUE项目中集成PDF.js时,有时需要进行基于token的权限验证,也就是需要把浏览器本地存储的token相关信息加入到PDF.js请求的header中。

解决方法:

在PDF.js文件的根目录的build文件夹下,对PDF.js进行修改,具体如下:

// 定位代码位置,大概1136行
const params = Object.create(null);
let rangeTransport = null,
worker = null;

// 需要添加设置token的代码
let user = window.localStorage.getItem('user')?JSON.parse(window.localStorage.getItem('user')):{};
params['httpHeaders'] = {"token": user.token}