jsPDF doc.html() saves nothing or a blank PDF in React
doc.html() runs but no PDF is downloadedShort 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.
Fix it fast
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"); }, });2Match what you see, then jump to its fix
- 1
doc.html(el) followed directly by doc.save() gives an empty filesave() is called outside the callbackFix - 2
npm ls shows html2canvas or dompurify missing, and html() produces no contenthtml2canvas (or dompurify) is not installed or was excluded by the bundlerFix - 3
The log from the first step prints null for the elementThe ref is null when html() is calledFix
- 1
What does the error look like?
doc.html() runs but no PDF is downloadedThe 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(" src2Fix
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 PDFConfirmed by raw.githack.com
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.js2Fix
Install the packages and list as externals only the optional packages you do not use.
npm install html2canvas dompurify3Verify
Run the export again; the console is free of loader errors and the PDF has content.
npm ls html2canvas dompurifyConfirmed by github.com
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=" src2Fix
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 outputConfirmed by react.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix