關(guān)于vue-pdf的基本使用方法

1、安裝依賴

npm install --save vue-pdf

2、html部分

<template>
  <div class="detail">
    <p class="arrow">
      <span 
        @click="changePdfPage(0)" 
        class="turn previewBtn" 
        :class="{grey: currentPage==1}"
      >上一頁</span>
      <span class="pageViews"
      >{{currentPage}} / {{pageCount }}</span>
      <span 
        @click="changePdfPage(1)" 
        class="turn nextBtn" 
        :class="{grey: currentPage==pageCount}"
        >下一頁</span>
    </p>
    <div class="pdf-box">
      <pdf 
        :src="pdffile"
        :page="currentPage"
        @num-pages="pageCount=$event"
        @page-loaded="currentPage=$event"
        @loaded="loadPdfHandler"
      ></pdf>
    </div>
  </div>
</template>

3、script部分

<script>
import pdf from "vue-pdf";
import CMapReaderFactory from 'vue-pdf/src/CMapReaderFactory.js';
export default {
  data() {
    return {
      currentPage: 0, // pdf文件頁碼
      pageCount: 0, // pdf文件總頁數(shù),
      flag: true,
    }
  },
  components: {
    pdf,
  },
  computed: {
    path() {
      return this.$route.query.path //path是pdf的地址,我是在上個(gè)頁面路由通過query傳過來的,具體根據(jù)自己的情況
    },
    pdffile() {
      return pdf.createLoadingTask({
        url: this.path,
        CMapReaderFactory //這里用來解決部分pdf預(yù)覽亂碼問題,主要是看不到漢字的問題
      })
    }
  },
  methods: {
    loadPdfHandler (e) {
      this.currentPage = 1 // 加載的時(shí)候先加載第一頁
    },
    changePdfPage (val) {
      if(!this.flag) return
      this.flag = false
      setTimeout(() => { //這里的定時(shí)器用來防止用戶頻繁點(diǎn)擊,我設(shè)置的0.5秒內(nèi)無論點(diǎn)擊多少次都只觸發(fā)一次
        this.flag = true
        if (val === 0 && this.currentPage > 1) {
          this.currentPage--
        }
        if (val === 1 && this.currentPage < this.pageCount) {
          this.currentPage++
        }
      }, 500)
    },
  }
}
</script>

4、css部分

<style lang="scss" scoped>
.detail {
  width: 100%;
  overflow-x: hidden;
  .arrow {
    display: flex;
    text-align: center;
    font-size: 20px;
    line-height: 50px;
    .previewBtn {
      flex: 1;
      justify-content: center;
      align-items: center;
      color: #5297f3;
    }
    .nextBtn {
      flex: 1;
      justify-content: center;
      color: #5297f3;
    }
    .pageViews {
      flex: 2;
      text-align: center;
      color: #5297f3;
    }
    .grey {
      color: #666;
    }
  }
}
</style>

vue-pdf 預(yù)覽亂碼問題,請(qǐng)點(diǎn)擊這里

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容