Vue3實(shí)現(xiàn)pdf在線預(yù)覽的三種方式
今天一天對當(dāng)前可用的pdf預(yù)覽插件做了測試,主要需求是只能預(yù)覽不能下載,但對于前端來說,沒有絕對的禁止,這里只羅列實(shí)現(xiàn)方式。
目前采用vue3版本為:3.2.37
- iframe
- vue-office
- pdfjs-dist
iframe
先說最簡單的,iframe可以直接展示pdf文件,所以如果不作禁止預(yù)覽等操作,iframe是最合適的。
<el-dialog
v-model="previewOtherUpload"
reset-drag-position
draggable
sticky
:title="_options.imgName || '詳情'"
footer-hide
class-name="vertical-center-modal"
>
<div
@contextmenu.prevent
style="user-select: none;"
>
<iframe
ref="iframe"
:src="`${modelValue}#toolbar=0`"
width="100%"
height="600px"
@load="onIframeLoad"
>
</iframe>
</div>
</el-dialog>
<script setup>
const modelValue = ref('https://501351981.github.io/vue-office/examples/dist/static/test-files/test.pdf')
let previewOtherUpload = ref(false);
const iframe = ref(null)
const clickShow = () => {
previewOtherUpload.value = true;
}
// 嘗試在iframe加載完畢后,進(jìn)行右鍵禁用,但實(shí)際需要通過postmessage來處理,所以這里無實(shí)際用處
const onIframeLoad = () => {
try {
console.log('iframe 已加載', iframe.value.contentWindow.window);
if (iframe.value.contentWindow.document) {
iframe.value.contentWindow.document.addEventListener('contextmenu', (e) => e.preventDefault());
}
} catch (error) {
console.error('無法訪問 iframe 內(nèi)容:', error);
}
}
</script>

vue-office
安裝
#docx文檔預(yù)覽組件 npm install @vue-office/docx vue-demi@0.14.6 #excel文檔預(yù)覽組件 npm install @vue-office/excel vue-demi@0.14.6 #pdf文檔預(yù)覽組件 npm install @vue-office/pdf vue-demi@0.14.6 #pptx文檔預(yù)覽組件 npm install @vue-office/pptx vue-demi@0.14.6
如果是vue2.6版本或以下還需要額外安裝 @vue/composition-api
npm install @vue/composition-api
我們?nèi)绻活A(yù)覽pdf,則安裝 npm install @vue-office/pdf vue-demi@0.14.6
<el-dialog
v-model="previewOtherUpload"
reset-drag-position
draggable
sticky
:title="_options.imgName || '詳情'"
footer-hide
class-name="vertical-center-modal"
>
<div
@contextmenu.prevent
style="user-select: none;"
>
<VueOfficePdf
:src="modelValue"
/>
</div>
</el-dialog>
<script setup>
import VueOfficePdf from '@vue-office/pdf'
const modelValue = ref('https://501351981.github.io/vue-office/examples/dist/static/test-files/test.pdf')
let previewOtherUpload = ref(false);
const clickShow = () => {
previewOtherUpload.value = true;
}
</script>

pdfjs-dist
這是目前最麻煩的一個(gè)插件,一定先確定下載的版本"pdfjs-dist": “2.16.105”,我用的是這個(gè),否則下面的workerSrc設(shè)置會(huì)有問題。
<el-dialog
v-model="previewOtherUpload"
reset-drag-position
draggable
sticky
:title="_options.imgName || '詳情'"
footer-hide
class-name="vertical-center-modal"
>
<div
id="pdf-view"
@contextmenu.prevent
style="user-select: none;"
>
<canvas v-for="page in state.pdfPages" :key="page" id="pdfCanvas" />
<div id="text-view"></div>
</div>
</el-dialog>
<script setup>
import { computed, reactive, ref, watch, nextTick } from "vue";
import * as pdfjsViewer from 'pdfjs-dist/web/pdf_viewer.js'
import 'pdfjs-dist/web/pdf_viewer.css'
import * as PDF from 'pdfjs-dist'
// 設(shè)置 pdf.worker.js 路徑
PDF.GlobalWorkerOptions.workerSrc = '../../../node_modules/pdfjs-dist/build/pdf.worker.js';
let pdfDoc = null;
const modelValue = ref('https://501351981.github.io/vue-office/examples/dist/static/test-files/test.pdf')
let previewOtherUpload = ref(false);
const clickShow = () => {
loadFile(modelValue)
previewOtherUpload.value = true;
}
const loadFile = (url) => {
PDF.getDocument({
url,
cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.16.105/cmaps/',
cMapPacked: true,
}).promise.then((pdf) => {
pdfDoc = pdf
// 獲取pdf文件總頁數(shù)
state.pdfPages = pdf.numPages
nextTick(() => {
renderPage(1) // 從第一頁開始渲染
})
})
}
const renderPage = (num) => {
pdfDoc.getPage(num).then((page) => {
const canvas = document.getElementById('pdfCanvas')
const ctx = canvas.getContext('2d')
const viewport = page.getViewport({ scale: state.pdfScale })
canvas.width = viewport.width
canvas.height = viewport.height
const renderContext = {
canvasContext: ctx,
viewport
}
page.render(renderContext)
})
}
</script>
插件樣式也不好調(diào)整,不推薦。
總結(jié)
最后還是使用了第二種方式,作為禁止下載的展示。
以上就是Vue3實(shí)現(xiàn)pdf在線預(yù)覽的三種方式的詳細(xì)內(nèi)容,更多關(guān)于Vue3在線預(yù)覽pdf的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
ElementUI修改實(shí)現(xiàn)更好用圖片上傳預(yù)覽組件
這篇文章主要為大家介紹了ElementUI修改實(shí)現(xiàn)更好用圖片上傳預(yù)覽組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
在Vue項(xiàng)目中集成和使用Lottie動(dòng)畫庫的步驟詳解
Lottie 是一個(gè)由 Airbnb 開源的動(dòng)畫庫,它允許你在 Web、iOS、Android 等平臺上使用體積小、高性能的體驗(yàn)豐富的矢量動(dòng)畫,本文將詳細(xì)介紹在 Vue 項(xiàng)目中如何集成和使用 Lottie,文中有詳細(xì)的代碼講解,需要的朋友可以參考下2023-11-11
Vue3系列之effect和ReactiveEffect?track?trigger源碼解析
這篇文章主要為大家介紹了Vue3系列之effect和ReactiveEffect?track?trigger源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
使用 Vue cli 3.0 構(gòu)建自定義組件庫的方法
本文旨在給大家提供一種構(gòu)建一個(gè)完整 UI 庫腳手架的思路。通過實(shí)例代碼給大家講解了使用 Vue cli 3.0 構(gòu)建自定義組件庫的方法,感興趣的朋友跟隨小編一起看看吧2019-04-04
前端vue+elementUI如何實(shí)現(xiàn)記住密碼功能
這篇文章主要給大家介紹了關(guān)于vue+elementUI如何實(shí)現(xiàn)記住密碼功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

