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
🌐
Nutrient
nutrient.io › blog › sdk › how to convert html to pdf using react
Generate PDFs from HTML in React with jsPDF
May 14, 2025 - Immediately after, doc.save("report.pdf") streams the finished document to the browser and prompts the user to download it under the specified filename. * The Nutrient React viewer renders existing PDF content, so external CSS limitations don’t apply. This guide showed you how to turn HTML or JSX into PDFs directly in the browser with jsPDF.
🌐
IronPDF
ironpdf.com › ironpdf blog › node pdf tools › html to pdf react
Convert HTML to PDF in React (Developer Tutorial) | IronPDF
July 29, 2025 - When you click on the Download button, the PDF file will be downloaded. The React-pdf library supports a wide range of styling options, similar to CSS. Here are some common styling properties you can use to customize the appearance of your PDF files: ... IronPDF for Node.js is an excellent alternative for converting HTML to PDF in React.
🌐
Nutrient
nutrient.io › blog › sdk › how to convert html to pdf using html2df and react
HTML to PDF in React: Convert HTML to PDF using html2pdf.js
May 7, 2025 - Learn how to convert HTML to PDF in React using html2pdf.js. This step-by-step guide covers setup, customization options, and PDF download functionality.
🌐
DEV Community
dev.to › hulyamasharipov › how-to-convert-html-to-pdf-using-react-37j4
How to Convert HTML to PDF Using React - DEV Community
November 28, 2023 - In this tutorial, you’ll learn how to convert HTML into PDF using React, one of the most popular JavaScript libraries. To achieve this, you’ll use an open source package called jsPDF, which is a client-side library that doesn’t require ...
🌐
npm
npmjs.com › package › react-pdf-html
react-pdf-html - npm
Html component for react-pdf with CSS support. Latest version: 2.1.3, last published: 9 months ago. Start using react-pdf-html in your project by running `npm i react-pdf-html`. There are 22 other projects in the npm registry using react-pdf-html.
      » npm install react-pdf-html
    
Published   Jan 15, 2025
Version   2.1.3
Author   Dan Blaisdell [email protected]
🌐
React-pdf
react-pdf.org
React-pdf
React renderer for creating PDF files on the browser and server
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.

🌐
Mdfaisal
mdfaisal.com › blog › download-html-as-a-pdf-in-react
Download HTML as a PDF in React | Mohammad Faisal
February 18, 2021 - html2canvas -> will convert our HTML document to image · jspdf -> will insert the generated image into a PDF file · The fullomplete code is at the bottom. If you are interested only in that go there directly. Otherwise bear with me. It won’t take long · First, install the required dependencies. ... Now Either create a separate component for your GenericPdfdownloader or put the following code inside the component you want to download.
Find elsewhere
🌐
npm
npmjs.com › package › react-to-pdf
react-to-pdf - npm
Create PDF documents from React Components. Latest version: 2.0.3, last published: 18 days ago. Start using react-to-pdf in your project by running `npm i react-to-pdf`. There are 28 other projects in the npm registry using react-to-pdf.
      » npm install react-to-pdf
    
Published   Nov 25, 2025
Version   2.0.3
Author   Marcos Andrei Ivanechtchuk
🌐
GitHub
github.com › christopherdro › react-native-html-to-pdf
GitHub - christopherdro/react-native-html-to-pdf: Convert html strings to PDF documents using React Native
Convert html strings to PDF documents using React Native - christopherdro/react-native-html-to-pdf
Starred by 451 users
Forked by 272 users
Languages   Kotlin 54.1% | TypeScript 16.7% | Objective-C++ 15.8% | JavaScript 5.4% | Ruby 4.9% | Swift 2.7% | Objective-C 0.4%
🌐
Syncfusion
support.syncfusion.com › kb › article › 13788 › how-to-convert-html-to-a-pdf-using-react-application
How to convert HTML to a PDF using React Application? | Syncfusion
By exporting HTML to PDF, you will get a PDF document as follows. You can download a complete working sample from HTMLToPDF_React.zip
🌐
GitHub
gist.github.com › brenopolanski › a316515d1b48f2d5ed6c5f71e638d510
Convert HTML to a PDF in React.JS · GitHub
import React, { Component } from 'react'; 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>); } } Sign up for free to join this conversation on GitHub.
🌐
PSPDFKit
pspdfkit.com › blog › 2022 › how-to-convert-html-to-pdf-using-react
How to Convert HTML to PDF in React - PSPDFKit
May 14, 2025 - Immediately after, doc.save("report.pdf") streams the finished document to the browser and prompts the user to download it under the specified filename. * The Nutrient React viewer renders existing PDF content, so external CSS limitations don’t apply. This guide showed you how to turn HTML or JSX into PDFs directly in the browser with jsPDF.
🌐
CodeSandbox
codesandbox.io › examples › package › react-pdf-html
react-pdf-html examples - CodeSandbox
html-to-pdf frontend (forked) raulgargon3Find more examples or templates · AboutHtml component for react-pdf with CSS support52,729Weekly Downloads · Latest version2.1.4 · LicenseMIT · External Links · github.com/danomatic/react-pdf-html#readme · github.com/danomatic/react-pdf-html ·
🌐
Medium
matheswaaran.medium.com › convert-html-string-to-pdf-using-jspdf-in-reactjs-255aba2d76a2
Convert HTML string to PDF in ReactJs | by Matheswaaran | Medium
August 28, 2022 - Convert HTML string to PDF in ReactJs This article discusses how to convert a HTML string into a PDF document in client side apps which uses ReactJs using jsPDF package. Install the required …
🌐
Medium
medium.com › @chrissyp › how-to-export-a-pdf-in-html-using-react-ffccdae8ab63
How to Export a PDF in HTML using React | by Chrissy | Medium
December 11, 2022 - When the button is clicked, the exportPDF function will be called, triggering the download of the PDF file. That’s it! Using the react-pdf library, you can easily export your HTML as a PDF file for the user to download.
🌐
npm
npmjs.com › package › react-native-html-to-pdf
react-native-html-to-pdf - npm
Convert html strings to PDF documents using React Native · npm install react-native-html-to-pdf ·
      » npm install react-native-html-to-pdf
    
Published   Sep 04, 2025
Version   1.3.0
Author   Christopher Dro
🌐
Startup House
startup-house.com › homepage › blog › converting html to pdf
How to generate PDF from HTML in React / Node.js app | Startup House
May 20, 2021 - React PDF, PDFKit, and jsPDF are some popular libraries for this purpose. However, if you're specifically looking at the React PDF library, it's crucial to use the appropriate React PDF components to achieve the desired result. Does exporting an HTML page to a PDF file retain the background color and custom fonts?