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


    1. <template>
    2. <div id="container">
    3. <!-- 上一页、下一页-->
    4. <div class="right-btn">
    5. <div @click="toFullOrExit" class="turn-btn"><span>{{ isFull == 1 ? "取消全屏" : "全屏" }}</span></div>
    6. <div @click="changePdfPage('first')" class="turn">
    7. 首页
    8. </div>
    9. <!-- 输入页码 -->
    10. <div class="pageNum">
    11. <input v-model.number="currentPage" type="number" class="inputNumber" @input="inputEvent()"> / {{ pageCount
    12. }}
    13. </div>
    14. <!-- 在按钮不符合条件时禁用 -->
    15. <div @click="changePdfPage('pre')" class="turn-btn" :style="currentPage === 1 ? 'cursor: not-allowed;' : ''">
    16. 上一页
    17. </div>
    18. <div @click="changePdfPage('next')" class="turn-btn"
    19. :style="currentPage === pageCount ? 'cursor: not-allowed;' : ''">
    20. 下一页
    21. </div>
    22. <div @click="changePdfPage('last')" class="turn-btn">
    23. 尾页
    24. </div>
    25. <div @click="scaleD" class="turn-btn">
    26. 放大
    27. </div>
    28. <div><input type="number" class="inputNumber" @input="ScaleDX" v-model="scale">%</div>
    29. <div @click="scaleX" class="turn-btn">
    30. 缩小
    31. </div>
    32. <div @click="AutoPdfPage()" :class="AutoPage == true ? 'turn-autobtn' : 'turn-btn'">
    33. 自动播放
    34. </div>
    35. <div><input type="number" class="inputNumber" v-model="AutoTime"></div>
    36. </div>
    37. <div class="pdfArea">
    38. <pdf :src="src" ref="pdf" v-show="loadedRatio === 1" :page="currentPage" @num-pages="pageCount = $event"
    39. @progress="loadedRatio = $event" @page-loaded="currentPage = $event" @loaded="loadPdfHandler"
    40. @link-clicked="currentPage = $event" id="pdfID" style="margin: auto;"></pdf>
    41. </div>
    42. <!-- 加载未完成时,展示进度条组件并计算进度 -->
    43. <div class="progress" v-show="loadedRatio !== 1">
    44. <el-progress type="circle" :width="70" color="#53a7ff"
    45. :percentage="Math.floor(loadedRatio * 100)"></el-progress>
    46. <br>
    47. <!-- 加载提示语 -->
    48. <span>{{ remindShow }}</span>
    49. </div>
    50. </div>
    51. </template>
    52. <script>
    53. import pdf from 'vue-pdf'
    54. export default {
    55. components: {
    56. pdf
    57. },
    58. computed: {
    59. },
    60. created() {
    61. this.prohibit()
    62. },
    63. destroyed() {
    64. // 在页面销毁时记得清空 setInterval
    65. clearInterval(this.intervalID)
    66. },
    67. mounted() {
    68. // 更改 loading 文字
    69. this.intervalID = setInterval(() => {
    70. this.remindShow === this.remindText.refresh
    71. ? this.remindShow = this.remindText.loading
    72. : this.remindShow = this.remindText.refresh
    73. }, 4000)
    74. this.ID = this.$route.query.ID;
    75. this.getDocumentData();
    76. // // 监听滚动条事件
    77. // this.listenerFunction()
    78. },
    79. data() {
    80. return {
    81. // ----- loading -----
    82. remindText: {
    83. loading: '加载文件中,文件较大请耐心等待...',
    84. refresh: '若卡住不动,可刷新页面重新加载...'
    85. },
    86. remindShow: '加载文件中,文件较大请耐心等待...',
    87. intervalID: '',
    88. // ----- vuepdf -----
    89. // src静态路径: /static/xxx.pdf /static/1.pdf
    90. // src服务器路径: 'http://.../xxx.pdf'
    91. src: "",
    92. // 当前页数
    93. currentPage: 0,
    94. // 总页数
    95. pageCount: 0,
    96. // 加载进度
    97. loadedRatio: 0,
    98. //是否自动播放
    99. AutoPage: false,
    100. timer: null,
    101. ID: "",
    102. AutoTime: 5,
    103. scale: 100,
    104. isFull: 0,
    105. }
    106. },
    107. methods: {
    108. async getDocumentData() {
    109. if (this.ID == null || this.ID == "") {
    110. this.$message.error("当前页面异常!");
    111. return;
    112. }
    113. let res = await this.http.get('/api/Document/GetDocumentData', {
    114. id: this.ID
    115. })
    116. if (!res.Status && res.Code == -1) {
    117. this.$message.error(res.Message);
    118. return;
    119. }
    120. if (res.Data && res.Data.Res) {
    121. this.fileData = res.Data.Res;
    122. this.src = this.fileData.PreViewPath;
    123. // this.src = pdf.createLoadingTask(this.src)
    124. // console.log(this.src);
    125. }
    126. },
    127. // // 监听滚动条事件
    128. // listenerFunction(e) {
    129. // document.getElementById('container').addEventListener('scroll', true)
    130. // },
    131. // 页面回到顶部
    132. toTop() {
    133. document.getElementById('container').scrollTop = 0
    134. },
    135. // 输入页码时校验
    136. inputEvent() {
    137. if (this.currentPage > this.pageCount) {
    138. // 1. 大于max
    139. this.currentPage = this.pageCount
    140. } else if (this.currentPage < 1) {
    141. // 2. 小于min
    142. this.currentPage = 1
    143. }
    144. },
    145. //放大
    146. scaleD() {
    147. this.scale += 5;
    148. this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
    149. },
    150. //缩小
    151. scaleX() {
    152. if (this.scale == 0) {
    153. return;
    154. }
    155. this.scale += -5;
    156. this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
    157. },
    158. ScaleDX() {
    159. this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
    160. },
    161. // 切换页数
    162. changePdfPage(val) {
    163. if (val === 'pre' && this.currentPage > 1) {
    164. // 切换后页面回到顶部
    165. this.currentPage--
    166. this.toTop()
    167. } else if (val === 'next' && this.currentPage < this.pageCount) {
    168. this.currentPage++
    169. this.toTop()
    170. } else if (val === 'first') {
    171. this.currentPage = 1
    172. this.toTop()
    173. } else if (val === 'last' && this.currentPage < this.pageCount) {
    174. this.currentPage = this.pageCount
    175. this.toTop()
    176. }
    177. },
    178. AutoPdfPage() {
    179. var AutoTime = this.AutoTime * 1000
    180. this.AutoPage = this.AutoPage == true ? false : true;
    181. if (this.AutoPage == true) {
    182. this.$message({
    183. type: "success",
    184. message: "自动播放启动 " + this.AutoTime + "S自动翻页",
    185. });
    186. this.timer = setInterval(() => {
    187. console.log(this.currentPage);
    188. if (this.currentPage == this.pageCount) {
    189. this.currentPage = 1;
    190. }
    191. else {
    192. this.currentPage++;
    193. }
    194. }, AutoTime)
    195. }
    196. else {
    197. this.$message({
    198. type: "success",
    199. message: "自动播放停止",
    200. });
    201. clearInterval(this.timer);
    202. }
    203. },
    204. // pdf加载时
    205. loadPdfHandler(e) {
    206. // 加载的时候先加载第一页
    207. this.currentPage = 1
    208. },
    209. //全屏
    210. requestFullScreen() {
    211. let de = document.documentElement
    212. if (de.requestFullscreen) {
    213. de.requestFullscreen()
    214. } else if (de.mozRequestFullScreen) {
    215. de.mozRequestFullScreen()
    216. } else if (de.webkitRequestFullScreen) {
    217. de.webkitRequestFullScreen()
    218. }
    219. },
    220. //退出全屏
    221. exitFullscreen() {
    222. let de = document
    223. if (de.exitFullscreen) {
    224. de.exitFullscreen()
    225. } else if (de.mozCancelFullScreen) {
    226. de.mozCancelFullScreen()
    227. } else if (de.webkitCancelFullScreen) {
    228. de.webkitCancelFullScreen()
    229. }
    230. },
    231. //全屏切换
    232. toFullOrExit() {
    233. this.isFull = !this.isFull
    234. if (this.isFull) {
    235. this.requestFullScreen()
    236. } else {
    237. this.exitFullscreen()
    238. }
    239. },
    240. // // 禁用鼠标右击、F12 来禁止打印和打开调试工具
    241. // prohibit() {
    242. // // console.log(document)
    243. // document.oncontextmenu = function () {
    244. // return false
    245. // }
    246. // document.onkeydown = function (e) {
    247. // 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)) {
    248. // return false
    249. // }
    250. // if (e.keyCode === 18 || e.keyCode === 123) {
    251. // return false
    252. // }
    253. // }
    254. // }
    255. }
    256. }
    257. </script>
    258. <style scoped>
    259. #container {
    260. overflow: auto;
    261. height: 100%;
    262. font-family: PingFang SC;
    263. width: 100%;
    264. display: flex;
    265. /* justify-content: center; */
    266. position: relative;
    267. }
    268. /* 右侧功能按钮区 */
    269. .right-btn {
    270. position: fixed;
    271. right: 5%;
    272. bottom: 15%;
    273. width: 120px;
    274. display: flex;
    275. flex-wrap: wrap;
    276. justify-content: center;
    277. z-index: 99;
    278. }
    279. .pdfArea {
    280. width: 100%;
    281. }
    282. /* ------------------- 输入页码 ------------------- */
    283. .pageNum {
    284. margin: 10px 0;
    285. font-size: 18px;
    286. }
    287. /*在谷歌下移除input[number]的上下箭头*/
    288. input::-webkit-outer-spin-button,
    289. input::-webkit-inner-spin-button {
    290. -webkit-appearance: none !important;
    291. margin: 0;
    292. }
    293. 在firefox下移除input[number]的上下箭头 input[type='number'] {
    294. -moz-appearance: textfield;
    295. }
    296. .inputNumber {
    297. border-radius: 8px;
    298. border: 1px solid #999999;
    299. height: 35px;
    300. font-size: 18px;
    301. width: 60px;
    302. text-align: center;
    303. }
    304. .inputNumber:focus {
    305. border: 1px solid #00aeff;
    306. background-color: rgba(18, 163, 230, 0.096);
    307. outline: none;
    308. transition: 0.2s;
    309. }
    310. /* ------------------- 切换页码 ------------------- */
    311. .turn {
    312. background-color: #24aceb;
    313. opacity: 0.7;
    314. color: #ffffff;
    315. height: 70px;
    316. width: 70px;
    317. border-radius: 50%;
    318. display: flex;
    319. align-items: center;
    320. justify-content: center;
    321. margin: 5px 0;
    322. }
    323. .turn-btn {
    324. background-color: #24aceb;
    325. opacity: 0.6;
    326. color: #ffffff;
    327. height: 70px;
    328. width: 70px;
    329. border-radius: 50%;
    330. margin: 5px 0;
    331. display: flex;
    332. align-items: center;
    333. justify-content: center;
    334. }
    335. .turn-autobtn {
    336. background-color: #0467fa;
    337. opacity: 0.6;
    338. color: #ffffff;
    339. height: 70px;
    340. width: 70px;
    341. border-radius: 50%;
    342. margin: 5px 0;
    343. display: flex;
    344. align-items: center;
    345. justify-content: center;
    346. }
    347. .turn-btn:hover,
    348. .turn:hover {
    349. transition: 0.3s;
    350. opacity: 0.5;
    351. cursor: pointer;
    352. }
    353. /* ------------------- 进度条 ------------------- */
    354. .progress {
    355. position: absolute;
    356. right: 50%;
    357. top: 50%;
    358. text-align: center;
    359. }
    360. .progress>span {
    361. color: #199edb;
    362. font-size: 14px;
    363. }
    364. </style>

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

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

  • 相关阅读:
    【JavaEE初阶】多线程 _ 基础篇 _ Thread类的使用、线程的几个重要操作和状态
    不懂“数据服务”,聊什么“数据中台”
    python学习--函数
    IntelliJ IDEA 2022.2发布首个Beta版本,看看有哪些更新
    2022 OpenCV AI 竞赛来啦!详细介绍Spatial AI赛道!
    Linux安装mysql数据库并实现主从搭建
    【堆】数据结构-堆的实现【超详细的数据结构教学】
    【AI视野·今日Robot 机器人论文速览 第五十四期】Fri, 13 Oct 2023
    代理IP与Socks5代理在多领域的卓越应用
    深入理解分布式一致算法:原理、应用与挑战
  • 原文地址:https://blog.csdn.net/ruo40018293/article/details/132976230