目前项目中,移动端和 PC端都有pdf预览的需求,个人建议PC端用vue-pdf,能很好地实现分页的效果,移动端建议用pdfh5.js,一次性加载,滑动查看,先记录下vue-pdf
1.安装:npm install --save vue-pdf
2.引入:
import pdf from 'vue-pdf' components: {pdf}3.写template
<div class="pdf" v-show="fileType === 'pdf'"> <p class="arrow"> // 上一页 <span @click="changePdfPage(0)" class="turn" :class="{grey: currentPage==1}">Preview</span> {{currentPage}} / {{pageCount}} // 下一页 <span @click="changePdfPage(1)" class="turn" :class="{grey: currentPage==pageCount}">Next</span> </p> // 引入就可以使用,这里我的需求是做了分页功能,如果不需要分页功能,只要src就可以了 <pdf :src="src" // src需要展示的PDF地址 :page="currentPage" // 当前展示的PDF页码 @num-pages="pageCount=$event" // PDF文件总页码 @page-loaded="currentPage=$event" // 一开始加载的页面 @loaded="loadPdfHandler"> // 加载事件 </pdf> </div>4.绑定data
currentPage: 0, // pdf文件页码 pageCount: 0, // pdf文件总页数 fileType: 'pdf', // 文件类型 src: '', // pdf文件地址5.写加载方法,
从后台获取到pdfUrl后
this.src = pdf.createLoadingTask(data.wjdz) // pdf地址6.pdf加载和上一页,下一页切换的方法
// 改变PDF页码,val传过来区分上一页下一页的值,0上一页,1下一页 changePdfPage (val) { // console.log(val) if (val === 0 && this.currentPage > 1) { this.currentPage-- // console.log(this.currentPage) } if (val === 1 && this.currentPage < this.pageCount) { this.currentPage++ // console.log(this.currentPage) } }, // pdf加载时 loadPdfHandler (e) { this.currentPage = 1 // 加载的时候先加载第一页 }pdfh5.js预览见下一篇
