Vue中实践使用PDFJS实现预览打印PDF文件

1、将PDFjs的依赖文件存放在public文件夹下的static文件夹下

静态文件地址

链接: https://pan.baidu.com/s/1UUHarsiNwhiL9v_LfS8x4Q 提取码: ohm1

2、书写组件

<template>
    <iframe  :src="pdfUrl" width="100%" :height="clHeight"></iframe>
</template>

<script>
import axios from 'axios'
import store from '@/store/'
export default {
  name: 'PDFDetail',
  data() {
    return {
      data: [],
      pdfUrl: '',
      showPDFModal: false,
      id: '',
      clHeight:'980px'
    }
  },
  mounted() {
    this.clHeight = document.body.clientHeight + 'px'
  },
  methods: {
    // 初始化获取pdf文件
    getPdfCode(alarmId) {
      let that = this;
      let urlBase = process.env.VUE_APP_API_BASE_URL
      let token = store.getters.token
      axios({
        method: 'get',
        url: urlBase + '/alarm/handle/export' + "?id=" + alarmId,
        headers: {
          'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
          'X-Access-Token': token
        },
        responseType: 'blob'  //设置响应的数据类型为一个包含二进制数据的 Blob 对象,必须设置!!!
      }).then(response => {
        that.pdfUrl = that.getObjectURL(response.data);
      }).catch(function (error) {
        console.log(error);
      });
    },
    // 将返回的流数据转换为url
    getObjectURL(file) {
      let url = null;
      if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
      } else if (window.webkitURL != undefined) { // webkit or chrome
        try {
          url = window.webkitURL.createObjectURL(file);
        } catch (error) {

        }
      } else if (window.URL != undefined) { // mozilla(firefox)
        try {
          url = window.URL.createObjectURL(file);
        } catch (error) {
        }
      }
      return url;
    },
  },
}
</script>

<style lang='less' scoped>
</style>

 3、将该组件注册为BlankLayout组件,在新的窗口打开。

4、需要自己根据实际情况修改部分代码即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王鹏飞的解忧屋

您的鼓励是我最大的创作来源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值