前端實(shí)現(xiàn)pdf預(yù)覽功能的全過(guò)程(基于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)的過(guò)程和問(wèn)題
一、利用iframe實(shí)現(xiàn)pdf預(yù)覽
1、實(shí)現(xiàn)過(guò)程
將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、遇到的問(wèn)題
電腦上測(cè)試正常,但是安卓端會(huì)出現(xiàn)空白頁(yè)和直接跳轉(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)過(guò)程
下包
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è)試還是存在空白頁(yè)問(wèn)題,于是換成插件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ì)象,可以用來(lái)操作pdf,監(jiān)聽(tīng)相關(guān)事件
// pdfh5 = new Pdfh5(selector, options) goto初始到第幾頁(yè),logo設(shè)置每一頁(yè)pdf上的水印
this.pdfh5 = new Pdfh5('#pdf-content', {
pdfurl: this.$route.query.pdfSrc,
goto: 1
// 設(shè)置每一頁(yè)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)聽(tīng)pdf準(zhǔn)備開(kāi)始渲染,此時(shí)可以拿到pdf總頁(yè)數(shù)
this.pdfh5.on('ready', function () {
console.log('總頁(yè)數(shù):' + this.totalNum)
})
// 監(jiān)聽(tīng)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使用ArcGis?API?for?js創(chuàng)建地圖實(shí)現(xiàn)示例
這篇文章主要為大家介紹了vue使用ArcGis?API?for?js創(chuàng)建地圖實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Vue開(kāi)發(fā)實(shí)踐指南之應(yīng)用入口
這篇文章主要給大家介紹了關(guān)于Vue開(kāi)發(fā)實(shí)踐指南之應(yīng)用入口的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
vue3在自定義hooks中使用useRouter報(bào)錯(cuò)的解決方案
這篇文章主要介紹了vue3在自定義hooks中使用useRouter報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue數(shù)據(jù)驅(qū)動(dòng)試圖的實(shí)現(xiàn)方法及原理
當(dāng)Vue實(shí)例中的數(shù)據(jù)(data)發(fā)生變化時(shí),與之相關(guān)聯(lián)的視圖(template)會(huì)自動(dòng)更新,反映出最新的數(shù)據(jù)狀態(tài), Vue的數(shù)據(jù)驅(qū)動(dòng)視圖是通過(guò)其響應(yīng)式系統(tǒng)實(shí)現(xiàn)的,以下是Vue數(shù)據(jù)驅(qū)動(dòng)視圖實(shí)現(xiàn)的核心原理,需要的朋友可以參考下2024-10-10
vue如何實(shí)現(xiàn)二進(jìn)制流文件導(dǎo)出excel
這篇文章主要介紹了vue如何實(shí)現(xiàn)二進(jìn)制流文件導(dǎo)出excel,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

