欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue使用vue-pdf實(shí)現(xiàn)PDF文件預(yù)覽

 更新時(shí)間:2023年03月15日 14:59:41   作者:會(huì)說(shuō)法語(yǔ)的豬  
這篇文章主要為大家詳細(xì)介紹了Vue如何使用vue-pdf實(shí)現(xiàn)PDF文件預(yù)覽的功能,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下

前言

vue-pdf npm文檔 https://www.npmjs.com/package/vue-pdf 

先看下效果 

安裝

npm install vue-pdf

頁(yè)面按需引入并注冊(cè)

import Pdf from "vue-pdf"
export default {
  components: { Pdf }
}

完整代碼

<template>
  <div class="global">
    <div class="preview">
      <el-button type="success" @click="isShow = true">開(kāi)始預(yù)覽</el-button>
    </div>
    <div class="pdfContainer" v-if="isShow">
      <div class="button">
        <el-button type="success" @click="prev()">上一頁(yè)</el-button>
        <el-button type="success" @click="next()">下一頁(yè)</el-button>
        <el-button type="success" @click="scaleD()">放大</el-button>
        <el-button type="success" @click="scaleX()">縮小</el-button>
        <el-button type="success" @click="clockwise()">順時(shí)針</el-button>
        <el-button type="success" @click="antiClockwise()">逆時(shí)針</el-button>
      </div>
      <div class="pdf">
        <pdf
          ref="pdf"
          :src="src"
          :page="currentPage"
          :rotate="pageRotate"
          @num-pages="pageCount = $event"
          @page-loaded="currentPage = $event"
          @loaded="loadPdfHandler"
        ></pdf>
      </div>
      <div class="page">
        <span class="pageNum">{{ currentPage }}</span>
        <span class="pageNum">/</span>
        <span class="pageNum">{{ pageCount }}</span>
      </div>
    </div>
  </div>
</template>
<script>
import Pdf from "vue-pdf";
export default {
  data() {
    return {
      src: "http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf",  // pdf的路徑 實(shí)際項(xiàng)目后端返回
      currentPage: 0, // pdf文件頁(yè)碼
      pageCount: 0, // pdf文件總頁(yè)數(shù)
      scale: 100,  //  開(kāi)始的時(shí)候默認(rèn)和容器一樣大即寬高都是100%
      pageRotate: 0,  // 旋轉(zhuǎn)角度
      isShow: false
    };
  },
  components: { Pdf },
  mounted() {
    console.log(Pdf, "Pdf----->>>");
  },
  methods: {
    // pdf加載時(shí)
    loadPdfHandler(e) {
      this.currentPage = 1; // 加載的時(shí)候先加載第一頁(yè)
      this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
      this.$refs.pdf.$el.style.height = parseInt(this.scale) + "%";
    },
    //  上一頁(yè)
    prev() {
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    },
    // 下一頁(yè)
    next() {
      if (this.currentPage < this.pageCount) {
        this.currentPage++;
      }
    },
    //放大
    scaleD() {
      if(this.scale == 100) return this.$message.warning('已經(jīng)是最大嘍~~')
      this.scale += 10;
      this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
      this.$refs.pdf.$el.style.height = parseInt(this.scale) + "%";
    },
    //縮小
    scaleX() {
      if(this.scale == 40) return this.$message.warning('已經(jīng)是最小嘍~~')
      this.scale += -10;
      this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
      this.$refs.pdf.$el.style.height = parseInt(this.scale) + "%";
    },
    // 順時(shí)針
    clockwise() {
      this.pageRotate += 90;
    },
    // 逆時(shí)針
    antiClockwise() {
      this.pageRotate -= 90;
    },
  },
};
</script>
<style scoped>
.global {
  width: 100%;
  height: 100%;
}
.preview {
  width: 100%;
  height: 50px;
}
 
.pdfContainer {
  width: 100%;
  height: calc(100% - 50px);
}
.pdfContainer .button {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
}
.pdfContainer .pdf {
  width: 100%;
  height: calc(100% - 100px);
  overflow-y: auto;
}
.pdfContainer .page {
  width: 100%;
  height: 50px;
}
 
.pageNum {
  font-size: 28px;
}
</style>

到此這篇關(guān)于Vue使用vue-pdf實(shí)現(xiàn)PDF文件預(yù)覽的文章就介紹到這了,更多相關(guān)Vue PDF文件預(yù)覽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論