当前位置: 首页 > news >正文

vue项目中实现预览pdf

vue项目中实现预览pdf

1. iframe

<iframe :src="pdfSrc"></iframe>
​data() {return {pdfSrc: 'http://192.168.0.254:19000/trend/2023/12/27/5635529375174c7798b5fabc22cbec45.pdf',}},​iframe {width: 100%;height: calc(100vh - 132px - 2 * 20px - 160px);}
2. vue-pdf

npm i vue-pdf --save-dev

<!--* @Description: vue-pdf使用* @Author: mhf* @Date: 2023-12-28 11:37:30
-->
<template><div class="systemDescription"><div class="systemDescription-header">vue-pdf使用</div>
​<lineH style="margin: 20px 0"/>
​<div class="systemDescription-pdf"><pdfv-for="i in numPages":key="i":src="pdfSrc":page="i"></pdf></div></div>
</template>
​
<script>
import pdf from 'vue-pdf'
​
export default {name: 'systemDescription',components: { pdf },props: {},data() {return {pdfSrc:'http://192.168.0.254:19000/trend/2023/12/27/5635529375174c7798b5fabc22cbec45.pdf',numPages: undefined,}},methods: {},created() {},mounted() {let src = pdf.createLoadingTask(this.pdfSrc);src.promise.then(pdf => {this.numPages = pdf.numPages; // 解决vue-pdf默认只展示第一页的问题});}
}
</script>
​
<style lang="scss" scoped>
.systemDescription {padding: 50px 30px 40px;
​&-header {font-size: 26px;font-family: Source Han Sans CN;font-weight: 700;color: #333;text-align: center;}
​&-pdf {margin: 0 0 0 -24px;width: calc(100% + 50px);height: calc(100vh - 132px - 2 * 20px - 180px);overflow-y: auto;}
}
</style>

参考:解决vue-pdf默认只展示第一页的问题
使用vue-pdf展示静态PDF文件的时候(在线PDF可使用embed标签查看),常规操作之后发现只能加载第一页PDF,以下是解决方案:
​
vue-pdf使用过程如下:
​
$ yarn add vue-pdf
or
$ npm install vue-pdf
​
​
在组件中使用:
<template><pdf src=""></pdf>
</template>
<script>import pdf from 'vue-pdf'exprot default {component: {pdf}}
</script>
​
这个时候,多页的PDF只会显示第一页,这时各位可以去查看一下vue-pdf的源码,我们可以发现,它的实现过程是将PDF按页绘制在canvas上的,其页码数oage默认值是1,展示第一页的canvas。所以我们主要使用两种方式处理。
​
第一种是使用v-for循环加载所有页面:
<template><pdf src=""></pdf>
</template>
<script>import pdf from 'vue-pdf'exprot default {component: {pdf}}
</script>
这个时候,多页的PDF只会显示第一页,这时各位可以去查看一下vue-pdf的源码,我们可以发现,它的实现过程是将PDF按页绘制在canvas上的,其页码数oage默认值是1,展示第一页的canvas。所以我们主要使用两种方式处理。
​
第一种是使用v-for循环加载所有页面:
​
<template><div><pdfv-for="i in numPages":key="i":src="src":page="i"style="display: inline-block; width: 25%"></pdf></div>
</template><script>import pdf from 'vue-pdf'var loadingTask = pdf.createLoadingTask('https://cdn.mozilla.net/pdfjs/tracemonkey.pdf');export default {components: {pdf},data() {return {src: loadingTask,numPages: undefined,}},mounted() {this.src.promise.then(pdf => {this.numPages = pdf.numPages;});}
}</script>
这样有一个问题就是如果页数非常多,加载会很慢。
​
第二张是采用分页的形式:
<template><div class="onlineHelp cg-box"><div class="tools"><div class="page">第 {{pageNum}} /{{pageTotalNum}}页 </div><el-input v-model.number="goPageNum" style="width: 70px;margin-right: 8px"></el-input><el-button type="success" @click.stop="goPage"> 前往</el-button><el-button type="primary" @click.stop="prePage"> 上一页</el-button><el-button type="primary" @click.stop="nextPage"> 下一页</el-button></div><div class="pdf-box"><pdf ref="pdf":src="url":page="pageNum"@progress="loadedRatio = $event"@page-loaded="pageLoaded($event)"@num-pages="pageTotalNum=$event"@error="pdfError($event)"@link-clicked="page = $event"></pdf></div></div>
</template>
<script>
import pdf from 'vue-pdf'
export default {name: 'onlineHelp',components: {pdf},data() {return {url: `${process.env.VUE_APP_BASEURL}/help.pdf`,pageNum: 1,pageTotalNum: 1,// 加载进度loadedRatio: 0,curPageNum: 0,goPageNum: 1};},methods: {// 上一页函数,prePage() {var page = this.pageNumpage = page > 1 ? page - 1 : this.pageTotalNumthis.pageNum = page},// 下一页函数nextPage() {var page = this.pageNumpage = page < this.pageTotalNum ? page + 1 : 1this.pageNum = page},// 前往页数goPage() {if(!this.goPageNum || /\D/.test(this.goPageNum) || this.goPageNum < 1 || this.goPageNum > this.pageTotalNum) {this.$message.warning('输入页码有误')return}this.pageNum = this.goPageNum},// 页面加载回调函数,其中e为当前页数pageLoaded(e) {this.curPageNum = e},// 其他的一些回调函数。pdfError(error) {console.error(error)}}
};
</script>
​
<style lang="scss">
.onlineHelp {height: 100%;position: relative;display: flex;justify-content: center;.tools {position: absolute;top: 10px;right: 10px;z-index: 999;.page {display: inline-block;margin-right: 10px;}}.pdf-box {height: 100%;overflow: auto;width: 90%;}
}
</style>
​
​
二、
<template><div class="wrap"><pdf v-for="item in numPages" :key="item" :src="pdfSrc" :page="item"/></div>
</template>
<script>
import pdf from 'vue-pdf'
import CMapReaderFactory from 'vue-pdf/src/CMapReaderFactory.js'
export default {components:{pdf},data(){return{pdfUrl: "http://192.168.0.223:8080/pdf/预报.pdf",pdfSrc: "",numPages: "",}},mounted(){this.getTitlePdfurl();},methods:{getTitlePdfurl(){this.pdfSrc = pdf.createLoadingTask({ url: this.pdfUrl, CMapReaderFactory });//解决中文乱码问题this.pdfSrc.promise.then((pdf) => {this.numPages = pdf.numPages;})},}
}
</script>
<style lang="less" scoped>
.wrap{position: absolute;left: 0;right: 0;top: 0;bottom: 0;
}
</style>

http://www.lryc.cn/news/271906.html

相关文章:

  • 【Vulnhub 靶场】【Looz: 1】【简单】【20210802】
  • 计算机基础面试题 |03.精选计算机基础面试题
  • SQL最消耗性能查询错误用法示例
  • Python学习笔记(六)面向对象编程
  • CCNP课程实验-05-Comprehensive_Experiment
  • 第3课 使用FFmpeg获取并播放音频流
  • Java 动态树的实现思路分析
  • 太阳系三体模拟器
  • SQL常见面试题
  • 怎么获取客户端真实IP?GO
  • 山海鲸可视化软件的优势:数据整合、可视化与个性化定制
  • Mybatis行为配置之Ⅰ—缓存
  • 【Java开发岗面试】八股文—计算机网络
  • 【PythonRS】基于矢量范围批量下载遥感瓦片高清数据(天地图、高德、谷歌等)
  • 穷举vs暴搜vs深搜vs回溯vs剪枝
  • Sensor Demosaic IP 手册PG286笔记
  • HarmonyOS —— UIAbility 页面跳转总结
  • Spring Boot 3 集成 Jasypt详解
  • Spring Boot整合 EasyExcel 实现复杂 Excel 表格的导入与导出功能
  • SQLSERVER排查CPU占用高
  • uniapp:富文本回显
  • flink内存配置
  • easyexcel 导出
  • maven命令行安装依赖测试
  • Redis 笔记
  • 可穿戴智能设备应用领域以及使用意义分别有哪些?
  • 【Linux操作系统】探秘Linux奥秘:文件系统的管理与使用
  • 机器学习——主成分分析(PCA)
  • 论最近热门的AI绘画技术—从小白绘画到文创手账设计【文末送书-13】
  • python打开文件的方式比较