vue-pdf實(shí)現(xiàn)pdf在線預(yù)覽并實(shí)現(xiàn)自定義預(yù)覽框高度
首先
安裝vue-pdf庫(kù)
npm install --save vue-pdf
在你的vue頁(yè)面文件中引入這個(gè)組件
然后運(yùn)行程序,這時(shí)候你會(huì)發(fā)現(xiàn),pdf預(yù)覽圖并不能鋪滿你的容器框。
查看樣式:

發(fā)現(xiàn)vue-pdf組件的canvas標(biāo)簽里面把高度寫死成 height:212.269px了。
我們嘗試給這個(gè)組件再寫一個(gè)pdf-preview的class 將設(shè)置高度為100%發(fā)現(xiàn)不生效。
.pdf-preview {
height: 100%;
}
解決方案
提高指定樣式的應(yīng)用優(yōu)先權(quán)(優(yōu)先級(jí))
.pdf-preview {
height: 100%;
}
// 穿透vue-pdf插件中的canvas樣式
.pdf-preview canvas {
//提高指定樣式規(guī)則的應(yīng)用優(yōu)先權(quán)(優(yōu)先級(jí))
height: 100% !important;
}
附上完整代碼
<!--
* @Author: WenZhiming
* @Date: 2022-09-26 17:17:55
* @LastEditors: WenZhiming
* @LastEditTime: 2022-09-26 18:03:13
* @Description: file content
-->
<template>
<div class="container_upload relative">
<pdf
v-if="pdfUrl && pdfUrl.endsWith('.pdf')"
class="pdf-preview"
:src="pdfUrl"
></pdf>
<div class="buttons">
<el-button v-if="pdfUrl" type="primary" plain @click="previewPDF">
{{ $t('查看') }}
</el-button>
<el-button type="primary" class="mt-12" @click="uploadPdf">
{{ $t('上傳') }}
</el-button>
</div>
<input
ref="pdfInput"
type="file"
style="display: none"
accept="application/pdf"
@change="fileChange"
/>
</div>
</template><script>
import pdf from 'vue-pdf'
export default {
components: {
pdf,
},
data() {
return {
pdfUrl: '',
}
},
methods: {
uploadPdf() {
this.$refs.pdfInput.click()
},
fileChange(ev) {
//文件上傳到阿里云oss獲得url
// this._upload(ev, (url) => {
// this.pdfUrl = url
// })
this.pdfUrl = 'https://www.pinduoduo.com/pdd_privacy_policy.pdf'
},
previewPDF() {
window.open(this.pdfUrl, '_blank')
},
},
}
</script><style lang="scss">
.container_upload {
width: 150px;
height: 256px;
border: 1px solid #ddd;
border-radius: 4px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
.buttons {
z-index: 1;
position: absolute;
display: flex;
flex-direction: column;
.el-button {
margin-left: 0;
width: 80px;
}
}
img {
width: 100%;
height: 100%;
}
}
.pdf-preview {
height: 100%;
}
// 穿透vue-pdf插件中的canvas樣式
.pdf-preview canvas {
//提高指定樣式規(guī)則的應(yīng)用優(yōu)先權(quán)(優(yōu)先級(jí))
height: 100% !important;
}
</style>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Vue的computed(計(jì)算屬性)使用實(shí)例之TodoList
本篇文章主要介紹了詳解Vue的computed(計(jì)算屬性)使用實(shí)例之TodoList,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
如何使用Nginx將前端Vue項(xiàng)目部署到云服務(wù)器
記錄使用Nginx將純前端的Vue3項(xiàng)目部署到阿里云服務(wù)器(Ubuntu?22.04)上,包含通過Nginx代理實(shí)現(xiàn)跨域請(qǐng)求、以及個(gè)人踩坑記錄,感興趣的朋友一起看看吧2024-04-04
詳解如何在vue+element-ui的項(xiàng)目中封裝dialog組件
這篇文章主要介紹了詳解如何在vue+element-ui的項(xiàng)目中封裝dialog組件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
vue+elementui+vuex+sessionStorage實(shí)現(xiàn)歷史標(biāo)簽菜單的示例代碼
本文主要介紹了vue+elementui+vuex+sessionStorage實(shí)現(xiàn)歷史標(biāo)簽菜單的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Vue3+Element?Plus實(shí)現(xiàn)動(dòng)態(tài)標(biāo)簽頁(yè)以及右鍵菜單功能
這篇文章主要給大家介紹了關(guān)于Vue3+Element?Plus實(shí)現(xiàn)動(dòng)態(tài)標(biāo)簽頁(yè)以及右鍵菜單功能的相關(guān)資料,Vue?3和Element?Plus提供了一種簡(jiǎn)單的方法來實(shí)現(xiàn)側(cè)邊菜單欄與標(biāo)簽頁(yè)之間的聯(lián)動(dòng),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
Vue下拉框值變動(dòng)事件傳多個(gè)參數(shù)方式
這篇文章主要介紹了Vue下拉框值變動(dòng)事件傳多個(gè)參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
vue3.x項(xiàng)目降級(jí)到vue2.7的解決方案
Vue2.7是Vue2.x的最終次要版本,下面這篇文章主要給大家介紹了關(guān)于vue3.x項(xiàng)目降級(jí)到vue2.7的解決方案,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03

