vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > VUE-PDF pdf在线预览

VUE-PDF实现pdf在线预览问题

作者:逩跑鍀小学生

这篇文章主要介绍了VUE-PDF实现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>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文