vue移动端项目打开(查看)pdf(服务器可以外网访问)

一、首先需要把pdfjs文件夹放在后端服务器,前端需要做的事情非常简单

pdfjs在这里获取:链接:https://pan.baidu.com/s/1ZmRkcUCvjGSJjVD6GdSWcQ 提取码:ikdy 

或者去网站下载:http://mozilla.github.io/pdf.js/getting_started/#download

二、在html中写

<iframe :src="pdfSrc" frameborder="0" width="100%" height="100%"></iframe>

data:

data(){
  return {
    pdfSrc: '', // pdf文件在服务器的路径,例如:http://192.168.1.2:8080/file/pdf/32c1881d-0371-6e0d-aa5c-7bdf3f223300.pdf
    serverIp: '' // 服务器ip加端口,例如:http://192.168.1.2:8080/(这个是本地服务)
  }
},

三、把服务器地址拼上pdfjs中viewer.html的路径和pdf地址,赋给pdfSrc

this.pdfSrc = this.serverIp+'pdfjs/web/viewer.html?file='+this.$route.query.url

服务器地址和pdf生成地址都需要后端通过接口返回给你

 如果出现跨域报错:file origin does not match viewer’s,可以在viewer.js中搜索  file origin does not match viewer  把这几行代码注释掉:

//     if (fileOrigin !== viewerOrigin) {
//      throw new Error('file origin does not match viewer\'s');
//     }

 

posted @ 2020-04-14 09:07  万变不离许嵩  阅读(1837)  评论(0编辑  收藏  举报