Vue 实现pdf文档预览(vue-pdf插件)

步骤

1. 安装依赖

npm install --save vue-pdf

2. 在需要的页面,和定义组件一样那种 引入、注册,使用插件

import pdf from 'vue-pdf'

components:{
      pdf
},

export default {
    data(){
      return {
            numPages:0,
            pdfSrc:"",

        }
    }
}

3.页面里使用

<pdf 
    v-for="(item,index) in numPages" 
    :key="item" 
    :src="pdfSrc" 
    :page="item" 
    ref="pdf">
</pdf>

4.触发获取PDF的方法

getPdf(item) {
        this.pdfSrc = ""
        let p = {}
        axios({
          url:'',
          method:"post",
          params:p,
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
          responseType: 'blob',
        }).then(res=>{
          const blob = new Blob([res.data],{
            type:"application/pdf;charset=utf-8"
          })
          this.pdfSrc = window.URL.createObjectURL(blob)
          let loadignTask = pdf.createLoadingTask(this.pdfSrc)
          loadignTask.promise.then(pdf=>{
            this.numPages = pdf.numPages
          })
        }).catch(e => {
            this.$message({
              message:'查看预览失败',
              type:'error'
            })
        })
 },
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值