如何用vue-pdf包實(shí)現(xiàn)pdf文件預(yù)覽,支持分頁(yè)
vue項(xiàng)目實(shí)現(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>
//分頁(yè)
<div class="pageButton">
<el-button size="mini" @click="changePage(0)" round>上一頁(yè)</el-button>
<span> {{currentPage}} / {{pageCount}} </span>
<el-button size="mini" @click="changePage(1)" round>下一頁(yè)</el-button>
</div>
引入組件,定義變量
//vue文件內(nèi)導(dǎo)入并引用
import pdf from "vue-pdf";
export default {
components: {
pdf,
},
//定義變量
data(){
return {
pdfFile: "", //pdf文件地址
currentPage: 0, // 頁(yè)碼
pageCount: 0, // 總頁(yè)數(shù)
}
},
methods:{
// 翻頁(yè)
changePage (val) {
if (val === 0 && this.currentPage > 1) {
this.currentPage --;
}
if (val === 1 && this.currentPage < this.pageCount) {
this.currentPage ++;
}
},
// pdf加載時(shí)
loadPdf () {
this.currentPage = 1 // 加載的時(shí)候先加載第一頁(yè)
},
}
}
vue-pdf實(shí)現(xiàn)pdf在線預(yù)覽
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)文章
element-ui中Table表格省市區(qū)合并單元格的方法實(shí)現(xiàn)
這篇文章主要介紹了element-ui中Table表格省市區(qū)合并單元格的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
vue前端實(shí)現(xiàn)表格數(shù)據(jù)增查改刪功能
增刪改查是我們寫項(xiàng)目百分之七十會(huì)遇到的代碼,下面這篇文章主要給大家介紹了關(guān)于vue前端實(shí)現(xiàn)表格數(shù)據(jù)增查改刪功能的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
Vue-drag-resize 拖拽縮放插件的使用(簡(jiǎn)單示例)
本文通過代碼給大家介紹了Vue-drag-resize 拖拽縮放插件使用簡(jiǎn)單示例,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
vue3中使用Apache?ECharts的詳細(xì)方法
最近在做一些數(shù)據(jù)透析的項(xiàng)目需要用到報(bào)表圖,那么報(bào)表圖好用的有老牌的ECharts,比較新意的AntV,思前馬后的想了一下還是用了Echarts,這篇文章主要介紹了vue3中使用Apache?ECharts,需要的朋友可以參考下2022-11-11
vue實(shí)現(xiàn)動(dòng)態(tài)路由的方法及路由原理解析
這篇文章主要介紹了路由原理及vue實(shí)現(xiàn)動(dòng)態(tài)路由,Vue Router 提供了豐富的 API,可以輕松地實(shí)現(xiàn)路由功能,并支持路由參數(shù)、查詢參數(shù)、命名路由、嵌套路由等功能,可以滿足不同應(yīng)用程序的需求,需要的朋友可以參考下2023-06-06
vue3動(dòng)態(tài)修改打包后的請(qǐng)求路徑的操作代碼
這篇文章主要介紹了vue3動(dòng)態(tài)修改打包后的請(qǐng)求路徑,需要我們創(chuàng)建一個(gè)靜態(tài)資源里的外部文件來實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
Django+Vue.js搭建前后端分離項(xiàng)目的示例
本篇文章主要介紹了Django+Vue.js搭建前后端分離項(xiàng)目的示例,具有一定參考價(jià)值,有興趣的可以了解一下2017-08-08

