vue3如何實(shí)現(xiàn)PDF文件在線預(yù)覽功能
概述
之前我們用 Reactjs + React-PDF 實(shí)現(xiàn)了 React 版的 PDF 文件預(yù)覽,今天我們用 Vue3 + Vue-PDF 實(shí)現(xiàn) Vue 版本的 PDF 文件在線預(yù)覽功能。
我們先來(lái)看看完成后效果
正文
創(chuàng)建 vue3 項(xiàng)目
我們先創(chuàng)建一個(gè)的 Vue3 項(xiàng)目, 在終端中輸入命令
pnpm create vite vue-pdf-preview
選擇 vue-ts 回車,cd 進(jìn)入項(xiàng)目根目錄,執(zhí)行 pnpm install, 等待項(xiàng)目依賴包安裝完成。
項(xiàng)目依賴包安裝完成后,我們來(lái)啟動(dòng)項(xiàng)目, 執(zhí)行命令 pnpm run dev ,可以看到控制臺(tái)輸入出了如下內(nèi)容
vite v2.9.9 dev server running at: > Local: http://localhost:3000/ > Network: use `--host` to expose ready in 780ms.
按住 control/command + 鼠標(biāo)左鍵,項(xiàng)目在瀏覽器中打開了
項(xiàng)目啟動(dòng)成功
添加 PDF 預(yù)覽插件
項(xiàng)目啟動(dòng)成功后,我們安裝 PDF 預(yù)覽插件
pnpm install vue-pdf-embed pnpm install vue3-pdfjs
我們?cè)?src 下新建一個(gè)文件 src/components/pdfPreview.vue,添加一些代碼,初始化 vue-pdf 預(yù)覽,代碼如下
<template> <div class="pdf-preview"> </div> </template> <script setup lang="ts"> import { reactive, onMounted, computed } from "vue"; const props = defineProps({ pdfUrl: { type: String, required: true } }) onMounted(() => { }); </script> <style lang="css" scoped> .pdf-preview { position: relative; height: 100vh; padding: 20px 0; box-sizing: border-box; background: rgb(66, 66, 66); } .vue-pdf-embed { text-align: center; width: 515px; border: 1px solid #e5e5e5; margin: 0 auto; box-sizing: border-box; } </style>
添加完成后,我們將 PDF 預(yù)覽組件引入到 App.vue 文件中,并將提前準(zhǔn)備的 PDF 文件也引入,如下所示
<template> <div> <PDFView :pdfUrl="jsPdf" /> </div> </template> <script setup lang="ts"> import PDFView from "./components/pdfPreview.vue" import jsPdf from "./Javascript.pdf" </script>
接下來(lái)我們回到剛剛新建的 PDF 預(yù)覽組件頁(yè)面,來(lái)完善預(yù)覽功能
我們先引入 PDF 預(yù)覽插件
import VuePdfEmbed from "vue-pdf-embed"; import { createLoadingTask } from "vue3-pdfjs/esm"; // 獲得總頁(yè)數(shù)
使用 vue3 的 reactive
定義一些頁(yè)數(shù),頁(yè)碼,PDF文件預(yù)覽地址變量
const state = reactive({ source: props.pdfUrl, 預(yù)覽pdf文件地址 pageNum: 1, 當(dāng)前頁(yè)面 scale: 1, // 縮放比例 numPages: 0, // 總頁(yè)數(shù) });
在 OnMounted 鉤子函數(shù)中使用createLoadingTask 獲取下 預(yù)覽文件的總頁(yè)數(shù)
const loadingTask = createLoadingTask(state.source); loadingTask.promise.then((pdf:{numPages: number}) => { state.numPages = pdf.numPages; });
在template中加入預(yù)覽插件代碼
<div class="pdf-wrap"> <vue-pdf-embed :source="state.source" :style="scaleFun" class="vue-pdf-embed" :page="state.pageNum" /> </div>
打開瀏覽器,可以看到 PDF 文件已經(jīng)加載出來(lái)了,但是樣式不是很好看,我們下一步將樣式優(yōu)化下,并將 PDF 文件的翻頁(yè)功能,縮放功能,當(dāng)前頁(yè)面/總頁(yè)數(shù)展示功能添加完善
添加如下代碼到 tempate 中
<div class="page-tool"> <div class="page-tool-item" >上一頁(yè)</div> <div class="page-tool-item">下一頁(yè)</div> <div class="page-tool-item">{{state.pageNum}}/{{state.numPages}}</div> <div class="page-tool-item" >放大</div> <div class="page-tool-item">縮小</div> </div>
美化樣式
.pdf-preview { position: relative; height: 100vh; padding: 20px 0; box-sizing: border-box; background-color: e9e9e9; } .pdf-wrap{ overflow-y:auto ; } .vue-pdf-embed { text-align: center; width: 515px; border: 1px solid #e5e5e5; margin: 0 auto; box-sizing: border-box; } .page-tool { position: absolute; bottom: 35px; padding-left: 15px; padding-right: 15px; display: flex; align-items: center; background: rgb(66, 66, 66); color: white; 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; }
添加文件的翻頁(yè)功能,縮放功能,當(dāng)前頁(yè)面/總頁(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 > 1) { state.scale -= 0.1; } }
tempalte
<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>
到這里,vue3 PDF 文件預(yù)覽功能我們已經(jīng)做完了,
總結(jié)
到此這篇關(guān)于vue3如何實(shí)現(xiàn)PDF文件在線預(yù)覽功能的文章就介紹到這了,更多相關(guān)vue3 PDF文件在線預(yù)覽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue中如何實(shí)現(xiàn)pdf文件預(yù)覽的方法
- Vue中如何實(shí)現(xiàn)在線預(yù)覽word文件、excel文件
- vue實(shí)現(xiàn)在線預(yù)覽pdf文件和下載(pdf.js)
- vue使用pdf.js預(yù)覽pdf文件的方法
- vue預(yù)覽 pdf、word、xls、ppt、txt文件的實(shí)現(xiàn)方法
- Vue3 + Vue-PDF 實(shí)現(xiàn)PDF 文件在線預(yù)覽實(shí)戰(zhàn)
- Vue實(shí)現(xiàn)在線預(yù)覽pdf文件功能(利用pdf.js/iframe/embed)
- Vue實(shí)現(xiàn)預(yù)覽docx/xlsx/pdf等類型文件功能
- vue3中各種類型文件進(jìn)行預(yù)覽功能實(shí)例
- vue中實(shí)現(xiàn)支持txt,docx,xlsx,mp4格式文件預(yù)覽功能(純前端)
相關(guān)文章
vue中使用input[type="file"]實(shí)現(xiàn)文件上傳功能
這篇文章主要介紹了vue中使用input[type="file"]實(shí)現(xiàn)文件上傳功能,實(shí)現(xiàn)代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09Django Vue實(shí)現(xiàn)動(dòng)態(tài)菜單和動(dòng)態(tài)權(quán)限
本文主要介紹了Django Vue實(shí)現(xiàn)動(dòng)態(tài)菜單和動(dòng)態(tài)權(quán)限,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06el-form 多層級(jí)表單的實(shí)現(xiàn)示例
這篇文章主要介紹了el-form 多層級(jí)表單的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09簡(jiǎn)易vuex4核心原理及實(shí)現(xiàn)源碼分析
這篇文章主要為大家介紹了簡(jiǎn)易vuex4核心原理及實(shí)現(xiàn)源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01在vue中實(shí)現(xiàn)清除echarts上次保留的數(shù)據(jù)(親測(cè)有效)
這篇文章主要介紹了在vue中實(shí)現(xiàn)清除echarts上次保留的數(shù)據(jù)(親測(cè)有效),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09