Vue PDF文件预览vue-pdf

2021-11-18  本文已影响0人  zZ_d205

git地址:https://github.com/FranckFreiburger/vue-pdf#readme
借鉴博客:https://www.cnblogs.com/steamed-twisted-roll/p/9648255.html
借鉴博客:https://blog.csdn.net/wgPython/article/details/108463993
1、下载

npm install --save vue-pdf

2、引用

import pdf from 'vue-pdf'
components: {
    pdf
  },
  <pdf :src="src" />

pdf文件显示全部页码
参考博客:https://blog.csdn.net/wgPython/article/details/108463993

  <pdf
          v-for="i in pageCount"
          :key="i"
          :src="src"
          :page="i"
        />


  data() {
    return {
      src: '',
      pageCount: 0 // pdf文件总页数
    }
  },


  created() {
    this.src = this.imgView('media/deliverable/49/7aac7cd4393546d68ea56d4e755c429b.pdf')

    // 有时PDF文件地址会出现跨域的情况,这里最好处理一下
    this.src = pdf.createLoadingTask(this.src)
    this.src.promise.then(pdf => {
      this.pageCount = pdf.numPages
    })
  },

分页处理
参考博客:https://www.cnblogs.com/steamed-twisted-roll/p/9648255.html

<template>
  <div class="pdf" v-show="fileType === 'pdf'">
    <p class="arrow">
    // 上一页
    <span @click="changePdfPage(0)" class="turn" :class="{grey: currentPage==1}">Preview</span>
    {{currentPage}} / {{pageCount}}
    // 下一页
    <span @click="changePdfPage(1)" class="turn" :class="{grey: currentPage==pageCount}">Next</span>
    </p>
    // 自己引入就可以使用,这里我的需求是做了分页功能,如果不需要分页功能,只要src就可以了
    <pdf
      :src="src" // src需要展示的PDF地址
      :page="currentPage" // 当前展示的PDF页码
      @num-pages="pageCount=$event" // PDF文件总页码
      @page-loaded="currentPage=$event" // 一开始加载的页面
      @loaded="loadPdfHandler"> // 加载事件
    </pdf>
  </div>
</template>
import pdf from 'vue-pdf'
  export default {
    components: {pdf},
    data () {
      return {
        currentPage: 0, // pdf文件页码
        pageCount: 0, // pdf文件总页数
        fileType: 'pdf', // 文件类型     src: '', // pdf文件地址
      }
    },  created: {    // 有时PDF文件地址会出现跨域的情况,这里最好处理一下    this.src = pdf.createLoadingTask(this.src)
  }
    method: {
      // 改变PDF页码,val传过来区分上一页下一页的值,0上一页,1下一页
      changePdfPage (val) {
        // console.log(val)
        if (val === 0 && this.currentPage > 1) {
          this.currentPage--
          // console.log(this.currentPage)
        }
        if (val === 1 && this.currentPage < this.pageCount) {
          this.currentPage++
          // console.log(this.currentPage)
        }
      },

      // pdf加载时
      loadPdfHandler (e) {
        this.currentPage = 1 // 加载的时候先加载第一页
      }

    }
  }

</script>
查看发票文字不显示   字体文件缺失


this.pdfUrl = pdf.createLoadingTask({
        url: row.img[0],
        // cMapUrl: "https://cdn.jsdelivr.net/npm/pdfjs-dist@2.6.347/cmaps/",
        cMapUrl: "https://saas-front-static-resources-test.oss-cn-beijing.aliyuncs.com/static-page/pdfjs-dist/cmaps/",
        cMapPacked: true
      })
      this.pdfUrl.promise.then(pdf => {
        this.loading = false
      })
上一篇 下一篇

猜你喜欢

热点阅读