移动端vue2使用vue-pdf当前页面预览(分页优化)

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>

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值