Vue使用vue-pdf實(shí)現(xiàn)PDF文件預(yù)覽
前言
vue-pdf npm文檔 https://www.npmjs.com/package/vue-pdf
先看下效果

安裝
npm install vue-pdf
頁(yè)面按需引入并注冊(cè)
import Pdf from "vue-pdf"
export default {
components: { Pdf }
}
完整代碼
<template>
<div class="global">
<div class="preview">
<el-button type="success" @click="isShow = true">開(kāi)始預(yù)覽</el-button>
</div>
<div class="pdfContainer" v-if="isShow">
<div class="button">
<el-button type="success" @click="prev()">上一頁(yè)</el-button>
<el-button type="success" @click="next()">下一頁(yè)</el-button>
<el-button type="success" @click="scaleD()">放大</el-button>
<el-button type="success" @click="scaleX()">縮小</el-button>
<el-button type="success" @click="clockwise()">順時(shí)針</el-button>
<el-button type="success" @click="antiClockwise()">逆時(shí)針</el-button>
</div>
<div class="pdf">
<pdf
ref="pdf"
:src="src"
:page="currentPage"
:rotate="pageRotate"
@num-pages="pageCount = $event"
@page-loaded="currentPage = $event"
@loaded="loadPdfHandler"
></pdf>
</div>
<div class="page">
<span class="pageNum">{{ currentPage }}</span>
<span class="pageNum">/</span>
<span class="pageNum">{{ pageCount }}</span>
</div>
</div>
</div>
</template>
<script>
import Pdf from "vue-pdf";
export default {
data() {
return {
src: "http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf", // pdf的路徑 實(shí)際項(xiàng)目后端返回
currentPage: 0, // pdf文件頁(yè)碼
pageCount: 0, // pdf文件總頁(yè)數(shù)
scale: 100, // 開(kāi)始的時(shí)候默認(rèn)和容器一樣大即寬高都是100%
pageRotate: 0, // 旋轉(zhuǎn)角度
isShow: false
};
},
components: { Pdf },
mounted() {
console.log(Pdf, "Pdf----->>>");
},
methods: {
// pdf加載時(shí)
loadPdfHandler(e) {
this.currentPage = 1; // 加載的時(shí)候先加載第一頁(yè)
this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
this.$refs.pdf.$el.style.height = parseInt(this.scale) + "%";
},
// 上一頁(yè)
prev() {
if (this.currentPage > 1) {
this.currentPage--;
}
},
// 下一頁(yè)
next() {
if (this.currentPage < this.pageCount) {
this.currentPage++;
}
},
//放大
scaleD() {
if(this.scale == 100) return this.$message.warning('已經(jīng)是最大嘍~~')
this.scale += 10;
this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
this.$refs.pdf.$el.style.height = parseInt(this.scale) + "%";
},
//縮小
scaleX() {
if(this.scale == 40) return this.$message.warning('已經(jīng)是最小嘍~~')
this.scale += -10;
this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
this.$refs.pdf.$el.style.height = parseInt(this.scale) + "%";
},
// 順時(shí)針
clockwise() {
this.pageRotate += 90;
},
// 逆時(shí)針
antiClockwise() {
this.pageRotate -= 90;
},
},
};
</script>
<style scoped>
.global {
width: 100%;
height: 100%;
}
.preview {
width: 100%;
height: 50px;
}
.pdfContainer {
width: 100%;
height: calc(100% - 50px);
}
.pdfContainer .button {
width: 100%;
height: 50px;
display: flex;
align-items: center;
}
.pdfContainer .pdf {
width: 100%;
height: calc(100% - 100px);
overflow-y: auto;
}
.pdfContainer .page {
width: 100%;
height: 50px;
}
.pageNum {
font-size: 28px;
}
</style>到此這篇關(guān)于Vue使用vue-pdf實(shí)現(xiàn)PDF文件預(yù)覽的文章就介紹到這了,更多相關(guān)Vue PDF文件預(yù)覽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue請(qǐng)求本地自己編寫(xiě)的json文件的方法
這篇文章主要介紹了vue請(qǐng)求本地自己編寫(xiě)的json文件,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
uniapp小程序上傳文件webapi后端項(xiàng)目asp.net完整代碼
在uniapp中,實(shí)現(xiàn)文件上傳功能也變得非常簡(jiǎn)單,下面這篇文章主要給大家介紹了關(guān)于uniapp小程序上傳文件webapi后端項(xiàng)目asp.net的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
vue組件實(shí)現(xiàn)文字居中對(duì)齊的方法
這篇文章主要為大家詳細(xì)介紹了vue組件實(shí)現(xiàn)文字居中對(duì)齊的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
vue3 高德地圖關(guān)鍵詞搜索獲取經(jīng)緯度的示例代碼
這篇文章主要介紹了vue3 高德地圖關(guān)鍵詞搜索獲取經(jīng)緯度的示例代碼,需要的朋友可以參考下2024-08-08
Element Carousel 走馬燈的具體實(shí)現(xiàn)
這篇文章主要介紹了Element Carousel 走馬燈的具體實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
vue3清空l(shuí)et?arr?=?reactive([])的實(shí)現(xiàn)示例
本文主要介紹了vue3清空l(shuí)et?arr?=?reactive([])的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03

