React pdf를 사용하여 React에 온라인/오프라인 pdf 파일 표시
npm i react-pdf
import { Document, Page, pdfjs } from 'react-pdf'
// right after your imports
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`
<Document
file={{ url: urlPdf }}
onLoadSuccess={onDocumentLoadSuccess}
>
// pages here, we will get back to that later
</Document>
위 코드에서 urlPDF는 pdf의 URL입니다.이 자원은 CORS에서 차단할 수 없습니다.그렇지 않으면 pdf를 보여줄 수 없습니다.만약 자원을 크로스 소스에 사용할 수 없다면, 그것을 사용하십시오. (단지 임시 해결 방안일 뿐, 생산에서 사용하지 마십시오.)
file={{ url: `https://cors-anywhere.herokuapp.com/${urlPdf}` }}
const[totalPages,setTotalPages]=useState(null)
const onDocumentLoadSuccess=({numPages})=>setTotalPages(numPages)
<Document
file={{ url: attachment.url }}
onLoadSuccess={onDocumentLoadSuccess}
>
{
Array.apply(null, Array(totalPages))
.map((x, i) => i + 1)
.map((page) => <Page pageNumber={page} />)
}
</Document>
https://github.com/wojtekmaj/react-pdf
Reference
이 문제에 관하여(React pdf를 사용하여 React에 온라인/오프라인 pdf 파일 표시), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mohitkyadav/display-online-offline-pdf-files-in-react-using-react-pdf-2482텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)