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

 更新时间:2023年10月24日 10:08:40   作者:逩跑鍀小学生  
这篇文章主要介绍了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>

总结

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

相关文章

  • Vuex state中同步数据和异步数据方式

    Vuex state中同步数据和异步数据方式

    这篇文章主要介绍了Vuex state中同步数据和异步数据方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • vue 实现在同一界面实现组件的动态添加和删除功能

    vue 实现在同一界面实现组件的动态添加和删除功能

    这篇文章主要介绍了vue 实现在同一界面实现组件的动态添加和删除,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • vue实现点击追加选中样式效果

    vue实现点击追加选中样式效果

    今天小编就为大家分享一篇vue实现点击追加选中样式效果,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • vue3点击出现弹窗后背景变暗且不可操作的实现代码

    vue3点击出现弹窗后背景变暗且不可操作的实现代码

    这篇文章主要介绍了vue3点击出现弹窗后背景变暗且不可操作的实现代码,本文通过实例代码图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • Vue折叠面板组件的封装

    Vue折叠面板组件的封装

    这篇文章主要为大家详细介绍了Vue折叠面板组件的封装,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • 解决vue3项目打包后部署后某些静态资源图片不加载问题

    解决vue3项目打包后部署后某些静态资源图片不加载问题

    这篇文章主要给大家介绍了如何解决vue3项目打包后部署后某些静态资源图片不加载问题,文中通过图文结合的方式讲解的非常详细,有需要的朋友可以参考下
    2024-05-05
  • vue3 reactive 请求接口数据赋值后拿不到的问题及解决方案

    vue3 reactive 请求接口数据赋值后拿不到的问题及解决方案

    这篇文章主要介绍了vue3 reactive 请求接口数据赋值后拿不到的问题及解决方案,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2024-04-04
  • Vue3 KeepAlive实现原理解析

    Vue3 KeepAlive实现原理解析

    KeepAlive 是一个内置组件,那封装一个组件对于大家来说应该不会有太大的困难,它的核心逻辑在于它的 render 函数,它用 map 去记录要缓存的组件,就是 [key,vnode] 的形式,这篇文章主要介绍了Vue3 KeepAlive实现原理,需要的朋友可以参考下
    2022-09-09
  • Vue3组件不发生变化如何监听pinia中数据变化

    Vue3组件不发生变化如何监听pinia中数据变化

    这篇文章主要给大家介绍了关于Vue3组件不发生变化如何监听pinia中数据变化的相关资料,pinia是Vue的存储库,它允许您跨组件/页面共享状态,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • vue项目npm run build打包dist文件及打包后空白解决办法

    vue项目npm run build打包dist文件及打包后空白解决办法

    npm run build 这个命令会执行Vue CLI中预定义的打包配置,并将打包后的文件存放在"dist"文件夹中,这篇文章主要给大家介绍了关于vue项目npm run build打包dist文件及打包后空白的解决办法,需要的朋友可以参考下
    2023-10-10

最新评论