vue-pdf 实现pdf在线预览;解决VUE-PDF预览的时候中文乱码

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,    //默认值,选中值
            default : ''
        }
    },
    data(){
        return {
            loading : true,    //加载中
            loadingError : false,    //加载失败
        }
    },
    watch : {
        src : {
            deep : true,
            immediate: true,
            handler(val){
                if(val){
                    this.getPDFnums(val)
                }
            }
        }
    },
    components: {
        pdf
    },
    methods:{
        //计算pdf页码总数
        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、当PDF 有很多页的时候,直接v-for 循环,直接显示完

<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,    //默认值,选中值
            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页码总数
        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、当PDF很大的时候,你会发现PDF加载回很慢,并且偶尔会跳出加载;这时就用到了下边的代码;PDF分页展示;并且解决PDF预览的时候偶尔中文会乱码,借用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,    //默认值,选中值
            default : ''
        }
    },
    data(){
        return {
            loading : true,    //加载中
            loadingError : false,    //加载失败
            numPages : 0,        //分页
            currentPage : 1, //当前显示页数
        }
    },
    watch : {
        src : {
            deep : true,
            immediate: true,
            handler(val){
                if(val){
                    this.getPDFnums(val)
                }
            }
        }
    },
    components: {
        pdf
    },
    methods:{
        //计算pdf页码总数
        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
        },

        // 回调
        loadPdfHandler(e) {
            this.currentPage = e
        }
    }
}
</script>

希望这些分享可以帮助到大家

谢谢各位看官的阅读~

 

posted @ 2021-12-27 17:24  逩跑得前端小学生  阅读(2385)  评论(0编辑  收藏  举报