Vue 使用vue-pdf 显示pdf文件 切换页面 缩放 全屏 自动播放等

<template><div id="container"><!-- 上一页、下一页--><div class="right-btn"><div @click="toFullOrExit" class="turn-btn"><span>{{ isFull == 1 ? "取消全屏" : "全屏" }}</span></div><div @click="changePdfPage('first')" class="turn">首页</div><!-- 输入页码 --><div class="pageNum"><input v-model.number="currentPage" type="number" class="inputNumber" @input="inputEvent()"> / {{ pageCount}}</div><!-- 在按钮不符合条件时禁用 --><div @click="changePdfPage('pre')" class="turn-btn" :style="currentPage === 1 ? 'cursor: not-allowed;' : ''">上一页</div><div @click="changePdfPage('next')" class="turn-btn":style="currentPage === pageCount ? 'cursor: not-allowed;' : ''">下一页</div><div @click="changePdfPage('last')" class="turn-btn">尾页</div><div @click="scaleD" class="turn-btn">放大</div><div><input type="number" class="inputNumber" @input="ScaleDX" v-model="scale">%</div><div @click="scaleX" class="turn-btn">缩小</div><div @click="AutoPdfPage()" :class="AutoPage == true ? 'turn-autobtn' : 'turn-btn'">自动播放</div><div><input type="number" class="inputNumber" v-model="AutoTime">秒</div></div><div class="pdfArea"><pdf :src="src" ref="pdf" v-show="loadedRatio === 1" :page="currentPage" @num-pages="pageCount = $event"@progress="loadedRatio = $event" @page-loaded="currentPage = $event" @loaded="loadPdfHandler"@link-clicked="currentPage = $event" id="pdfID" style="margin: auto;"></pdf></div><!-- 加载未完成时,展示进度条组件并计算进度 --><div class="progress" v-show="loadedRatio !== 1"><el-progress type="circle" :width="70" color="#53a7ff":percentage="Math.floor(loadedRatio * 100)"></el-progress><br><!-- 加载提示语 --><span>{{ remindShow }}</span></div></div>
</template>
<script>
import pdf from 'vue-pdf'
export default {components: {pdf},computed: {},created() {this.prohibit()},destroyed() {// 在页面销毁时记得清空 setIntervalclearInterval(this.intervalID)},mounted() {// 更改 loading 文字this.intervalID = setInterval(() => {this.remindShow === this.remindText.refresh? this.remindShow = this.remindText.loading: this.remindShow = this.remindText.refresh}, 4000)this.ID = this.$route.query.ID;this.getDocumentData();// // 监听滚动条事件// this.listenerFunction()},data() {return {// ----- loading -----remindText: {loading: '加载文件中,文件较大请耐心等待...',refresh: '若卡住不动,可刷新页面重新加载...'},remindShow: '加载文件中,文件较大请耐心等待...',intervalID: '',// ----- vuepdf -----// src静态路径: /static/xxx.pdf /static/1.pdf// src服务器路径: 'http://.../xxx.pdf'src: "",// 当前页数currentPage: 0,// 总页数pageCount: 0,// 加载进度loadedRatio: 0,//是否自动播放AutoPage: false,timer: null,ID: "",AutoTime: 5,scale: 100,isFull: 0,}},methods: {async getDocumentData() {if (this.ID == null || this.ID == "") {this.$message.error("当前页面异常!");return;}let res = await this.http.get('/api/Document/GetDocumentData', {id: this.ID})if (!res.Status && res.Code == -1) {this.$message.error(res.Message);return;}if (res.Data && res.Data.Res) {this.fileData = res.Data.Res;this.src = this.fileData.PreViewPath;// this.src = pdf.createLoadingTask(this.src)// console.log(this.src);}},// // 监听滚动条事件// listenerFunction(e) {//     document.getElementById('container').addEventListener('scroll', true)// },// 页面回到顶部toTop() {document.getElementById('container').scrollTop = 0},// 输入页码时校验inputEvent() {if (this.currentPage > this.pageCount) {// 1. 大于maxthis.currentPage = this.pageCount} else if (this.currentPage < 1) {// 2. 小于minthis.currentPage = 1}},//放大scaleD() {this.scale += 5;this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";},//缩小scaleX() {if (this.scale == 0) {return;}this.scale += -5;this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";},ScaleDX() {this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";},// 切换页数changePdfPage(val) {if (val === 'pre' && this.currentPage > 1) {// 切换后页面回到顶部this.currentPage--this.toTop()} else if (val === 'next' && this.currentPage < this.pageCount) {this.currentPage++this.toTop()} else if (val === 'first') {this.currentPage = 1this.toTop()} else if (val === 'last' && this.currentPage < this.pageCount) {this.currentPage = this.pageCountthis.toTop()}},AutoPdfPage() {var AutoTime = this.AutoTime * 1000this.AutoPage = this.AutoPage == true ? false : true;if (this.AutoPage == true) {this.$message({type: "success",message: "自动播放启动 " + this.AutoTime + "S自动翻页",});this.timer = setInterval(() => {console.log(this.currentPage);if (this.currentPage == this.pageCount) {this.currentPage = 1;}else {this.currentPage++;}}, AutoTime)}else {this.$message({type: "success",message: "自动播放停止",});clearInterval(this.timer);}},// pdf加载时loadPdfHandler(e) {// 加载的时候先加载第一页this.currentPage = 1},//全屏requestFullScreen() {let de = document.documentElementif (de.requestFullscreen) {de.requestFullscreen()} else if (de.mozRequestFullScreen) {de.mozRequestFullScreen()} else if (de.webkitRequestFullScreen) {de.webkitRequestFullScreen()}},//退出全屏exitFullscreen() {let de = documentif (de.exitFullscreen) {de.exitFullscreen()} else if (de.mozCancelFullScreen) {de.mozCancelFullScreen()} else if (de.webkitCancelFullScreen) {de.webkitCancelFullScreen()}},//全屏切换toFullOrExit() {this.isFull = !this.isFullif (this.isFull) {this.requestFullScreen()} else {this.exitFullscreen()}},// // 禁用鼠标右击、F12 来禁止打印和打开调试工具// prohibit() {//     // console.log(document)//     document.oncontextmenu = function () {//         return false//     }//     document.onkeydown = function (e) {//         if (e.ctrlKey && (e.keyCode === 65 || e.keyCode === 67 || e.keyCode === 73 || e.keyCode === 74 || e.keyCode === 80 || e.keyCode === 83 || e.keyCode === 85 || e.keyCode === 86 || e.keyCode === 117)) {//             return false//         }//         if (e.keyCode === 18 || e.keyCode === 123) {//             return false//         }//     }// }}
}
</script><style scoped>
#container {overflow: auto;height: 100%;font-family: PingFang SC;width: 100%;display: flex;/* justify-content: center; */position: relative;
}/* 右侧功能按钮区 */
.right-btn {position: fixed;right: 5%;bottom: 15%;width: 120px;display: flex;flex-wrap: wrap;justify-content: center;z-index: 99;
}.pdfArea {width: 100%;
}/* ------------------- 输入页码 ------------------- */
.pageNum {margin: 10px 0;font-size: 18px;
}/*在谷歌下移除input[number]的上下箭头*/
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {-webkit-appearance: none !important;margin: 0;
}在firefox下移除input[number]的上下箭头 input[type='number'] {-moz-appearance: textfield;
}.inputNumber {border-radius: 8px;border: 1px solid #999999;height: 35px;font-size: 18px;width: 60px;text-align: center;
}.inputNumber:focus {border: 1px solid #00aeff;background-color: rgba(18, 163, 230, 0.096);outline: none;transition: 0.2s;
}/* ------------------- 切换页码 ------------------- */
.turn {background-color: #24aceb;opacity: 0.7;color: #ffffff;height: 70px;width: 70px;border-radius: 50%;display: flex;align-items: center;justify-content: center;margin: 5px 0;
}.turn-btn {background-color: #24aceb;opacity: 0.6;color: #ffffff;height: 70px;width: 70px;border-radius: 50%;margin: 5px 0;display: flex;align-items: center;justify-content: center;
}.turn-autobtn {background-color: #0467fa;opacity: 0.6;color: #ffffff;height: 70px;width: 70px;border-radius: 50%;margin: 5px 0;display: flex;align-items: center;justify-content: center;
}.turn-btn:hover,
.turn:hover {transition: 0.3s;opacity: 0.5;cursor: pointer;
}/* ------------------- 进度条 ------------------- */
.progress {position: absolute;right: 50%;top: 50%;text-align: center;
}.progress>span {color: #199edb;font-size: 14px;
}
</style>

中途发现如果PDF在前端文件夹 正常。后端服务器的文件会一直报跨域报错,并且我后台已经设置了跨域还是报。

最终解决是在前端代理里加入

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/111893.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue3 Ajax(axios)异步

文章目录 Vue3 Ajax(axios)异步1. 基础1.1 安装Ajax1.2 使用方法1.3 浏览器支持情况 2. GET方法2.1 参数传递2.2 实例 3. POST方法4. 执行多个并发请求5. axios API5.1 传递配置创建请求5.2 请求方法的别名5.3 并发5.4 创建实例5.5 实例方法5.6 请求配置项5.7 响应结构5.8 配置…

7.idea 使用 docker 构建 spring boot 项目

本文目录 step 1&#xff1a;编写 Dockerfile 文件step 2&#xff1a;pom.xml 中添加如下配置step 3&#xff1a;maven仓库 setting.xml <servers> 模块下&#xff0c;添加访问自定义仓库的用户名&#xff0c;密码step 4&#xff1a;使用 maven命令开始 clean、packagest…

2023年华为杯研究生数学建模竞赛辅导

2023年华为杯研究生数学建模竞赛辅导 各研究生培养单位&#xff1a; 中国研究生数学建模竞赛作为教育部学位管理与研究生教育司指导&#xff0c;中国学位与研究生教育学会、中国科协青少年科技中心主办的“中国研究生创新实践系列大赛”主题赛事之一&#xff0c;是一项面向在校…

kafka知识点汇总

kafka是什么&#xff1f; Kafka是一个分布式的基于发布/订阅模式的消息队列&#xff08;Message Queue&#xff09;&#xff0c;主要应用于大数据实时处理领域。 Kafka是由Apache软件基金会开发的一个开源流处理平台&#xff0c;由Scala和Java编写。Kafka是一种高吞吐量的分布…

K8S:pod资源限制及探针

文章目录 一.pod资源限制1.pod资源限制方式2.pod资源限制指定时指定的参数&#xff08;1&#xff09;request 资源&#xff08;2&#xff09; limit 资源&#xff08;3&#xff09;两种资源匹配方式 3.资源限制的示例&#xff08;1&#xff09;官网示例&#xff08;2&#xff0…

2023年最热门的编程语言:前进的趋势和机会

2023年最热门的编程语言&#xff1a;前进的趋势和机会 2023年最热门的编程语言&#xff1a;前进的趋势和机会摘要引言1. 编程语言的热门趋势1.1 新兴编程语言的崛起1.2 编程语言的可持续性发展1.3 跨平台编程语言的兴起1.4 人工智能和机器学习编程语言的需求 2. 编程语言职业机…

在PHP8中对数组进行排序-PHP8知识详解

在php8中&#xff0c;提供了丰富的排序函数&#xff0c;可以对数组进行排序操作。常见的排序函数如下几个&#xff1a;sort() 函数、rsort() 函数、asort() 函数、arsort() 函数、ksort() 函数、krsort() 函数、natsort()函数和natcascsort()函数。 1、sort() 函数&#xff1a;…

行业追踪,2023-09-18

自动复盘 2023-09-18 凡所有相&#xff0c;皆是虚妄。若见诸相非相&#xff0c;即见如来。 k 线图是最好的老师&#xff0c;每天持续发布板块的rps排名&#xff0c;追踪板块&#xff0c;板块来开仓&#xff0c;板块去清仓&#xff0c;丢弃自以为是的想法&#xff0c;板块去留让…

PHP8中获取并删除数组中最后一个元素-PHP8知识详解

在php8中&#xff0c;array_pop()函数将返回数组的最后一个元素&#xff0c;并且将该元素从数组中删除。语法格式如下&#xff1a; array_pop(目标数组) 获取并删除数组中最后一个元素&#xff0c;参考代码&#xff1a; <?php $stu array(s001>明明,s002>亮亮,s…

Vulnhub系列靶机---XXE Lab: 1

文章目录 信息收集主机发现端口扫描目录扫描 漏洞利用 靶机文档&#xff1a;XXE Lab: 1 下载地址&#xff1a;Download (Mirror) 告诉了利用点&#xff1a;http://your-ip/xxe 信息收集 主机发现 端口扫描 目录扫描 访问robots.txt 访问/xxe 随意输入账密&#xff0c;登陆抓包…

Ansible自动化:简化你的运维任务

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

【数据结构】堆的应用+TOP-K问题+二叉树遍历

欢迎来到我的&#xff1a;世界 希望作者的文章对你有所帮助&#xff0c;有不足的地方还请指正&#xff0c;大家一起学习交流 ! 目录 前言堆的时间复杂度向下调整算法的时间复杂度向上调整算法的时间复杂度 堆的应用堆排序TOP—K问题链式二叉树二叉树的节点&#xff1a;初始化节…