VUE-PDF實(shí)現(xiàn)pdf在線(xiàn)預(yù)覽問(wèn)題
1、首先安裝vue-pdf
在命令行中輸入如下代碼:
npm install --save vue-pdf
2、頁(yè)面引用
新建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:{ //計(jì)算pdf頁(yè)碼總數(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有很多頁(yè)的時(shí)候
直接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:{ //計(jì)算pdf頁(yè)碼總數(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很大的時(shí)候
你會(huì)發(fā)現(xiàn)PDF加載回很慢,并且偶爾會(huì)跳出加載;
這時(shí)就用到了下邊的代碼;PDF分頁(yè)展示;
并且解決PDF預(yù)覽的時(shí)候偶爾中文會(huì)亂碼,借用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">上一頁(yè)</span> <span>{{currentPage}}/{{numPages}}</span> <span @click.stop="nextPage">下一頁(yè)</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, //分頁(yè) currentPage : 1, //當(dāng)前顯示頁(yè)數(shù) } }, watch : { src : { deep : true, immediate: true, handler(val){ if(val){ this.getPDFnums(val) } } } }, components: { pdf }, methods:{ //計(jì)算pdf頁(yè)碼總數(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; }) }, // 上一頁(yè) prePage() { var page = this.currentPage page = page > 1 ? page - 1 : this.numPages this.currentPage = page }, // 下一頁(yè) nextPage() { var page = this.currentPage page = page < this.numPages ? page + 1 : 1 this.currentPage = page }, // 回調(diào) loadPdfHandler(e) { this.currentPage = e } } } </script>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vuex state中同步數(shù)據(jù)和異步數(shù)據(jù)方式
這篇文章主要介紹了Vuex state中同步數(shù)據(jù)和異步數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08vue 實(shí)現(xiàn)在同一界面實(shí)現(xiàn)組件的動(dòng)態(tài)添加和刪除功能
這篇文章主要介紹了vue 實(shí)現(xiàn)在同一界面實(shí)現(xiàn)組件的動(dòng)態(tài)添加和刪除,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06vue實(shí)現(xiàn)點(diǎn)擊追加選中樣式效果
今天小編就為大家分享一篇vue實(shí)現(xiàn)點(diǎn)擊追加選中樣式效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11vue3點(diǎn)擊出現(xiàn)彈窗后背景變暗且不可操作的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue3點(diǎn)擊出現(xiàn)彈窗后背景變暗且不可操作的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08解決vue3項(xiàng)目打包后部署后某些靜態(tài)資源圖片不加載問(wèn)題
這篇文章主要給大家介紹了如何解決vue3項(xiàng)目打包后部署后某些靜態(tài)資源圖片不加載問(wèn)題,文中通過(guò)圖文結(jié)合的方式講解的非常詳細(xì),有需要的朋友可以參考下2024-05-05vue3 reactive 請(qǐng)求接口數(shù)據(jù)賦值后拿不到的問(wèn)題及解決方案
這篇文章主要介紹了vue3 reactive 請(qǐng)求接口數(shù)據(jù)賦值后拿不到的問(wèn)題及解決方案,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04Vue3?KeepAlive實(shí)現(xiàn)原理解析
KeepAlive?是一個(gè)內(nèi)置組件,那封裝一個(gè)組件對(duì)于大家來(lái)說(shuō)應(yīng)該不會(huì)有太大的困難,它的核心邏輯在于它的?render?函數(shù),它用?map?去記錄要緩存的組件,就是?[key,vnode]?的形式,這篇文章主要介紹了Vue3?KeepAlive實(shí)現(xiàn)原理,需要的朋友可以參考下2022-09-09Vue3組件不發(fā)生變化如何監(jiān)聽(tīng)pinia中數(shù)據(jù)變化
這篇文章主要給大家介紹了關(guān)于Vue3組件不發(fā)生變化如何監(jiān)聽(tīng)pinia中數(shù)據(jù)變化的相關(guān)資料,pinia是Vue的存儲(chǔ)庫(kù),它允許您跨組件/頁(yè)面共享狀態(tài),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11vue項(xiàng)目npm?run?build打包dist文件及打包后空白解決辦法
npm run build 這個(gè)命令會(huì)執(zhí)行Vue CLI中預(yù)定義的打包配置,并將打包后的文件存放在"dist"文件夾中,這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目npm?run?build打包dist文件及打包后空白的解決辦法,需要的朋友可以參考下2023-10-10