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)文章
vue線上部署請求接口報錯net::ERR_CONNECTION_REFUSED
vue線上部署請求接口報錯net::ERR_CONNECTION_REFUSED問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
element el-table表格的二次封裝實現(xiàn)(附表格高度自適應(yīng))
這篇文章主要介紹了element el-table表格的二次封裝實現(xiàn)(附表格高度自適應(yīng)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
基于vue實現(xiàn)多功能樹形結(jié)構(gòu)組件的示例代碼
一個優(yōu)雅展示樹形結(jié)構(gòu)數(shù)據(jù)的 Vue 組件,遞歸渲染每個節(jié)點及其子節(jié)點,支持自定義顏色、文本和布局,通過獨特的樣式巧妙處理不同層級,為用戶打造豐富的視覺盛宴,文中通過代碼給大家介紹的非常詳細(xì),感興趣的同學(xué)可以自己動手嘗試一下2024-02-02
vue App.vue中的公共組件改變值觸發(fā)其他組件或.vue頁面監(jiān)聽
這篇文章主要介紹了vue App.vue里的公共組件改變值觸發(fā)其他組件或.vue頁面監(jiān)聽,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
應(yīng)用provide與inject刷新Vue頁面方法
這篇文章主要介紹了應(yīng)用provide與inject刷新Vue頁面的兩種方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,多多進(jìn)步,祝大家早日升職加薪2021-09-09

