前端實(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)文章
Vue.js第一天學(xué)習(xí)筆記(數(shù)據(jù)的雙向綁定、常用指令)
這篇文章主要為大家分享了Vue.js第一天的學(xué)習(xí)筆記,包括數(shù)據(jù)的雙向綁定、常用指令學(xué)習(xí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
html+vue.js 實(shí)現(xiàn)漂亮分頁功能可兼容IE
功能比較簡(jiǎn)單,在單一html中使用vue.js分頁展示數(shù)據(jù),并未安裝腳手架,或使用相關(guān)UI框架,此時(shí)需要手寫一個(gè)分頁器,不失為最合理最便捷的解決方案,需要的朋友可以參考下2020-11-11
Vue+tracking.js 實(shí)現(xiàn)前端人臉檢測(cè)功能
這篇文章主要介紹了Vue+tracking.js 實(shí)現(xiàn)前端人臉檢測(cè)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
vue中transition組件在項(xiàng)目中運(yùn)用小結(jié)
這篇文章主要介紹了vue中transition組件在項(xiàng)目中運(yùn)用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
Vue注冊(cè)模塊與登錄狀態(tài)的持久化實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Vue注冊(cè)模塊與登錄狀態(tài)的持久化實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vue上傳項(xiàng)目到git時(shí),如何忽略node_modules文件夾
這篇文章主要介紹了vue上傳項(xiàng)目到git時(shí),如何忽略node_modules文件夾,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

