import { FunctionComponent, useEffect, useRef, useState } from 'react' import { Document, Page, pdfjs } from 'react-pdf' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import Loading from '../Loading' import DownloadBtn from '../DownloadBtn' pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.min.js` const PDFPreview: FunctionComponent<{ file: any }> = ({ file }) => { const [pageNumber, setPageNumber] = useState(1) const [totalPages, setTotalPages] = useState(0) const [loadingText, setLoadingText] = useState('Loading PDF ...') const [pdfContainerWidth, setPdfContainerWidth] = useState(400) const pdfContainter = useRef(null) const onDocumentLoadSuccess = (pdf: any) => { setTotalPages(pdf.numPages) } useEffect(() => { setPdfContainerWidth(pdfContainter.current ? pdfContainter.current.offsetWidth : 400) }, []) return ( <>
} onLoadProgress={({ loaded, total }) => { setLoadingText(`Loading PDF ${Math.round((loaded / total) * 100)}%`) }} options={{ cMapUrl: `//cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/cmaps/`, cMapPacked: true, }} > 400 ? pdfContainerWidth * 0.8 : pdfContainerWidth} />
Page{' '} { const v = parseInt(e.target.value) if (v <= totalPages && v >= 0) { setPageNumber(v) } }} > /{totalPages}
) } export default PDFPreview