vue實(shí)現(xiàn)pdf文檔在線預(yù)覽功能
針對android系統(tǒng)不支持pdf文檔在線預(yù)覽,可通過引入pdf.js插件實(shí)現(xiàn),其具體實(shí)現(xiàn)步驟如下
一、引入插件
方式一:npm install --save pdfjs-dist,安裝完成后在vue項(xiàng)目的node_modules出現(xiàn)如下依賴
方式二:只引入pdf.js的核心文件pdf.js和pdf.work.js,其他無關(guān)的文件全部刪除,如圖
方式三:將插件直接放在static文件夾下,如圖
二、前端頁面代碼
方式一和方式二:特點(diǎn)精簡
<template> <div> <canvas v-for="page in pages" :id="'the-canvas'+page" :key="page"></canvas> </div> </template> <script> // 方式一 import PDFJS from 'pdfjs-dist' // 方式二 import * as PDFJS from '../../../static/pdf/build/pdf' export default { // 返回?cái)?shù)據(jù) data () { return { pdfDoc: null, pages: 0 } }, created () { }, mounted () { this.showPdf() }, methods: { showPdf: function () { // 請求本地文件 let url = '/static/pdf/web/compressed.tracemonkey-pldi-09.pdf' // 跨域請求文件,需要走后臺代理,后臺需要將文件流返回前端才可在頁面顯示 // let url = '/pdf/showPdf?pdfUrl=http://test.hccb.cc/corporBankWXTest/static/123.pdf' this.loadFile(url) }, renderPage: function (num) { let _this = this this.pdfDoc.getPage(num).then(function (page) { let canvas = document.getElementById('the-canvas' + num) let ctx = canvas.getContext('2d') let dpr = window.devicePixelRatio || 1.0 let bsr = ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1.0 let ratio = dpr / bsr let viewport = page.getViewport(window.screen.availWidth / page.getViewport(1).width) canvas.width = viewport.width * ratio canvas.height = viewport.height * ratio canvas.style.width = viewport.width + 'px' canvas.style.height = viewport.height + 'px' ctx.setTransform(ratio, 0, 0, ratio, 0, 0) var renderContext = { canvasContext: ctx, viewport: viewport } page.render(renderContext) if (_this.pages > num) { _this.renderPage(num + 1) } }) }, loadFile: function (url) { let _this = this PDFJS.getDocument(url).then(function (pdf) { _this.pdfDoc = pdf _this.pages = _this.pdfDoc.numPages _this.$nextTick(() => { _this.renderPage(1) }) }) } } } </script> <style scoped> canvas { display: block; border-bottom: 1px solid black; } </style>
方式三:功能強(qiáng)大,但是引入過多無用文件,此種方式的filePath如為本地文件不進(jìn)行編碼也可發(fā)送請求,如為跨域文件不進(jìn)行編碼無法發(fā)送請求,因此建議統(tǒng)一進(jìn)行編碼。
<template> <div > <iframe :src="url" id="iframe" style="width: 100%;" @load="sureHeight"></iframe> </div> </template> <script> export default { // 返回?cái)?shù)據(jù) data () { return { url: '' } }, // 模塊創(chuàng)建時(shí)執(zhí)行 created () { }, // 模塊渲染時(shí)執(zhí)行 mounted () { // 本地請求文件 let filePath = encodeURIComponent('/static/pdf/web/compressed.tracemonkey-pldi-09.pdf') // 跨域請求文件,需走后臺代理 // let filePath2 = encodeURIComponent('/pdf/showPdf?pdfUrl=http://test.hccb.cc/corporBankWXTest/static/123.pdf') // pdf文檔展示的頁面 this.url = '/static/pdf/web/viewer.html?file=' + filePath }, // 定義模塊測試方法 methods: { // 此方法用于動態(tài)確定元素iframe的高度,使展示的pdf文檔占滿整個(gè)屏幕 sureHeight: function () { let element = document.getElementById('iframe') element.style.height = window.screen.height + 'px' } } } </script> <style scoped> </style>
三、后臺代碼實(shí)現(xiàn)
后臺通過http請求將獲取的文檔流返回給前端
@Controller public class ShowPdfController { @RequestMapping(name = "/showPdf") public String showPdf(HttpServletRequest request, HttpServletResponse response, String pdfUrl) { try { pdfUrl = pdfUrl.trim(); URL url = new URL(pdfUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5*1000); InputStream inputStream = conn.getInputStream(); response.setHeader("Content-Disposition", "attachment;fileName=show.pdf"); response.setContentType("multipart/form-data"); OutputStream outputStream = response.getOutputStream(); IOUtils.write(IOUtils.toByteArray(inputStream), outputStream); } catch (Exception e) { e.printStackTrace(); } return null; } }
具體采用哪種方式實(shí)現(xiàn)pdf文檔的在線預(yù)覽,可根據(jù)項(xiàng)目實(shí)際情況選擇,如業(yè)務(wù)簡單建議使用方式一和方式二(精簡),如業(yè)務(wù)復(fù)雜建議使用方式三(功能強(qiáng)大)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Element樹形控件el-tree實(shí)現(xiàn)一鍵全選、反選功能
最近做的項(xiàng)目用到了全選全不選功能,于是就自己動手寫了一個(gè),這篇文章主要給大家介紹了關(guān)于Element樹形控件el-tree實(shí)現(xiàn)一鍵全選、反選功能的相關(guān)資料,需要的朋友可以參考下2023-10-10vue3中setup語法糖下通用的分頁插件實(shí)例詳解
這篇文章主要介紹了vue3中setup語法糖下通用的分頁插件,實(shí)例代碼介紹了自定義分頁插件:PagePlugin.vue,文中提到了vue3中setup語法糖下父子組件之間的通信,需要的朋友可以參考下2022-10-10vue中watch和computed為什么能監(jiān)聽到數(shù)據(jù)的改變以及不同之處
這篇文章主要介紹了vue中watch和computed為什么能監(jiān)聽到數(shù)據(jù)的改變以及不同之處,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12vue項(xiàng)目打包發(fā)布后接口報(bào)405錯(cuò)誤的解決
這篇文章主要介紹了vue項(xiàng)目打包發(fā)布后接口報(bào)405錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vue?draggable組件實(shí)現(xiàn)拖拽及點(diǎn)擊無效問題的解決
這篇文章主要介紹了vue?draggable組件實(shí)現(xiàn)拖拽及點(diǎn)擊無效問題的解決,只需要在設(shè)置handle屬性就可以了,.defaultTypeTag 是要拖拽的塊的類名,要注意的是需要做點(diǎn)擊事件的項(xiàng)不能包含在這個(gè)類名里面,不然會無法觸發(fā)點(diǎn)擊事件,詳細(xì)解決辦法跟隨小編一起學(xué)習(xí)吧2022-05-05使用vue-cli4.0快速搭建一個(gè)項(xiàng)目的方法步驟
這篇文章主要介紹了使用vue-cli4.0快速搭建一個(gè)項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Vue實(shí)現(xiàn)側(cè)邊導(dǎo)航欄于Tab頁關(guān)聯(lián)的示例代碼
本文主要介紹了Vue實(shí)現(xiàn)側(cè)邊導(dǎo)航欄于Tab頁關(guān)聯(lián)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11vue實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)及滾動監(jiān)聽的方法
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)及滾動監(jiān)聽的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07