vue移动端pdf预览

2,878 阅读1分钟

一、 安装

npm install --save vue-pdf

二、局部引入

import pdf from 'vue-pdf'

三、模版代码

<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="pdfSrc"
      :page="currentPage"
      @num-pages="pageCount=$event"
      @page-loaded="currentPage=$event"
      @loaded="loadPdfHandler" >
    </pdf>
  </div>
</template>

四、js代码

<script>
import pdf from 'vue-pdf'
  export default {
    metaInfo: {
      title: 'This is the test',
      meta: [
        { charset: 'utf-8' },
        { name: 'viewport', content: 'width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=2,user-scalable=yes' }
      ]
    },
        components: {pdf},
        // props: ['pdfSrc'],
    data () {
      return {
        currentPage: 0, // pdf文件页码
        pageCount: 0, // pdf文件总页数
        fileType: 'pdf', // 文件类型
&emsp;&emsp;   &emsp; pdfSrc: '../../static/xy.pdf' // pdf文件地址
      }
    },
&emsp;&emsp;created() {
&emsp;&emsp;&emsp;// 有时PDF文件地址会出现跨域的情况,这里最好处理一下
&emsp;&emsp;&emsp; this.pdfSrc = pdf.createLoadingTask(this.pdfSrc)
    },
    methods: {
      // 改变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>

五、问题

以上方法只适用于无签章的的pdf,百度了很多,都说注释调下面这几行代码就可以显示,但我的始终未显示。

node_modules/pdfjs-dist/build/pdf.worker.js

if (data.fieldType === "Sig") {
  // data.fieldValue = null;
  // this.setFlags(_util.AnnotationFlag.HIDDEN);
}