前端實(shí)現(xiàn)pdf預(yù)覽功能的全過程(基于vue)
前言:
項(xiàng)目中之前pdf預(yù)覽是客戶端andrio實(shí)現(xiàn)的,現(xiàn)在需要前端H5自己實(shí)現(xiàn)預(yù)覽功能,項(xiàng)目是基于vue的H5項(xiàng)目,記錄一下pdf預(yù)覽功能實(shí)現(xiàn)的過程和問題
一、利用iframe實(shí)現(xiàn)pdf預(yù)覽
1、實(shí)現(xiàn)過程
將pdf路徑設(shè)置給iframe的src屬性
<iframe :src="pdfUrl" marginWidth="0" marginHeight="0" scrolling="no" frameBorder="0" style="width: calc(100% + 17px); height: calc(100% + 17px)"></iframe>
create(){ //路由路徑上獲取pdf路徑參數(shù) var extension = this.$route.query.pdfSrc.split('.').pop().toLowerCase() console.log(extension, 'extensionextension') if (extension == 'pdf') { this.pdfSrc = `${this.$route.query.pdfSrc}#toolbar=0` } else { this.pdfSrc = 'https://view.officeapps.live.com/op/embed.aspx?src=' + this.$route.query.pdfSrc } }
2、遇到的問題
電腦上測(cè)試正常,但是安卓端會(huì)出現(xiàn)空白頁和直接跳轉(zhuǎn)下載的現(xiàn)象,解決思路:客戶端同事推薦用pdf.js,然后在網(wǎng)上查找發(fā)現(xiàn),vue有一個(gè)插件vue-pdf,是基于pdf.js封住的,因此決定采用插件vue-pdf實(shí)現(xiàn)
二、vue-pdf插件實(shí)現(xiàn)預(yù)覽
1、實(shí)現(xiàn)過程
下包
npm i vue-pdf
引入并使用
<template> <div class="pdf-container"> <pdf v-for="item in numPages" :key="item" :src="pdfSrc" :page="item" ref="pdf"></pdf> </div> </template> <script> import pdf from 'vue-pdf' export default { data () { return { pdfSrc: '', numPages: null } }, components: { pdf }, computed: {}, created () { var extension = this.$route.query.pdfSrc.split('.').pop().toLowerCase() if (extension == 'pdf') { this.pdfSrc = `${this.$route.query.pdfSrc}#toolbar=0` } else { this.pdfSrc = 'https://view.officeapps.live.com/op/embed.aspx?src=' + this.$route.query.pdfSrc } } }, mounted () { this.getNumPages() }, methods: { getNumPages () { const loadingTask = pdf.createLoadingTask(this.pdfSrc) loadingTask.promise.then(pdf => { this.numPages = pdf.numPages console.log(' this.numPages', this.numPages) }).catch(err => { debugger console.error('pdf 加載失敗', err) }) } } } </script>
部署到測(cè)試線app中測(cè)試還是存在空白頁問題,于是換成插件pdfH5
三、pdfH5實(shí)現(xiàn)預(yù)覽
下包
npm i pdfh5
代碼實(shí)現(xiàn)
<template> <div class="pdf-container"> <div id="pdf-content"></div> <iframe v-if="docType!='pdf'" :src="pdfUrl" marginWidth="0" marginHeight="0" scrolling="no" frameBorder="0" style="width: calc(100% + 17px); height: calc(100% + 17px)"></iframe> </div> </template> <script> import Pdfh5 from 'pdfh5' import 'pdfh5/css/pdfh5.css' export default { name: 'Pdfh5', data () { return { docType: '', pdfh5: null, // 可引入網(wǎng)絡(luò)文件或者本地文件 pdfUrl: 'http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf' // 如果引入本地pdf文件,需要將pdf放在public文件夾下,引用時(shí)使用絕對(duì)路徑(/:表示public文件夾) } }, mounted () { this.docType = this.$route.query.pdfSrc.split('.').pop().toLowerCase() if (this.docType == 'pdf') { this.initPdf() } else { this.pdfUrl = 'https://view.officeapps.live.com/op/embed.aspx?src=' + this.$route.query.pdfSrc } }, methods: { initPdf () { // pdfh5實(shí)例化時(shí)傳兩個(gè)參數(shù):selector選擇器,options配置項(xiàng)參數(shù),會(huì)返回一個(gè)pdfh5實(shí)例對(duì)象,可以用來操作pdf,監(jiān)聽相關(guān)事件 // pdfh5 = new Pdfh5(selector, options) goto初始到第幾頁,logo設(shè)置每一頁pdf上的水印 this.pdfh5 = new Pdfh5('#pdf-content', { pdfurl: this.$route.query.pdfSrc, goto: 1 // 設(shè)置每一頁pdf上的水印 // logo: { src: require('@/assets/images/bus/icon_head@2x.png'), x: 420, y: 700, width: 120, height: 120 } }) this.pdfh5.scrollEnable(true) // 允許pdf滾動(dòng) // 監(jiān)聽pdf準(zhǔn)備開始渲染,此時(shí)可以拿到pdf總頁數(shù) this.pdfh5.on('ready', function () { console.log('總頁數(shù):' + this.totalNum) }) // 監(jiān)聽pdf加載完成事件,加載失敗、渲染成功都會(huì)觸發(fā) this.pdfh5.on('complete', (status, msg, time) => { console.log('狀態(tài):' + status + ',信息:' + msg + ',耗時(shí):' + time + '毫秒') }) } } } </script> <style scoped> .pdfjs { height: 100vh !important; } .pdf-container { width: 100%; height: 100%; } </style>
最終測(cè)試,該方案可以。
總結(jié)
到此這篇關(guān)于前端實(shí)現(xiàn)pdf預(yù)覽功能的文章就介紹到這了,更多相關(guān)前端實(shí)現(xiàn)pdf預(yù)覽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3.x 使用jsplumb實(shí)現(xiàn)拖拽連線
這篇文章主要為大家詳細(xì)介紹了vue3.x 使用jsplumb實(shí)現(xiàn)拖拽連線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03VUE腳手架框架編寫簡(jiǎn)潔的登錄界面的實(shí)現(xiàn)
本文主要介紹了VUE腳手架框架編寫簡(jiǎn)潔的登錄界面的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Vue項(xiàng)目如何設(shè)置反向代理和cookie設(shè)置問題
這篇文章主要介紹了Vue項(xiàng)目如何設(shè)置反向代理和cookie設(shè)置問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04一文詳解Pinia和Vuex與兩個(gè)Vue狀態(tài)管理模式
這篇文章主要介紹了一文詳解Pinia和Vuex與兩個(gè)Vue狀態(tài)管理模式,Pinia和Vuex一樣都是是vue的全局狀態(tài)管理器。其實(shí)Pinia就是Vuex5,只不過為了尊重原作者的貢獻(xiàn)就沿用了這個(gè)看起來很甜的名字Pinia2022-08-08vue獲取token實(shí)現(xiàn)token登錄的示例代碼
最近新做了個(gè)vue項(xiàng)目,正好項(xiàng)目中有登錄部分,本文就詳細(xì)的介紹一下登錄部分的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下2021-11-11vue2.0 如何把子組件的數(shù)據(jù)傳給父組件(推薦)
這篇文章主要介紹了vue2.0 如何把子組件的數(shù)據(jù)傳給父組件,需要的朋友可以參考下2018-01-01