import { useFullscreen } from "ahooks"; import { Button, message, Modal, Spin } from "antd"; import { useRef, useState } from "react"; import { Document, Page, pdfjs } from "react-pdf"; import useDownloadFile from "../../hooks/useDownloadFile"; import { getFileUrl } from "../../utils"; import "react-pdf/dist/Page/AnnotationLayer.css"; import "react-pdf/dist/Page/TextLayer.css"; import "./index.less"; /** * PDF查看组件 */ function Pdf(props) { const { visible = false, onCancel, file, name, inline = false, style = {}, } = props; const fileUrl = getFileUrl(); const [numPages, setNumPages] = useState(0); const [pdfWidth, setPdfWidth] = useState(600); const [loading, setLoading] = useState(true); const fullscreenRef = useRef(null); const [isFullscreen, { enterFullscreen, exitFullscreen }] = useFullscreen(fullscreenRef); const { downloadFile } = useDownloadFile(); pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`; const onDocumentLoadSuccess = ({ numPages }) => { setNumPages(numPages); setLoading(false); }; const onDocumentLoadError = () => { setLoading(false); message.error("加载 PDF 文件失败"); if (onCancel) onCancel(); }; const onPageLoadSuccess = ({ width }) => { setPdfWidth(width); }; // 内联模式的PDF内容 const renderPdfContent = () => ( <> {loading && (