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

發(fā)現(xiàn)vue-pdf組件的canvas標簽里面把高度寫死成 height:212.269px了。
我們嘗試給這個組件再寫一個pdf-preview的class 將設(shè)置高度為100%發(fā)現(xiàn)不生效。
.pdf-preview {
height: 100%;
}
解決方案
提高指定樣式的應(yīng)用優(yōu)先權(quán)(優(yōu)先級)
.pdf-preview {
height: 100%;
}
// 穿透vue-pdf插件中的canvas樣式
.pdf-preview canvas {
//提高指定樣式規(guī)則的應(yīng)用優(yōu)先權(quán)(優(yōu)先級)
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)先級)
height: 100% !important;
}
</style>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Vue的computed(計算屬性)使用實例之TodoList
本篇文章主要介紹了詳解Vue的computed(計算屬性)使用實例之TodoList,具有一定的參考價值,有興趣的可以了解一下2017-08-08
詳解如何在vue+element-ui的項目中封裝dialog組件
這篇文章主要介紹了詳解如何在vue+element-ui的項目中封裝dialog組件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
vue+elementui+vuex+sessionStorage實現(xiàn)歷史標簽菜單的示例代碼
本文主要介紹了vue+elementui+vuex+sessionStorage實現(xiàn)歷史標簽菜單的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
Vue3+Element?Plus實現(xiàn)動態(tài)標簽頁以及右鍵菜單功能
這篇文章主要給大家介紹了關(guān)于Vue3+Element?Plus實現(xiàn)動態(tài)標簽頁以及右鍵菜單功能的相關(guān)資料,Vue?3和Element?Plus提供了一種簡單的方法來實現(xiàn)側(cè)邊菜單欄與標簽頁之間的聯(lián)動,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-09-09

