Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

jsPDF doc.html() saves nothing or a blank PDF in React

Errordoc.html() runs but no PDF is downloaded

Short answer

jsPDF's html() renders asynchronously, so the PDF is saved inside its callback, not on the next line. It also needs html2canvas, and dompurify when you pass an HTML string. In React, render from a ref only after the element is mounted, such as in a click handler, never during render.

  • Applies to jsPDF, html2canvas, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Add a log inside the html() callback and one before it. If the second never prints, the render failed or the element is null. Open the browser console for errors from html2canvas.

    console.log("element", ref.current);
    doc.html(ref.current, {
      callback: (pdf) => { console.log("rendered"); pdf.save("out.pdf"); },
    });
  2. 2Match what you see, then jump to its fix

    1. 1doc.html(el) followed directly by doc.save() gives an empty filesave() is called outside the callbackFix
    2. 2npm ls shows html2canvas or dompurify missing, and html() produces no contenthtml2canvas (or dompurify) is not installed or was excluded by the bundlerFix
    3. 3The log from the first step prints null for the elementThe ref is null when html() is calledFix

What does the error look like?

  • doc.html() runs but no PDF is downloaded
  • The PDF opens with one empty page

Cause 1 of 3

save() is called outside the callback

1Check

html() is documented with a callback option that receives the jsPDF instance, which is where the file is saved. Saving outside the callback means saving before the callback has handed you the finished document.

grep -rn "doc.html\|\.html(" src

2Fix

Move save() into the callback and use the instance it gives you.

const doc = new jsPDF();
doc.html(ref.current, {
  callback: (pdf) => pdf.save("invoice.pdf"),
  x: 10,
  y: 10,
});

3Verify

Click the button once and the file downloads with your content.

# click the export button in the browser and open the saved PDF

Confirmed by raw.githack.com

Did this fix it?

Cause 2 of 3

html2canvas (or dompurify) is not installed or was excluded by the bundler

1Check

jsPDF's README says the html method needs html2canvas, and dompurify when the source is an HTML string. They load dynamically, and a webpack externals entry for a package you do use stops them being bundled.

npm ls html2canvas dompurify
grep -rn "externals" webpack.config.js

2Fix

Install the packages and list as externals only the optional packages you do not use.

npm install html2canvas dompurify

3Verify

Run the export again; the console is free of loader errors and the PDF has content.

npm ls html2canvas dompurify

Confirmed by github.com

Did this fix it?

Cause 3 of 3

The ref is null when html() is called

1Check

ref.current is null on the first render and is set by React when the DOM is committed. Reading it during rendering, or from a component that is not rendered yet, passes null.

grep -rn "useRef\|ref=" src

2Fix

Read the ref inside an event handler, such as the export button's onClick, after the element is on screen.

const ref = useRef(null);

function exportPdf() {
  const doc = new jsPDF();
  doc.html(ref.current, { callback: (pdf) => pdf.save("page.pdf") });
}

return (
  <>
    <div ref={ref}>...</div>
    <button onClick={exportPdf}>Download PDF</button>
  </>
);

3Verify

The log from the first step prints an element, not null.

# click the export button and read the console output

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Build the PDF only in a user event or an Effect, never during render.
  • Do not pass raw user HTML to jsPDF without sanitising it first, as its README advises.
  • Keep a small test page for the export so a bundler change shows up early.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.