vue + pdf.js 实现PDF在线预览【后端返回文档流】

直接上代码:
当然,你要先下载 pdf.js ,直接百度搜索,下载之后减压,解压的文件重新命名为 pdf,将该文件夹直接放在根目录 public 下就OK,

<iframe v-if="isFull" :src="url" frameborder="0" style="width: 100%; height: 100%"></iframe>
// pdf文件预览,接口的配置文件

export function getStrData (parameter) {
  return request({
    url: '/report/pdf/getPdf',
    method: 'post',
    data: parameter,
    responseType: 'blob' //重要,一定要加
  })
}
    // 获取 url 文件,这里的写法:'/pdf/web/viewer.html?file=',因为是 plblic ,所以路径不用写public,写上是会报错的,找不到路径。
    getStrData(this.pdfQueryParam).then((res) => {
      console.log(res)
      this.url = '/pdf/web/viewer.html?file=' + encodeURIComponent(this.getObjectURL(res))
    }),
    // 获取 pdf 文件地址 
    getObjectURL(file) {
      var binaryData = [];
      binaryData.push(file);
      let url = null;
      if (window.createObjectURL !== undefined) { // basic
        url = window.createObjectURL(new Blob(binaryData, { type: 'application/pdf' }));
      } else if (window.webkitURL !== undefined) { // webkit or chrome
        try {
          url = window.webkitURL.createObjectURL(new Blob(binaryData, { type: 'application/pdf' }));
        } catch (error) {

        }
      } else if (window.URL !== undefined) { // mozilla(firefox)
        try {
          url = window.URL.createObjectURL(new Blob(binaryData, { type: 'application/pdf' }));
        } catch (error) {

        }
      }
      return url;
    },

后端返回的数据结构是:

Blob {size: 375808, type: "application/pdf"}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值