Vue使用vue-pdf實現(xiàn)PDF文件預覽
更新時間:2023年03月15日 14:59:41 作者:會說法語的豬
這篇文章主要為大家詳細介紹了Vue如何使用vue-pdf實現(xiàn)PDF文件預覽的功能,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的可以了解一下
前言
vue-pdf npm文檔 https://www.npmjs.com/package/vue-pdf
先看下效果

安裝
npm install vue-pdf
頁面按需引入并注冊
import Pdf from "vue-pdf"
export default {
components: { Pdf }
}
完整代碼
<template>
<div class="global">
<div class="preview">
<el-button type="success" @click="isShow = true">開始預覽</el-button>
</div>
<div class="pdfContainer" v-if="isShow">
<div class="button">
<el-button type="success" @click="prev()">上一頁</el-button>
<el-button type="success" @click="next()">下一頁</el-button>
<el-button type="success" @click="scaleD()">放大</el-button>
<el-button type="success" @click="scaleX()">縮小</el-button>
<el-button type="success" @click="clockwise()">順時針</el-button>
<el-button type="success" @click="antiClockwise()">逆時針</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的路徑 實際項目后端返回
currentPage: 0, // pdf文件頁碼
pageCount: 0, // pdf文件總頁數(shù)
scale: 100, // 開始的時候默認和容器一樣大即寬高都是100%
pageRotate: 0, // 旋轉角度
isShow: false
};
},
components: { Pdf },
mounted() {
console.log(Pdf, "Pdf----->>>");
},
methods: {
// pdf加載時
loadPdfHandler(e) {
this.currentPage = 1; // 加載的時候先加載第一頁
this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
this.$refs.pdf.$el.style.height = parseInt(this.scale) + "%";
},
// 上一頁
prev() {
if (this.currentPage > 1) {
this.currentPage--;
}
},
// 下一頁
next() {
if (this.currentPage < this.pageCount) {
this.currentPage++;
}
},
//放大
scaleD() {
if(this.scale == 100) return this.$message.warning('已經是最大嘍~~')
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('已經是最小嘍~~')
this.scale += -10;
this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
this.$refs.pdf.$el.style.height = parseInt(this.scale) + "%";
},
// 順時針
clockwise() {
this.pageRotate += 90;
},
// 逆時針
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>到此這篇關于Vue使用vue-pdf實現(xiàn)PDF文件預覽的文章就介紹到這了,更多相關Vue PDF文件預覽內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
uniapp小程序上傳文件webapi后端項目asp.net完整代碼
在uniapp中,實現(xiàn)文件上傳功能也變得非常簡單,下面這篇文章主要給大家介紹了關于uniapp小程序上傳文件webapi后端項目asp.net的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-07-07
Element Carousel 走馬燈的具體實現(xiàn)
這篇文章主要介紹了Element Carousel 走馬燈的具體實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
vue3清空let?arr?=?reactive([])的實現(xiàn)示例
本文主要介紹了vue3清空let?arr?=?reactive([])的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-03-03

