欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實(shí)現(xiàn)pdf文檔在線預(yù)覽功能

 更新時(shí)間:2019年11月26日 14:16:31   作者:TSCB_0325  
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)pdf文檔在線預(yù)覽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

針對(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)如下依賴

方式二:只引入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文檔占滿整個(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)文章

最新評(píng)論