vue實(shí)現(xiàn)pdf文檔在線預(yù)覽功能
針對(duì)android系統(tǒng)不支持pdf文檔在線預(yù)覽,可通過(guò)引入pdf.js插件實(shí)現(xiàn),其具體實(shí)現(xiàn)步驟如下
一、引入插件
方式一:npm install --save pdfjs-dist,安裝完成后在vue項(xiàng)目的node_modules出現(xiàn)如下依賴(lài)

方式二:只引入pdf.js的核心文件pdf.js和pdf.work.js,其他無(wú)關(guān)的文件全部刪除,如圖

方式三:將插件直接放在static文件夾下,如圖

二、前端頁(yè)面代碼
方式一和方式二:特點(diǎn)精簡(jiǎ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 () {
// 請(qǐng)求本地文件
let url = '/static/pdf/web/compressed.tracemonkey-pldi-09.pdf'
// 跨域請(qǐng)求文件,需要走后臺(tái)代理,后臺(tái)需要將文件流返回前端才可在頁(yè)面顯示
// 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)大,但是引入過(guò)多無(wú)用文件,此種方式的filePath如為本地文件不進(jìn)行編碼也可發(fā)送請(qǐng)求,如為跨域文件不進(jìn)行編碼無(wú)法發(fā)送請(qǐng)求,因此建議統(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 () {
// 本地請(qǐng)求文件
let filePath = encodeURIComponent('/static/pdf/web/compressed.tracemonkey-pldi-09.pdf')
// 跨域請(qǐng)求文件,需走后臺(tái)代理
// let filePath2 = encodeURIComponent('/pdf/showPdf?pdfUrl=http://test.hccb.cc/corporBankWXTest/static/123.pdf')
// pdf文檔展示的頁(yè)面
this.url = '/static/pdf/web/viewer.html?file=' + filePath
},
// 定義模塊測(cè)試方法
methods: {
// 此方法用于動(dòng)態(tài)確定元素iframe的高度,使展示的pdf文檔占滿(mǎn)整個(gè)屏幕
sureHeight: function () {
let element = document.getElementById('iframe')
element.style.height = window.screen.height + 'px'
}
}
}
</script>
<style scoped>
</style>
三、后臺(tái)代碼實(shí)現(xiàn)
后臺(tái)通過(guò)http請(qǐng)求將獲取的文檔流返回給前端
@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ù)簡(jiǎn)單建議使用方式一和方式二(精簡(jiǎn)),如業(yè)務(wù)復(fù)雜建議使用方式三(功能強(qiáng)大)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Ant Design Vue table中列超長(zhǎng)顯示...并加提示語(yǔ)的實(shí)例
這篇文章主要介紹了Ant Design Vue table中列超長(zhǎng)顯示...并加提示語(yǔ)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
手寫(xiě)實(shí)現(xiàn)Vue計(jì)算屬性
這篇文章主要介紹了手寫(xiě)實(shí)現(xiàn)Vue計(jì)算屬性,本文從一個(gè)簡(jiǎn)單的計(jì)算屬性例子開(kāi)始,一步步實(shí)現(xiàn)了計(jì)算屬性。并且針對(duì)這個(gè)例子,詳細(xì)分析了頁(yè)面渲染時(shí)的整個(gè)代碼執(zhí)行邏輯,需要的小伙伴可以參考一下2022-08-08
vue單頁(yè)面SEO優(yōu)化的實(shí)現(xiàn)
本文主要介紹了vue單頁(yè)面SEO優(yōu)化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
vue中PC端地址跳轉(zhuǎn)移動(dòng)端的操作方法
最近小編接到一個(gè)項(xiàng)目pc端和移動(dòng)端是兩個(gè)獨(dú)立的項(xiàng)目,兩個(gè)項(xiàng)目項(xiàng)目中的內(nèi)容基本相同,鏈接組合的方式都有規(guī)律可循,接到的需求便是在移動(dòng)端訪問(wèn)pc端的URL連接時(shí),重定向至移動(dòng)端對(duì)應(yīng)頁(yè)面,下面小編給大家分享實(shí)現(xiàn)過(guò)程,一起看看吧2021-11-11
Vue動(dòng)態(tài)添加屬性到data的實(shí)現(xiàn)
在vue中請(qǐng)求接口中,一個(gè)請(qǐng)求方法可能對(duì)應(yīng)后臺(tái)兩個(gè)請(qǐng)求接口,所以請(qǐng)求參數(shù)就會(huì)有所不同。需要我們先設(shè)置共同的參數(shù),然后根據(jù)條件動(dòng)態(tài)添加參數(shù)屬性2022-08-08
vue或css動(dòng)畫(huà)實(shí)現(xiàn)列表向上無(wú)縫滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了vue或css動(dòng)畫(huà)實(shí)現(xiàn)列表向上無(wú)縫滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
從Element日期組件源碼中學(xué)到的兩個(gè)工具方法技巧
這篇文章主要介紹了從Element日期組件源碼中學(xué)到的兩個(gè)工具方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
vue3+electron12+dll開(kāi)發(fā)客戶(hù)端配置詳解
本文將結(jié)合實(shí)例代碼,介紹vue3+electron12+dll客戶(hù)端配置,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06

