Rendering react as pdf is generally a pain, but there is a way around it using canvas.

The idea is to convert : HTML -> Canvas -> PNG (or JPEG) -> PDF

To achieve the above, you'll need :

  1. html2canvas &
  2. jsPDF

import React, {Component, PropTypes} from 'react';

// download html2canvas and jsPDF and save the files in app/ext, or somewhere else
// the built versions are directly consumable
// import {html2canvas, jsPDF} from 'app/ext';


export default class Export extends Component {
  constructor(props) {
    super(props);
  }

  printDocument() {
    const input = document.getElementById('divToPrint');
    html2canvas(input)
      .then((canvas) => {
        const imgData = canvas.toDataURL('image/png');
        const pdf = new jsPDF();
        pdf.addImage(imgData, 'JPEG', 0, 0);
        // pdf.output('dataurlnewwindow');
        pdf.save("download.pdf");
      })
    ;
  }

  render() {
    return (<div>
      <div className="mb5">
        <button onClick={this.printDocument}>Print</button>
      </div>
      <div id="divToPrint" className="mt4" {...css({
        backgroundColor: '#f5f5f5',
        width: '210mm',
        minHeight: '297mm',
        marginLeft: 'auto',
        marginRight: 'auto'
      })}>
        <div>Note: Here the dimensions of div are same as A4</div> 
        <div>You Can add any component here</div>
      </div>
    </div>);
  }
}

The snippet will not work here because the required files are not imported.

An alternate approach is being used in this answer, where the middle steps are dropped and you can simply convert from HTML to PDF. There is an option to do this in the jsPDF documentation as well, but from personal observation, I feel that better accuracy is achieved when dom is converted into png first.

Update 0: September 14, 2018

The text on the pdfs created by this approach will not be selectable. If that's a requirement, you might find this article helpful.

Answer from Shivek Khurana on Stack Overflow
🌐
React-pdf
react-pdf.org
React-pdf
React renderer for creating PDF files on the browser and server
Styling
React renderer for creating PDF files on the browser and server
Advanced
If you need to render documents ... in React can occupy the browser's main thread for a long time. This can lead to unresponsive UI and browsers offering the user to abort the script. To avoid this, you should render large documents inside a web worker. Web workers are executed in separate threads, and therefore do not block the main thread of the browser. This way, the UI can stay responsive while the PDF is being rendered. For an example on how to ...
Components
React renderer for creating PDF files on the browser and server
Rendering process
React renderer for creating PDF files on the browser and server
🌐
npm
npmjs.com › package › react-to-pdf
react-to-pdf - npm
You probably will not need this ... can use a function to return the target element besides using React refs const getTargetElement = () => document.getElementById('content-id'); const Component = () => { return ( <div> <button ...
      » npm install react-to-pdf
    
Published   Nov 25, 2025
Version   2.0.3
Author   Marcos Andrei Ivanechtchuk
🌐
GitHub
github.com › wojtekmaj › react-pdf
GitHub - wojtekmaj/react-pdf: Display PDFs in your React app as easily as if they were images.
Check the sample directory in this repository for a full working example. For more examples and more advanced use cases, check Recipes in React-PDF Wiki. If you want to use annotations (e.g.
Starred by 10.7K users
Forked by 981 users
Languages   TypeScript 94.3% | CSS 5.6% | HTML 0.1%
🌐
Nutrient
nutrient.io › blog › sdk › how to create pdfs with react to pdf
How to create PDFs with React to PDF
June 17, 2025 - Create a simple component that lets users download content as a PDF: ... The code uses the usePDF hook from react-to-pdf to generate a PDF from a specific section of a React component.
🌐
CodeSandbox
codesandbox.io › examples › package › react-pdf
react-pdf examples - CodeSandbox
Use this online react-pdf playground to view and fork react-pdf example apps and templates on CodeSandbox.
Top answer
1 of 13
168

Rendering react as pdf is generally a pain, but there is a way around it using canvas.

The idea is to convert : HTML -> Canvas -> PNG (or JPEG) -> PDF

To achieve the above, you'll need :

  1. html2canvas &
  2. jsPDF

import React, {Component, PropTypes} from 'react';

// download html2canvas and jsPDF and save the files in app/ext, or somewhere else
// the built versions are directly consumable
// import {html2canvas, jsPDF} from 'app/ext';


export default class Export extends Component {
  constructor(props) {
    super(props);
  }

  printDocument() {
    const input = document.getElementById('divToPrint');
    html2canvas(input)
      .then((canvas) => {
        const imgData = canvas.toDataURL('image/png');
        const pdf = new jsPDF();
        pdf.addImage(imgData, 'JPEG', 0, 0);
        // pdf.output('dataurlnewwindow');
        pdf.save("download.pdf");
      })
    ;
  }

  render() {
    return (<div>
      <div className="mb5">
        <button onClick={this.printDocument}>Print</button>
      </div>
      <div id="divToPrint" className="mt4" {...css({
        backgroundColor: '#f5f5f5',
        width: '210mm',
        minHeight: '297mm',
        marginLeft: 'auto',
        marginRight: 'auto'
      })}>
        <div>Note: Here the dimensions of div are same as A4</div> 
        <div>You Can add any component here</div>
      </div>
    </div>);
  }
}

The snippet will not work here because the required files are not imported.

An alternate approach is being used in this answer, where the middle steps are dropped and you can simply convert from HTML to PDF. There is an option to do this in the jsPDF documentation as well, but from personal observation, I feel that better accuracy is achieved when dom is converted into png first.

Update 0: September 14, 2018

The text on the pdfs created by this approach will not be selectable. If that's a requirement, you might find this article helpful.

2 of 13
19

@react-pdf/renderer is a great resource for this.

It is a bit time consuming converting your markup and CSS to React-PDF's format, but it is easy to understand. Exporting a PDF and from it is fairly straightforward.

To allow a user to download a PDF generated by react-PDF, use their on the fly rendering, which provides a customizable download link. When clicked, the site renders and downloads the PDF for the user.

Here's their REPL which will familiarize you with the markup and styling required. They have a download link for the PDF too, but they don't show the code for that here.

🌐
Medium
medium.com › @ryanmambou › how-to-generate-a-pdf-file-from-a-react-component-87707e869296
How to Generate a PDF file from a React Component | by Ryan Mambou | Medium
August 29, 2024 - Hello my brothers and sisters from the internet, I encountered a problem a few months ago where I had to print a card component I created in react to a pdf file. Didn’t have the slightest clue on how to do it but after some searching, I found a solution. The task may sound scary at first but with a little bit of searching and thinking, you’ll always figure it out.
🌐
GitHub
github.com › diegomura › react-pdf
GitHub - diegomura/react-pdf: 📄 Create PDF files using React
import React from 'react'; import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer'; // Create styles const styles = StyleSheet.create({ page: { flexDirection: 'row', backgroundColor: '#E4E4E4', }, section: { margin: 10, ...
Starred by 16.2K users
Forked by 1.3K users
Languages   TypeScript 83.1% | JavaScript 16.8%
Find elsewhere
🌐
npm
npmjs.com › package › react-pdf
react-pdf - npm
Check the sample directory in this repository for a full working example. For more examples and more advanced use cases, check Recipes in React-PDF Wiki. If you want to use annotations (e.g.
      » npm install react-pdf
    
Published   Oct 09, 2025
Version   10.2.0
Author   Wojciech Maj
🌐
Nutrient
nutrient.io › blog › sdk › how to build a reactjs pdf viewer with react pdf
React PDF viewer: Complete guide to building with react-pdf in 2025
August 12, 2025 - You can use our demo document as an example; you just need to rename it to document.pdf. react-pdf comes with two components: Document and Page. Document is used to open a PDF and is mandatory. Within the document, you can mount pages, which are used to render the PDF page.
🌐
CodeSandbox
codesandbox.io › examples › package › react-to-pdf
react-to-pdf examples - CodeSandbox
Use this online react-to-pdf playground to view and fork react-to-pdf example apps and templates on CodeSandbox.
🌐
DEV Community
dev.to › jaymeeu › how-to-generate-custom-pdf-using-react-and-react-pdf-6d4
How to Generate Custom PDF, Using React and React-PDF. - DEV Community
February 12, 2024 - The image below shows the screenshot of a sample PDF invoice we will design for this project. The image below shows a preview of this project. Each invoice sample has download, print, and share buttons. ... Basic knowledge of JavaScript and ReactJS. Knowledge of CSS Flexbox property. The source code of the project is available on GitHub. To get started with React.
🌐
YouTube
youtube.com › watch
How to generate PDF in React using React pdf - YouTube
Check out MiniTool MovieMaker: https://bit.ly/4hKFA9Agenerate PDF use html2canvas and jsPDF: https://youtu.be/gkCaIyBPnd8In this video, I’ll show you how to ...
Published   December 18, 2024
🌐
GitHub
github.com › ivmarcos › react-to-pdf › blob › main › examples › ExampleAdvanced.tsx
react-to-pdf/examples/ExampleAdvanced.tsx at main · ivmarcos/react-to-pdf
import generatePDF, { Resolution, Margin, Options } from "react-to-pdf"; import { Card } from "./Card"; import { Button } from "./Button"; import { Container } from "./Container"; · const options: Options = { filename: "advanced-example.pdf", // default is `save` method: "save", // default is Resolution.MEDIUM = 3, which should be enough, higher values ·
Author   ivmarcos
🌐
GitHub
github.com › ivmarcos › react-to-pdf
GitHub - ivmarcos/react-to-pdf: Generate pdf from react components
import { usePDF } from 'react-to-pdf'; const Component = () => { const { toPDF, targetRef } = usePDF({filename: 'page.pdf'}); return ( <div> <button onClick={() => toPDF()}>Download PDF</button> <div ref={targetRef}> Content to be generated ...
Starred by 329 users
Forked by 67 users
Languages   TypeScript 93.2% | JavaScript 5.7% | HTML 1.1%
🌐
LogRocket
blog.logrocket.com › home › generating pdfs in react with react-pdf
Generating PDFs in React with react-pdf - LogRocket Blog
January 24, 2025 - Explore how to implement a PDF document generation feature in your React app using react-pdf and other popular PDF generation tools.
🌐
Wojtekmaj
projects.wojtekmaj.pl › react-pdf
React-PDF
Easily display PDFs in your React app.
🌐
React PDF Viewer
react-pdf-viewer.dev
A React component to view PDF documents - React PDF Viewer
React PDF Viewer · Docs · Plugins · Localizations · Examples · UpdatesPurchase · You can drag and drop a PDF document to the demo area below · // Core viewer · import { Viewer } from '@react-pdf-viewer/core'; // Plugins · import { ...