2 Things:

  1. When getting access to a DOM Element in react you should always use useRef

  2. It appears that .html method is async and requires a callback

A working example would be something like this:

import { useRef } from 'react';
import { jsPDF } from 'jspdf';

export default function PDF() {
    const pdfRef = useRef(null);

    const handleDownload = () => {
        const content = pdfRef.current;

        const doc = new jsPDF();
        doc.html(content, {
            callback: function (doc) {
                doc.save('sample.pdf');
            }
        });
    };

    return (
        <div>
            <header ref={pdfRef}>
                <div>l kldfjlkasjfld asjflkajf ljfasd'flksdasjf lsdasjfsadf</div>
                <div>l kldfjlkasjfld asjflkajf ljfasd'flksdasjf lsdasjfsadf</div>
                <div>l kldfjlkasjfld asjflkajf ljfasd'flksdasjf lsdasjfsadf</div>
                <div>l kldfjlkasjfld asjflkajf ljfasd'flksdasjf lsdasjfsadf</div>
                <div>l kldfjlkasjfld asjflkajf ljfasd'flksdasjf lsdasjfsadf</div>
                <div>l kldfjlkasjfld asjflkajf ljfasd'flksdasjf lsdasjfsadf</div>
            </header>
            <footer>
                <button onClick={handleDownload}>Download</button>
            </footer>
        </div>
    );
}

Update

If you need to control the size you can add additional properties as per documentation cited above:

Option 1 - use the html2canvas option and control the scale:

const handleDownload = () => {
    const content = pdfRef.current;

    const doc = new jsPDF();
    doc.html(content, {
        callback: function (doc) {
            doc.save('sample.pdf');
        },
        html2canvas: { scale: 0.5 } // change the scale to whatever number you need
    });
};

Option 2 - use width and windowWidth:

const handleDownload = () => {
    const content = pdfRef.current;

    const doc = new jsPDF();
    doc.html(content, {
        callback: function (doc) {
            doc.save('sample.pdf');
        },
        width: 200, // <- here
        windowWidth: 200 // <- here
    });
};
Answer from SakoBu on Stack Overflow
🌐
npm
npmjs.com › package › jspdf
jspdf - npm
In React (create-react-app) projects, externals can be defined by either using react-app-rewired or ejecting. jsPDF can be imported just like any other 3rd party library. This works with all major toolkits and frameworks.
      » npm install jspdf
    
Published   Nov 19, 2025
Version   3.0.4
🌐
Nutrient
nutrient.io › blog › sdk › how to convert html to pdf using react
Generate PDFs from HTML in React with jsPDF
May 14, 2025 - Step‑by‑step instructions for converting HTML/JSX to professional PDFs in React using jsPDF — setup, custom fonts, multipage output, and common pitfalls.
Top answer
1 of 3
6

2 Things:

  1. When getting access to a DOM Element in react you should always use useRef

  2. It appears that .html method is async and requires a callback

A working example would be something like this:

import { useRef } from 'react';
import { jsPDF } from 'jspdf';

export default function PDF() {
    const pdfRef = useRef(null);

    const handleDownload = () => {
        const content = pdfRef.current;

        const doc = new jsPDF();
        doc.html(content, {
            callback: function (doc) {
                doc.save('sample.pdf');
            }
        });
    };

    return (
        <div>
            <header ref={pdfRef}>
                <div>l kldfjlkasjfld asjflkajf ljfasd'flksdasjf lsdasjfsadf</div>
                <div>l kldfjlkasjfld asjflkajf ljfasd'flksdasjf lsdasjfsadf</div>
                <div>l kldfjlkasjfld asjflkajf ljfasd'flksdasjf lsdasjfsadf</div>
                <div>l kldfjlkasjfld asjflkajf ljfasd'flksdasjf lsdasjfsadf</div>
                <div>l kldfjlkasjfld asjflkajf ljfasd'flksdasjf lsdasjfsadf</div>
                <div>l kldfjlkasjfld asjflkajf ljfasd'flksdasjf lsdasjfsadf</div>
            </header>
            <footer>
                <button onClick={handleDownload}>Download</button>
            </footer>
        </div>
    );
}

Update

If you need to control the size you can add additional properties as per documentation cited above:

Option 1 - use the html2canvas option and control the scale:

const handleDownload = () => {
    const content = pdfRef.current;

    const doc = new jsPDF();
    doc.html(content, {
        callback: function (doc) {
            doc.save('sample.pdf');
        },
        html2canvas: { scale: 0.5 } // change the scale to whatever number you need
    });
};

Option 2 - use width and windowWidth:

const handleDownload = () => {
    const content = pdfRef.current;

    const doc = new jsPDF();
    doc.html(content, {
        callback: function (doc) {
            doc.save('sample.pdf');
        },
        width: 200, // <- here
        windowWidth: 200 // <- here
    });
};
2 of 3
1
// use can you ReactDOMServer.renderToString(element) 

import { renderToString } from "react-dom/server";
import { jsPDF } from "jspdf";
export const dow = () => {
  let htmlElement = <div>Hello </div> 
  let elementAsString = renderToString(htmlElement);
  var doc = new jsPDF();
  doc.html(elementAsString, {
    callback: function (doc) {
      doc.save("test.pdf");
    },
    x: 10,
    y: 10,
  });
};

// use can use this code as function to handle an event 
🌐
CodeSandbox
codesandbox.io › examples › package › jspdf-react
jspdf-react examples - CodeSandbox
Use this online jspdf-react playground to view and fork jspdf-react example apps and templates on CodeSandbox.
🌐
npm
npmjs.com › package › jspdf-react
jspdf-react - npm
Wrapper jsPDF for React. Latest version: 1.0.11, last published: 6 years ago. Start using jspdf-react in your project by running `npm i jspdf-react`. There are 7 other projects in the npm registry using jspdf-react.
      » npm install jspdf-react
    
Published   Jul 17, 2019
Version   1.0.11
Author   apipemc
🌐
GitHub
github.com › apipemc › jspdf-react
GitHub - apipemc/jspdf-react: Wrapper jsPDF for React
import React, { Component } from 'react' import PDF, { Text, AddPage, Line, Image, Table, Html } from 'jspdf-react' import OctoCatImage from './OctoCatImage' const styleH1 = { fontSize: '15px', textAlign: 'center', color: 'red' }; const invisibleStyle = { display: 'none', }; export default class App extends Component { render () { const properties = { header: 'Acme' } const head = [["ID", "Name", "Country"]] const body = [ [1, "Shaw", "Tanzania"], [2, "Nelson", "Kazakhstan"], [3, "Garcia", "Madagascar"], ] return ( <React.Fragment> <PDF properties={properties} preview={true} > <Text x={35} y={
Starred by 34 users
Forked by 11 users
Languages   JavaScript 96.8% | HTML 3.2%
🌐
Medium
medium.com › @laurenbethhess › how-to-create-a-pdf-in-react-d18818190b6
How to create a pdf in react. I recently completed a project where a… | by Lauren Hess | Medium
January 29, 2022 - The first step is to install jsPDF by running ‘npm install jspdf — save’ in your terminal. After that, import it into the component that you want to download the pdf from along with ReactDOMServer.
Find elsewhere
🌐
CodePen
codepen.io › mjunaidi › pen › XomOQz
ReactJS: jsPDF - print with Style
console.clear() class Boilerplate extends React.Component { state={} render() { return ( <div> </div> ) } } const data = { firstName: 'john', lastName: 'donohue', email: '[email protected]', } class App extends React.Component { state={} componentDidMount() { this.setup() } download=event=>{ this.doc.save('sample.pdf') } setup() { const doc = new jsPDF() const el = document.getElementById('content') if (typeof(el)==='object'&&el!==null) { const width = 170 const elementHandlers = { '#ignorePDF': (element,renderer)=>{ return true } } doc.fromHTML(el,15,15,{width,elementHandlers},()=>{
🌐
GitHub
gist.github.com › SebastienBelmon › a38dcc1d714cbc49fd6e319b9c130ae7
react-jsPDF.js · GitHub
react-jsPDF.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
w3collective
w3collective.com › category: tutorial › exporting a react component to a pdf file on click
Exporting a React component to a PDF file on click - w3collective
February 18, 2022 - In this tutorial we’ll covering the process of exporting a React component to a PDF file. We’ll be using jsPDF to generate the PDF which can be installed by…
🌐
DEV Community
dev.to › parth24072001 › creating-dynamic-pdf-downloads-with-react-a-guide-using-jspdf-and-jspdf-autotable-4kbp
Creating Dynamic PDF Downloads with React: A Guide Using jsPDF and jspdf-autotable - DEV Community
February 22, 2024 - Discuss any potential updates or improvements in the jsPDF or jspdf-autotable libraries related to performance. By addressing the challenges of handling large datasets in the blog post, you provide valuable insights to readers who may encounter similar issues in their projects. It also showcases your awareness of practical considerations when working with extensive data sets. import React from 'react'; import jsPDF from "jspdf"; import "jspdf-autotable"; export function App(props) { const generatePDF = () => { const doc = new jsPDF("l", "mm", "legal"); doc.text("left heading", 10, 10); doc.tex
🌐
Quora
quora.com › How-do-I-create-a-PDF-using-JSPDF-in-ReactJS
How to create a PDF using JSPDF in ReactJS - Quora
Answer: To create a PDF using JSPDF in a React.js app, you can follow these steps: 1. Install the [code ]jspdf[/code] and [code ]html2canvas[/code] libraries: [code]Copy codenpm install jspdf html2canvas [/code] 1. Import the libraries into your React component: [code]Copy codeimport jsPDF from...
🌐
Stack Overflow
stackoverflow.com › questions › 68593655 › react-convert-the-component-as-pdf-using-jspdf
React convert the component as pdf using jspdf
import { useRef } from "react"; import html2canvas from "html2canvas"; import { jsPDF } from "jspdf"; import Document from "./Doocument" const App = () => { const inputRef = useRef(null); const printDocument = () => { html2canvas(inputRef.current).then((canvas) => { const imgData = canvas.toDataURL("image/png"); const pdf = new jsPDF(); pdf.addImage(imgData, "JPEG", 0, 0); pdf.save("download.pdf"); }); }; return ( <> <div className="App"> <h1>Hello CodeSandbox</h1> <h2>Start editing to see some magic happen!</h2> <div className="mb5"> <button onClick={printDocument}>Print</button> </div> <div id="divToPrint" ref={inputRef}> <Document/> </div> </div> </> ); }; export default App;
🌐
Medium
medium.com › dont-leave-me-out-in-the-code › 5-steps-to-create-a-pdf-in-react-using-jspdf-1af182b56cee
5 Steps to Create a PDF in React Using jsPDF | by Lance Watanabe | Don’t Leave Me Out in the Code | Medium
July 29, 2020 - Let’s find out how to create a PDF that is downloaded to the user’s local computer. We will be adding text and a picture to the PDF. I’m going to use jsPDF which is a JavaScript client-side library.
🌐
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 any server-side processing.
🌐
GitHub
github.com › bwl21 › react-jspdf-example
GitHub - bwl21/react-jspdf-example: This project is a playground and demonstrator how to embed jsPDF in a react project
This project is a playground and demonstrator how to embed jsPDF in a react project - bwl21/react-jspdf-example
Author   bwl21
🌐
CodeSandbox
codesandbox.io › s › jspdf-react-example-7i8xe
jspdf-react-example - CodeSandbox
February 26, 2020 - jspdf-react-example using jspdf, jspdf-react, prop-types, react, react-dom, react-scripts
Published   Feb 26, 2020
🌐
npm
npmjs.com › package › react-to-pdf
react-to-pdf - npm
You probably will not need this and things can break, // so use with caution. overrides: { // see https://artskydj.github.io/jsPDF/docs/jsPDF.html for more options pdf: { compress: true }, // see https://html2canvas.hertzen.com/configuration for more options canvas: { useCORS: true } }, }; // you can use a function to return the target element besides using React refs const getTargetElement = () => document.getElementById('content-id'); const Component = () => { return ( <div> <button onClick={() => generatePDF(getTargetElement, options)}>Generate PDF</button> <div id="content-id"> Content to be generated to PDF </div> </div> ); } Stackblitz demo ·
      » npm install react-to-pdf
    
Published   Nov 25, 2025
Version   2.0.3
Author   Marcos Andrei Ivanechtchuk