天天看點

vue中預覽PDF文檔

https://blog.csdn.net/qq_38543537/article/details/123715548

方法一:使用 vue-pdf 插件來實作

npm install --save vue-pdf
           
<template>
  <div class="scrollBox">
      <pdf v-for="item in numPages" :key="item" :src="pdfSrc" :page="item" ref="pdf"></pdf>
  </div>
</template>
           
<script>
import pdf from 'vue-pdf'
export default {
  components:{
      pdf
  },
  data(){
      return {
          pdfSrc:"http://image.cache.timepack.cn/nodejs.pdf",
          numPages: null, // pdf 總頁數
      }
  },
  mounted() {
      this.getNumPages()
  },
  methods: {
    # 計算pdf頁碼總數
    getNumPages() {
      let loadingTask = pdf.createLoadingTask(this.pdfSrc)
      loadingTask.promise.then(pdf => {
        this.numPages = pdf.numPages
      }).catch(err => {
        console.error('pdf 加載失敗', err);
      })
    },
  }
</script>
           
.scrollBox{
   overflow: auto;
   height: 600px;
 }
           

方法二:使用 iframe 方式實作

<!-- PDF預覽 -->
<el-dialog title="PDF預覽" :visible.sync="dialogVisible4" width="70%" append-to-body center>
     <iframe :src="pdfSrc" frameborder="0" style="width: 100%; height: 100%"></iframe>
</el-dialog>
           
data() {
    return {
        dialogVisible4: false,
        pdfSrc: '', // 将從背景擷取到的 PDF 檔案位址指派給這個字段
    }
}
           

效果圖:

vue中預覽PDF文檔