React to PDF

3.2.2 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to use the `usePDF` hook to generate and download a PDF from a React component's content, including basic page options.

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;

view raw JSON →