React to PDF
react-to-pdf is a JavaScript/TypeScript library designed to convert React components into PDF documents by rendering them as images within the PDF. The current stable version is 3.2.2. The project's release cadence is closely tied to its underlying dependency, jsPDF, often releasing minor or patch updates following jsPDF's security and feature releases, with major versions of react-to-pdf typically aligning with major jsPDF upgrades. A key differentiator is its simplicity and direct approach: it captures a screenshot of the React component rather than generating a vectorized PDF, which means the content is rasterized and not scalable like true vector graphics. This also means it does not support Server-Side Rendering (SSR). For vectorized or more advanced PDF generation from React, the library explicitly recommends alternatives like `@react-pdf/renderer` or `react-pdf` (for display).
Common errors
-
Content in PDF appears blurry or pixelated.
cause The default `resolution` setting might be too low for the content or target display density, as the PDF is generated from an image capture.fixIncrease the `resolution` option (e.g., `Resolution.HIGH` or a higher numeric value like `6` or `10`) in the `usePDF` hook or `generatePDF` options. Be aware that higher resolutions increase PDF file size and generation time. -
ReferenceError: require is not defined or Module parse failed: 'import' and 'export' may only appear at the top level.
cause Attempting to use CommonJS `require()` syntax or incorrect ES module syntax in a modern JavaScript project configured for ES modules.fixEnsure you are using ES module `import` statements for `react-to-pdf`. For example, use `import { usePDF } from 'react-to-pdf';` or `import generatePDF from 'react-to-pdf';`. -
PDF is blank, incomplete, or missing styling/images.
cause The target React component might not have fully rendered, or external resources (images, fonts, complex CSS) might not have loaded before the PDF generation function was called. CORS issues can also prevent image loading.fixEnsure the target element is fully mounted and all asynchronous content (images, fonts) has loaded. Use the `overrides.canvas.useCORS: true` option if images are from a different origin. Implement a delay or wait for critical resources to load before triggering PDF generation. -
TypeError: (0, react_to_pdf__WEBPACK_IMPORTED_MODULE_0__.generatePDF) is not a function
cause Incorrect import syntax for a default export. `generatePDF` is a default export, but it's being imported as a named export.fixCorrect the import statement for `generatePDF` to `import generatePDF from 'react-to-pdf';`. Named exports like `usePDF`, `Resolution`, and `Margin` correctly use curly braces.
Warnings
- breaking Version 3.0.0 introduced significant changes by upgrading to `jsPDF v4`, which dropped support for Internet Explorer. This also included various dependency updates and security fixes. Projects targeting older browsers need to either remain on an earlier `react-to-pdf` version or use polyfills for `jsPDF`.
- breaking Version 2.0.0 marked a major release, aligning with `jsPDF v3`'s breaking changes. This version dropped support for Internet Explorer and included security fixes for `jsPDF`'s `html` function by updating `dompurify`. While no other direct breaking changes were noted, the IE drop is significant.
- gotcha The generated PDFs are not vectorized; they are created from a screenshot of the React component. This means the output is an image, not scalable vector graphics. Text in the PDF will not be selectable or searchable as native text.
- gotcha The library does not support Server-Side Rendering (SSR). It relies on browser-specific APIs to capture the component's appearance, making it unsuitable for Node.js-only environments.
- breaking Multiple versions have addressed security vulnerabilities originating from the underlying `jsPDF` library (e.g., `v3.2.0`, `v3.1.0`, `v3.0.0`, `v2.0.2`, `v2.0.0`) and other dependencies like `tar-fs` (`v2.0.1`). Regularly updating to the latest stable version is critical to mitigate these risks.
Install
-
npm install react-to-pdf -
yarn add react-to-pdf -
pnpm add react-to-pdf
Imports
- usePDF
const { usePDF } = require('react-to-pdf')import { usePDF } from 'react-to-pdf' - generatePDF
import { generatePDF } from 'react-to-pdf'import generatePDF from 'react-to-pdf'
- Resolution, Margin
import { Resolution, Margin } from 'react-to-pdf'
Quickstart
import { usePDF } from 'react-to-pdf';
const MyPdfDocument = () => {
const { toPDF, targetRef } = usePDF({
filename: 'my-react-component.pdf',
page: {
margin: 10, // in mm
format: 'A4',
orientation: 'portrait'
}
});
return (
<div>
<button onClick={() => toPDF()}>Download PDF</button>
<div ref={targetRef} style={{
padding: '20px',
border: '1px solid #ccc',
borderRadius: '8px',
fontFamily: 'Arial, sans-serif'
}}>
<h1>Hello from React to PDF!</h1>
<p>This content will be converted into a PDF document.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<p>Current date: {new Date().toLocaleDateString()}</p>
</div>
</div>
);
};
export default MyPdfDocument;