如何用vue-pdf包實現(xiàn)pdf文件預(yù)覽,支持分頁
vue項目實現(xiàn)pdf文件預(yù)覽功能
下載vue-pdf包
npm install --save vue-pdf
template模板內(nèi)容:
//pdf組件 <pdf :src="pdfFile" :page="currentPage" @num-pages="pageCount=$event" @page-loaded="currentPage=$event" @loaded="loadPdf"> </pdf> //分頁 <div class="pageButton"> <el-button size="mini" @click="changePage(0)" round>上一頁</el-button> <span> {{currentPage}} / {{pageCount}} </span> <el-button size="mini" @click="changePage(1)" round>下一頁</el-button> </div>
引入組件,定義變量
//vue文件內(nèi)導(dǎo)入并引用 import pdf from "vue-pdf"; export default { components: { pdf, }, //定義變量 data(){ return { pdfFile: "", //pdf文件地址 currentPage: 0, // 頁碼 pageCount: 0, // 總頁數(shù) } }, methods:{ // 翻頁 changePage (val) { if (val === 0 && this.currentPage > 1) { this.currentPage --; } if (val === 1 && this.currentPage < this.pageCount) { this.currentPage ++; } }, // pdf加載時 loadPdf () { this.currentPage = 1 // 加載的時候先加載第一頁 }, } }
vue-pdf實現(xiàn)pdf在線預(yù)覽
1、首先安裝vue-pdf ,在命令行中輸入如下代碼:
npm install --save vue-pdf
2、頁面引用,新建index.vue
<template> <div class="ins-submit-docs-content ins-submit-docs-pdf"> <div v-if="loading" style="position: absolute; top: 40%; width: 100%;text-align: center;"> <van-loading type="spinner" color="#fc8955" /> </div> <van-empty description="文檔加載失敗" v-if="loadingError" /> <pdf ref="morePDF" :src="src"></pdf> </div> </template>
<script> import Vue from 'vue'; import pdf from 'vue-pdf' import { Loading } from 'vant'; Vue.use(Loading); export default { name : 'ins-docs-pdf', props : { src : { type : String, //默認(rèn)值,選中值 default : '' } }, data(){ return { loading : true, //加載中 loadingError : false, //加載失敗 } }, watch : { src : { deep : true, immediate: true, handler(val){ if(val){ this.getPDFnums(val) } } } }, components: { pdf }, methods:{ //計算pdf頁碼總數(shù) getPDFnums(url) { this.loading = true //let loadURL = pdf.createLoadingTask(url) let loadURL = pdf.createLoadingTask({ url: url,//你的pdf地址 }) loadURL.promise.then(pdf => { this.$set(this, 'docsPDF.numPages', pdf.numPages) this.loading = false }).catch(err => { this.loading = false; this.loadingError = true; }) } } } </script>
3、當(dāng)PDF 有很多頁的時候,直接v-for 循環(huán),直接顯示完
<template> <div class="ins-submit-docs-content ins-submit-docs-pdf"> <div v-if="loading" style="position: absolute; top: 40%; width: 100%;text-align: center;"> <van-loading type="spinner" color="#fc8955" /> </div> <van-empty description="文檔加載失敗" v-if="loadingError" /> <pdf ref="morePDF" :src="src" :page="i" v-for="i in numPages" :key="i"></pdf> </div> </template>
<script> import Vue from 'vue'; import pdf from 'vue-pdf' import { Loading } from 'vant'; Vue.use(Loading); export default { name : 'ins-docs-pdf', props : { src : { type : String, //默認(rèn)值,選中值 default : '' } }, data(){ return { loading : true, //加載中 loadingError : false, //加載失敗 numPages : 0, } }, watch : { src : { deep : true, immediate: true, handler(val){ if(val){ this.getPDFnums(val) } } } }, components: { pdf }, methods:{ //計算pdf頁碼總數(shù) getPDFnums(url) { this.loading = true //let loadURL = pdf.createLoadingTask(url) let loadURL = pdf.createLoadingTask({ url: url,//你的pdf地址 }) loadURL.promise.then(pdf => { this.numPages = pdf.numPages this.$set(this, 'docsPDF.numPages', pdf.numPages) this.loading = false }).catch(err => { this.loading = false; this.loadingError = true; }) } } } </script>
4、當(dāng)PDF很大的時候,你會發(fā)現(xiàn)PDF加載回很慢,并且偶爾會跳出加載;
這時就用到了下邊的代碼;PDF分頁展示;
并且解決PDF預(yù)覽的時候偶爾中文會亂碼,借用VUE-PDF中CMapReaderFactory
<template> <div class="ins-submit-docs-content ins-submit-docs-pdf"> <div v-if="loading" style="position: absolute; top: 40%; width: 100%;text-align: center;"> <van-loading type="spinner" color="#fc8955" /> </div> <van-empty description="文檔加載失敗" v-if="loadingError" /> <div v-show="numPages <= 50"> <pdf ref="morePDF" :src="src" :page="i" v-for="i in numPages" :key="i"></pdf> </div> <div v-show="numPages > 50"> <pdf ref="PDF" :src="src" :page="currentPage" @num-pages="numPages=$event" @loaded="loadPdfHandler"></pdf> <div class="ins-pdf-button-box"> <span @click.stop="prePage">上一頁</span> <span>{{currentPage}}/{{numPages}}</span> <span @click.stop="nextPage">下一頁</span> </div> </div> </div> </template>
<script> import Vue from 'vue'; import pdf from 'vue-pdf' import CMapReaderFactory from 'vue-pdf/src/CMapReaderFactory.js'; import { Loading } from 'vant'; Vue.use(Loading); export default { name : 'ins-docs-pdf', props : { src : { type : String, //默認(rèn)值,選中值 default : '' } }, data(){ return { loading : true, //加載中 loadingError : false, //加載失敗 numPages : 0, //分頁 currentPage : 1, //當(dāng)前顯示頁數(shù) } }, watch : { src : { deep : true, immediate: true, handler(val){ if(val){ this.getPDFnums(val) } } } }, components: { pdf }, methods:{ //計算pdf頁碼總數(shù) getPDFnums(url) { this.loading = true //let loadURL = pdf.createLoadingTask(url) let loadURL = pdf.createLoadingTask({ url: url,//你的pdf地址 CMapReaderFactory }) loadURL.promise.then(pdf => { this.numPages = pdf.numPages this.currentPage = 1 this.$set(this, 'docsPDF.numPages', pdf.numPages) this.loading = false }).catch(err => { this.loading = false; this.loadingError = true; }) }, // 上一頁 prePage() { var page = this.currentPage page = page > 1 ? page - 1 : this.numPages this.currentPage = page }, // 下一頁 nextPage() { var page = this.currentPage page = page < this.numPages ? page + 1 : 1 this.currentPage = page }, // 回調(diào) loadPdfHandler(e) { this.currentPage = e } } } </script>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
element-ui中Table表格省市區(qū)合并單元格的方法實現(xiàn)
這篇文章主要介紹了element-ui中Table表格省市區(qū)合并單元格的方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vue前端實現(xiàn)表格數(shù)據(jù)增查改刪功能
增刪改查是我們寫項目百分之七十會遇到的代碼,下面這篇文章主要給大家介紹了關(guān)于vue前端實現(xiàn)表格數(shù)據(jù)增查改刪功能的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05Vue-drag-resize 拖拽縮放插件的使用(簡單示例)
本文通過代碼給大家介紹了Vue-drag-resize 拖拽縮放插件使用簡單示例,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12vue3中使用Apache?ECharts的詳細(xì)方法
最近在做一些數(shù)據(jù)透析的項目需要用到報表圖,那么報表圖好用的有老牌的ECharts,比較新意的AntV,思前馬后的想了一下還是用了Echarts,這篇文章主要介紹了vue3中使用Apache?ECharts,需要的朋友可以參考下2022-11-11vue實現(xiàn)動態(tài)路由的方法及路由原理解析
這篇文章主要介紹了路由原理及vue實現(xiàn)動態(tài)路由,Vue Router 提供了豐富的 API,可以輕松地實現(xiàn)路由功能,并支持路由參數(shù)、查詢參數(shù)、命名路由、嵌套路由等功能,可以滿足不同應(yīng)用程序的需求,需要的朋友可以參考下2023-06-06