前端實(shí)現(xiàn)pdf文件預(yù)覽的操作步驟方法
1. 使用 PDF.js 進(jìn)行自定義預(yù)覽
PDF.js 是一個(gè)用 JavaScript 實(shí)現(xiàn)的 PDF 閱讀器,可以嵌入到網(wǎng)頁(yè)中進(jìn)行 PDF 文件的預(yù)覽,并且可以自定義其功能。
步驟如下:
1. 引入 PDF.js 庫(kù)
import pdfjsLib from 'pdfjs-dist'; import 'pdfjs-dist/web/pdf_viewer';
2. 創(chuàng)建 PDF 預(yù)覽組件
import React, { useEffect, useRef } from 'react'; import pdfjsLib from 'pdfjs-dist'; import 'pdfjs-dist/web/pdf_viewer'; const PdfPreview = ({ url }) => { const pdfContainer = useRef(null); useEffect(() => { // 配置 PDF.js pdfjsLib.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjsLib.version}/pdf.worker.js`; // 加載 PDF const loadingTask = pdfjsLib.getDocument(url); loadingTask.promise.then((pdf) => { // 獲取第一頁(yè) pdf.getPage(1).then((page) => { const viewport = page.getViewport({ scale: 1.5 }); const canvas = document.createElement('canvas'); const context = canvas.getContext('2d'); canvas.height = viewport.height; canvas.width = viewport.width; const renderContext = { canvasContext: context, viewport: viewport }; page.render(renderContext).promise.then(() => { pdfContainer.current.appendChild(canvas); }); }); }); }, [url]); return <div ref={pdfContainer} style={{ width: '100%', height: '600px' }}></div>; }; export default PdfPreview;
3. 使用
if (record.name.endsWith('.pdf')) { const baseUrl = new URL(API_BASE_URL_PRO).origin; const fileUrl = `${baseUrl.replace(/:\d+$/, ':3100')}/api/common/file/download?fileId=${record.fileId}&bucketName=dataset`; const previewModal = Modal.info({ title: ( <> 文件預(yù)覽 <span style={{ fontSize: '14px', textDecoration: 'underline', marginLeft: '15px' }} > {record.name} </span> </> ), content: <PdfPreview url={fileUrl} />, footer: null, width: '75%', style: { top: 35 }, closable: true, onCancel: () => previewModal.destroy(), }); return; }
2. 使用 iframe 嵌入并自定義參數(shù)
如果不想使用 PDF.js,也可以通過(guò) iframe 嵌入 PDF 文件,并通過(guò)特定參數(shù)來(lái)禁用下載功能。
步驟如下:
當(dāng)檢測(cè)到文件為 PDF 時(shí),使用 iframe 進(jìn)行嵌入,并添加 #toolbar=0
參數(shù)來(lái)禁用工具欄(包括下載按鈕)。
if (record.name.endsWith('.pdf')) { const pdfFileUrl= new URL(API_BASE_URL_PRO).origin; // window.open(pdfFileUrl, '_blank'); 或者使用打開(kāi)新窗口方式 const fileUrl = `${baseUrl.replace(/:\d+$/, ':3100')}/api/common/file/download?fileId=${record.fileId}&bucketName=dataset#toolbar=0`; const previewModal = Modal.info({ title: ( <> 文件預(yù)覽 <span style={{ fontSize: '14px', textDecoration: 'underline', marginLeft: '15px' }} > {record.name} </span> </> ), content: ( <iframe src={pdfFileUrl} style={{ width: '100%', height: '600px', border: 'none' }} ></iframe> ), footer: null, width: '75%', style: { top: 35 }, closable: true, onCancel: () => previewModal.destroy(), }); return; }
選擇適合你項(xiàng)目需求的方法進(jìn)行實(shí)現(xiàn)即可。如果你希望有更高的定制性和更好的用戶體驗(yàn),推薦使用 PDF.js。如果你希望實(shí)現(xiàn)簡(jiǎn)單快捷,可以選擇 iframe 方法。
總結(jié)
到此這篇關(guān)于前端實(shí)現(xiàn)pdf文件預(yù)覽操作步驟方法的文章就介紹到這了,更多相關(guān)前端pdf文件預(yù)覽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用JS實(shí)現(xiàn)氣泡跟隨鼠標(biāo)移動(dòng)的動(dòng)畫(huà)效果
氣泡跟隨鼠標(biāo)移動(dòng),并在每次點(diǎn)擊時(shí)產(chǎn)生不同的變化,效果非常迷人,下面小編給大家?guī)?lái)了,基于js實(shí)現(xiàn)的氣泡效果實(shí)例代碼,需要的朋友參考下吧2017-09-09javascript檢測(cè)頁(yè)面是否縮放的小例子
簡(jiǎn)單寫(xiě)了下,只測(cè)試了chrome,道理很簡(jiǎn)單,其他瀏覽器自行擴(kuò)充。2013-05-05javascript+ajax實(shí)現(xiàn)產(chǎn)品頁(yè)面加載信息
本文給大家分享的是使用javascript結(jié)合ajax實(shí)現(xiàn)產(chǎn)品頁(yè)面無(wú)刷新加載信息的代碼,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-07-07JavaScript簡(jiǎn)單表格編輯功能實(shí)現(xiàn)方法
這篇文章主要介紹了JavaScript簡(jiǎn)單表格編輯功能實(shí)現(xiàn)方法,涉及javascript操作表格的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04DataGear開(kāi)發(fā)基于three.js的3D數(shù)據(jù)可視化看板的詳細(xì)代碼
DataGear?支持采用原生的HTML、JavaScript、CSS制作數(shù)據(jù)可視化看板,也支持導(dǎo)入由npm、vite等前端工具構(gòu)建的前端程序包,這篇文章主要介紹了DataGear制作基于three.js的3D數(shù)據(jù)可視化看板,需要的朋友可以參考下2024-02-02javascript設(shè)計(jì)模式之解釋器模式詳解
這篇文章主要介紹了javascript設(shè)計(jì)模式之解釋器模式詳解,當(dāng)有一個(gè)語(yǔ)言需要解釋執(zhí)行,并且可以將該語(yǔ)言中的句子表示為一個(gè)抽象語(yǔ)法樹(shù)的時(shí)候,可以考慮使用解釋器模式,需要的朋友可以參考下2014-06-06