Vue使用vue-pdf實現(xiàn)PDF文件預覽
更新時間:2023年03月15日 14:59:41 作者:會說法語的豬
這篇文章主要為大家詳細介紹了Vue如何使用vue-pdf實現(xiàn)PDF文件預覽的功能,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的可以了解一下
前言
vue-pdf npm文檔 https://www.npmjs.com/package/vue-pdf
先看下效果
安裝
npm install vue-pdf
頁面按需引入并注冊
import Pdf from "vue-pdf" export default { components: { Pdf } }
完整代碼
<template> <div class="global"> <div class="preview"> <el-button type="success" @click="isShow = true">開始預覽</el-button> </div> <div class="pdfContainer" v-if="isShow"> <div class="button"> <el-button type="success" @click="prev()">上一頁</el-button> <el-button type="success" @click="next()">下一頁</el-button> <el-button type="success" @click="scaleD()">放大</el-button> <el-button type="success" @click="scaleX()">縮小</el-button> <el-button type="success" @click="clockwise()">順時針</el-button> <el-button type="success" @click="antiClockwise()">逆時針</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的路徑 實際項目后端返回 currentPage: 0, // pdf文件頁碼 pageCount: 0, // pdf文件總頁數(shù) scale: 100, // 開始的時候默認和容器一樣大即寬高都是100% pageRotate: 0, // 旋轉角度 isShow: false }; }, components: { Pdf }, mounted() { console.log(Pdf, "Pdf----->>>"); }, methods: { // pdf加載時 loadPdfHandler(e) { this.currentPage = 1; // 加載的時候先加載第一頁 this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%"; this.$refs.pdf.$el.style.height = parseInt(this.scale) + "%"; }, // 上一頁 prev() { if (this.currentPage > 1) { this.currentPage--; } }, // 下一頁 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) + "%"; }, // 順時針 clockwise() { this.pageRotate += 90; }, // 逆時針 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>
到此這篇關于Vue使用vue-pdf實現(xiàn)PDF文件預覽的文章就介紹到這了,更多相關Vue PDF文件預覽內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue2.5.2使用http請求獲取靜態(tài)json數(shù)據(jù)的實例代碼
這篇文章主要介紹了vue2.5.2使用http請求獲取靜態(tài)json數(shù)據(jù)的實例代碼,需要的朋友可以參考下2018-02-02Vue官方推薦AJAX組件axios.js使用方法詳解與API
axios是Vue官方推薦AJAX組件,下面為大家介紹axios.js庫的詳細使用方法與API介紹2018-10-10