1.初始使用iframe总觉得很丑,于是进行改进PDF预览方式,这是使用了vue-pdf的预览
2.此代码是直接一次性展示全部文件,文件内容多时,存在很长时间的加载空白问题
### 模板
<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>
<!-- <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;
},
//页面加载完
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>