React-PDF (currently at v10.4.1) is a JavaScript library for displaying PDF documents within React applications, leveraging Mozilla's PDF.js for rendering. It aims to simplify PDF integration by providing React components like `Document` and `Page`, abstracting the complexities of PDF.js. Major releases, such as v10.0.0, incorporate significant upgrades to the underlying PDF.js library, leading to enhanced stability and performance. The project maintains an active release cadence, with patch and minor updates released frequently to address bugs and introduce new features like customizable page colors and annotation filtering. A key differentiator since v10.1.0 is the support for functions as children within the `Document` component, which streamlines the API by offering direct access to PDF properties (e.g., `numPages`) without requiring manual state management or `onLoadSuccess` handlers, simplifying development. It ships with its own TypeScript definitions, making it well-suited for TypeScript projects.
npm install react-pdfVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic PDF viewer using `Document` and `Page` components with the recommended 'functions as children' pattern (since v10.1.0) for dynamic page rendering and crucial `pdfjs.GlobalWorkerOptions.workerSrc` setup. It includes styling for a presentable viewer.
Review the 'Upgrade guide from version 9.x to 10.x' on the GitHub Wiki. Update import paths (e.g., from `/dist/esm/Page/AnnotationLayer.css` to `/dist/Page/AnnotationLayer.css`). If using Jest, consider migrating to Vitest or configuring Jest for ESM. Ensure your browser/Node.js environment is up-to-date.
Always set `pdfjs.GlobalWorkerOptions.workerSrc` in the component file where you render `<Document>` or `<Page>`. For modern bundlers, use `new URL('pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url).toString()`. If self-hosting, ensure the path is correct and accessible. For Next.js, consider dynamic import with `ssr: false`.Refactor `Document` usage to utilize the 'functions as children' pattern. Instead of `onLoadSuccess`, wrap your `Page` components in a function that receives `{ pdf }` and iterate `Array(pdf.numPages)` to render pages.Upgrade to `react-pdf` version 10.4.1 or newer. If upgrading immediately is not feasible, ensure that any custom HTML generated by `customTextRenderer` is thoroughly sanitized before being rendered to prevent arbitrary script execution.
Verify that `pdfjs.GlobalWorkerOptions.workerSrc` is set correctly to the absolute path of the worker file. For modern bundlers, `new URL('pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url).toString()` is often recommended. If using a CDN, ensure the version matches. Ensure the worker is accessible (e.g., copied to `public` folder in Next.js).Upgrade to at least `react-pdf@9.1.1`, which includes a fix for this specific error. For large PDFs or performance-sensitive applications, ensure efficient component lifecycle management and optimize PDF rendering parameters (e.g., `scale`, `width`, `height`).
Configure Jest to handle ESM by adjusting `transformIgnorePatterns` or enabling experimental ECMAScript module support. The maintainer strongly recommends migrating testing frameworks to Vitest, which has better native ESM support.
If you control the PDF server, configure it to include the `Access-Control-Allow-Origin` header for your domain. If not, consider proxying the PDF request through your backend or hosting the PDF on the same origin as your application. Ensure the server supports Partial Content requests for optimal performance.