Vue3實現(xiàn)pdf在線預覽的三種方式
今天一天對當前可用的pdf預覽插件做了測試,主要需求是只能預覽不能下載,但對于前端來說,沒有絕對的禁止,這里只羅列實現(xiàn)方式。
目前采用vue3版本為:3.2.37
- iframe
- vue-office
- pdfjs-dist
iframe
先說最簡單的,iframe可以直接展示pdf文件,所以如果不作禁止預覽等操作,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加載完畢后,進行右鍵禁用,但實際需要通過postmessage來處理,所以這里無實際用處 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文檔預覽組件 npm install @vue-office/docx vue-demi@0.14.6 #excel文檔預覽組件 npm install @vue-office/excel vue-demi@0.14.6 #pdf文檔預覽組件 npm install @vue-office/pdf vue-demi@0.14.6 #pptx文檔預覽組件 npm install @vue-office/pptx vue-demi@0.14.6
如果是vue2.6版本或以下還需要額外安裝 @vue/composition-api
npm install @vue/composition-api
我們?nèi)绻活A覽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
這是目前最麻煩的一個插件,一定先確定下載的版本"pdfjs-dist": “2.16.105”,我用的是這個,否則下面的workerSrc設(shè)置會有問題。
<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實現(xiàn)pdf在線預覽的三種方式的詳細內(nèi)容,更多關(guān)于Vue3在線預覽pdf的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue?proxytable代理根路徑的同時增加其他代理方式
這篇文章主要介紹了vue?proxytable代理根路徑的同時增加其他代理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04element表格數(shù)據(jù)部分模糊的實現(xiàn)代碼
這篇文章給大家介紹了element表格數(shù)據(jù)模糊的實現(xiàn)代碼,文中有詳細的效果展示和實現(xiàn)代碼供大家參考,具有一定的參考價值,需要的朋友可以參考下2024-01-01一文詳解Vue的響應(yīng)式原則與雙向數(shù)據(jù)綁定
使用 Vue.js 久了,還是不明白響應(yīng)式原理和雙向數(shù)據(jù)綁定的區(qū)別?今天,我們就一起來學習一下,將解釋它們的區(qū)別,快跟隨小編一起學習學習吧2022-08-08