如何用vue-pdf包实现pdf文件预览,支持分页
作者:幸福浅暧
这篇文章主要介绍了如何用vue-pdf包实现pdf文件预览,支持分页问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
vue项目实现pdf文件预览功能
下载vue-pdf包
npm install --save vue-pdf
template模板内容:
//pdf组件 <pdf :src="pdfFile" :page="currentPage" @num-pages="pageCount=$event" @page-loaded="currentPage=$event" @loaded="loadPdf"> </pdf> //分页 <div class="pageButton"> <el-button size="mini" @click="changePage(0)" round>上一页</el-button> <span> {{currentPage}} / {{pageCount}} </span> <el-button size="mini" @click="changePage(1)" round>下一页</el-button> </div>
引入组件,定义变量
//vue文件内导入并引用 import pdf from "vue-pdf"; export default { components: { pdf, }, //定义变量 data(){ return { pdfFile: "", //pdf文件地址 currentPage: 0, // 页码 pageCount: 0, // 总页数 } }, methods:{ // 翻页 changePage (val) { if (val === 0 && this.currentPage > 1) { this.currentPage --; } if (val === 1 && this.currentPage < this.pageCount) { this.currentPage ++; } }, // pdf加载时 loadPdf () { this.currentPage = 1 // 加载的时候先加载第一页 }, } }
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>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。