admin管理员组文章数量:1649156
1.继续上次的PDF预览优化,为解决文件过多一次性加载超长时间问题,做出分页效果,进行分页加载
### 模板
<template>
<div class="page">
<MyHeader pageTitle="PDF预览" :backPre="true" :downMenu="false"/>
<div class="content">
<Loading v-if="loading"></Loading>
<!-- pdf组件多页显示(一次性都出来) -->
<!-- <div class="pdf" v-for="i in numPages" :key="i">
<pdf ref="pdf" :src="src" :page="i"></pdf>
</div> -->
<pdf
ref="pdf"
:src="src"
:page="numPages"
@progress="loadedRatio=$event"
@num-pages="pageTotalNum=$event"
@error="pdfError($event)"
@loaded="loadHandler"
@page-loaded="pageLoaded($event)">
</pdf>
<div class="pdfBtn">
<vant-button
round
type="info"
size="small"
@click="prePage"
:class="showBtn ? 'active' : 'notActive'"
:disabled="isDisableLeft"
v-show="showBtnAgainLeft">
上一页
</van-button>
<vant-button
round
type="info"
size="small"
@click="nextPage"
:class="showBtn ? 'active' : 'notActive'"
:disabled="isDisableLeft"
v-show="showBtnAgainLeft">
下一页
</van-button>
</div>
<!-- <div class="control-box">
<button
:class="{select:idx=0}"
@touchstart="idx=0"
@touchend="idx=-1"
@click="scaleD">
放大
</button>
<button
:class="{select:idx==1}"
@touchstart="idx=1"
@touchend="idx=-1"
@click="scaleX">
缩小
</button>
</div> -->
</div>
</div>
</template>
### JS代码
<script>
import axios from "axios";
import vuePdf from "vue-pdf";
const pdf = {...vuePdf,destroyed:undefined}; //解决加载后页面空白问题
// import CMapReaderFactory from "vue-pdf/src/CMapReaderFactory.js";// 解决中文加载问题
export default{
name:"DownloadServiceManul",
data(){
return{
numPage:1,//当前页码数
url:"",//基础地址
path:"",//pdf动态路径,例: /testFile.pdf
src:"",
scale:100,//放大系数
idx:-1,
loading:true,
loadedRatio:0,// 加载进度
pageTotalNum:1,// 总页数
curPageNum:0,
showBtn:false,
isDisableLeft:true,
isDisableRight:false,
showBtnAgainLeft:false,
showBtnAgainRight:true,
};
},
props:{
propsData:[Object]
},
components:{
pdf
},
methods:{
//初始化
initPDF(){
const {fileId} = this.propsData;
//this.url="/cpic-health-ecs-eservice/api";
this.url = axios.defaults.baseURL;
this.path = `/downloadServiceManul?fileId=${fileId}&v=${Date.now()}`;
this.src = pdf.createLoadingTask({
url : this.url + this.path,//动态
cMapUrl : "/cmaps/",
cMapPacked : true
});
/* 直接展示全部pdf的使用代码(不是分页的)*/
/* this.src.promise.then(pdf => {
this.numPages = pdf.numPages;
this.loading = false;
})*/
//加载完PDF后对缓存进行清除
for(var key in require.cache){
if(key.indexOf("bcmap") >= 0){
delete require.cache[key];
}
}
},
//PDF渲染完函数
loadHandler(){
this.showBtn = true;
},
//上一页
prePage(){
if(this.pageTotalNum > 1){
this.isDisableRight = false;
this.showBtnAgainRight = true;
}
var page = this.numPages;
if(page <= 2){
this.isDisableLeft = true;
this.showBtnAgainLeft = false;
}
this.numPages = --page;
this.loading = false;
},
//下一页
nextPage(){
if(this.pageTotalNum >1){
this.isDisableRight = false;
this.showBtnAgainRight = true;
}
var page = this.numPages;
if(page >= this.pageTotalNum -1){
this.isDisableLeft = true;
this.showBtnAgainLeft = false;
}
this.numPages = ++page;
this.loading = false;
},
//页面加载完
pageLoaded(e){
this.curPageNum = e;
this.loading = false;
},
//PDF加载错误
pdfError(error){
console.log(error);
return Promise.reject(error);
},
//放大
/*scaleD(){
this.scale += 5;
const pdfDomList = this.$refs.pdf;
pdfDomList.forEach(pdf => {
pdf.$el.style.width = parseInt(this.scale) + "%";
})
},*/
//缩小
/*scaleX(){
if(this.scale == 100) return;
this.scale += -5;
const pdfDomList = this.$refs.pdf;
pdfDomList.forEach(pdf => {
pdf.$el.style.width = parseInt(this.scale) + "%";
});
}*/
},
mounted(){
this.initPDF();
}
}
</script>
### 样式代码
<style lang="less" scoped>
.content{
bottom:0;
.pdfBtn{
display: flex;
justify-content: space-around;
margin-top: 1rem;
}
.control-box{
position: fixed;
right: 20px;
bottom: 35px;
}
.active{
display: block;
}
.notActive{
display: none;
}
}
</style>
版权声明:本文标题:移动端vue2使用vue-pdf当前页面预览(分页优化) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1729505662a1203598.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论