vue3+vite實(shí)現(xiàn)在線預(yù)覽pdf功能
效果圖

代碼
<template>
<div class="pdf-preview">
<div class="pdf-wrap">
<vue-pdf-embed :source="state.source" :style="scale" class="vue-pdf-embed" :page="state.pageNum" />
</div>
<div class="page-tool">
<div class="page-tool-item" @click="lastPage()">上一頁(yè)</div>
<div class="page-tool-item" @click="nextPage()">下一頁(yè)</div>
<div class="page-tool-item">{{state.pageNum}}/{{state.numPages}}</div>
<div class="page-tool-item" @click="pageZoomOut()">放大</div>
<div class="page-tool-item" @click="pageZoomIn()">縮小</div>
<div class="page-tool-item" @click="$emit('close')">關(guān)閉</div>
</div>
</div>
</template>
<script setup lang="ts">
import VuePdfEmbed from "vue-pdf-embed";
//import { createLoadingTask } from "vue3-pdfjs/esm/vue-pdf.js";
import * as pdfjsLib from "pdfjs-dist";
import { reactive, onMounted, computed } from "vue";
const props = defineProps({
pdfUrl: {
type: String,
required: true
}
})
const state = reactive({
source: props.pdfUrl, //預(yù)覽pdf文件地址
pageNum: 1, //當(dāng)前頁(yè)面
scale: 1, // 縮放比例
numPages: 0, // 總頁(yè)數(shù)
});
const scale = computed(() => `transform:scale(${state.scale})`)
function lastPage() {
if (state.pageNum > 1) {
state.pageNum -= 1;
}
}
function nextPage() {
if (state.pageNum < state.numPages) {
state.pageNum += 1;
}
}
function pageZoomOut() {
if (state.scale < 2) {
state.scale += 0.1;
}
}
function pageZoomIn() {
if (state.scale > 0.8) {
state.scale -= 0.1;
}
}
onMounted(() => {
pdfjsLib.GlobalWorkerOptions.workerSrc = "./pdf.worker.js";
const loadingTask = pdfjsLib.getDocument(state.source);
loadingTask.promise.then((pdf) => {
state.numPages = pdf.numPages;
});
});
</script>
<style lang="css" scoped>
.pdf-preview {
position: fixed;
height: 100vh;
width: 100vw;
padding: 20px 0;
box-sizing: border-box;
background-color: #e9e9e9;
left: 0;
top: 0;
z-index: 99999999;
overflow-y: auto;
}
.pdf-wrap {
overflow-y: auto;
}
.vue-pdf-embed {
text-align: center;
width: 60vw;
border: 1px solid #e5e5e5;
margin: 0 auto;
box-sizing: border-box;
}
.page-tool {
position: fixed;
bottom: 35px;
padding-left: 15px;
padding-right: 15px;
display: flex;
align-items: center;
background: rgb(66, 66, 66);
border-radius: 19px;
z-index: 100;
cursor: pointer;
margin-left: 50%;
transform: translateX(-50%);
}
.page-tool-item {
padding: 8px 15px;
padding-left: 10px;
cursor: pointer;
color: #fff !important;
}
</style>
到此這篇關(guān)于vue3+vite實(shí)現(xiàn)在線預(yù)覽pdf功能的文章就介紹到這了,更多相關(guān)vue3 vite在線預(yù)覽pdf內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue項(xiàng)目啟動(dòng)提示Cannot GET /問(wèn)題
這篇文章主要介紹了Vue項(xiàng)目啟動(dòng)提示Cannot GET /問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue3 座位選座矩陣布局的實(shí)現(xiàn)方法(可點(diǎn)擊選中拖拽調(diào)換位置)
由于公司項(xiàng)目需求需要做一個(gè)線上設(shè)置考場(chǎng)相關(guān)的座位布局用于給學(xué)生考機(jī)排號(hào)考試,實(shí)現(xiàn)教室考場(chǎng)座位布局的矩陣布局,可點(diǎn)擊選中標(biāo)記是否有座無(wú)座拖拽調(diào)換位置橫向縱向排列,本文給大家分享實(shí)現(xiàn)代碼,一起看看吧2023-11-11
淺談Vue-cli單文件組件引入less,sass,css樣式的不同方法
下面小編就為大家分享一篇淺談Vue-cli單文件組件引入less,sass,css樣式的不同方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Vue使用Echarts的三種實(shí)現(xiàn)方式
文章介紹了三種在Vue項(xiàng)目中使用ECharts的方法:直接使用原始方法、使用vue-echarts和v-chhart,詳細(xì)描述了每種方法的步驟和優(yōu)勢(shì),幫助開(kāi)發(fā)者在Vue項(xiàng)目中更方便地地使用ECharts2026-04-04
警告[vue-router]?Duplicate?named?routes?definition簡(jiǎn)單解決方法
這篇文章主要關(guān)于介紹了警告[vue-router]?Duplicate?named?routes?definition的解決方法,這個(gè)錯(cuò)誤提示是因?yàn)樵赩ue Router中定義了重復(fù)的路由名稱(chēng),需要的朋友可以參考下2023-12-12
解決vue無(wú)法偵聽(tīng)數(shù)組及對(duì)象屬性的變化問(wèn)題
這篇文章主要介紹了解決vue無(wú)法偵聽(tīng)數(shù)組及對(duì)象屬性的變化問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07

