html4pdf vs jsPDF and html2canvas
This is the first result for almost every search about downloading a PDF from a web app, which is why so many products ship a PDF that nobody can copy text out of.
The short answer
jsPDF with html2canvas is free and needs no backend, and it produces a picture of your page rather than a document. If the text has to be selectable, searchable or printable at quality, it is the wrong tool.
What jsPDF and html2canvas is
jsPDF builds PDFs in the browser with a drawing API. html2canvas rasterises part of the DOM into a canvas. Put together, the usual recipe screenshots an element and places the image into a one page PDF. It is entirely client side, so there is no server, no key and no cost.
Side by side
| jsPDF and html2canvas | html4pdf | |
|---|---|---|
| Where it runs | The browser | Our server, in memory |
| Output | An image of the page, embedded in a PDF | Real text and vectors |
| Selectable text | No | Yes |
| Searchable and accessible | No | Yes |
| Print quality | Fixed resolution, soft on paper | Vector, sharp at any size |
| Page breaks | Wherever the image is sliced, often mid-line | Real pagination |
| File size | Large, it is a bitmap | Small |
| CSS fidelity | html2canvas reimplements rendering and misses things | It is Chrome |
| Cost | Free | From $0.033 per document |
| Backend needed | No | Yes, a server call |
Choose jsPDF and html2canvas when
- You want a rough visual capture and nobody will read it on paper.
- There is no backend at all and there cannot be one.
- The content must never leave the browser, and a picture is acceptable.
- It is a chart or a diagram, where an image is the honest output anyway.
Choose html4pdf when
- Someone will select, copy or search the text: an invoice, a contract, a report.
- It will be printed, or read on a screen bigger than the one that made it.
- The document is longer than one page and needs real page breaks.
- Accessibility matters, and a flat image fails it completely.
See the code
The working example, in the language this decision is being made in.
jsPDF and html2canvas: common questions
Why is my html2canvas PDF blurry?
Because it is a bitmap sized to the screen. Raising the scale option helps and makes the file much larger, but it is still an image: at print resolution it will always be softer than real text.
Can I select text in a jsPDF and html2canvas PDF?
Not when the page was captured by html2canvas. The text is pixels. Text written directly through the jsPDF drawing API is selectable, but then you are not converting HTML, you are drawing the document by hand.
How do I handle multiple pages?
The usual recipe slices the tall canvas into page-height strips, which cuts through whatever happens to be at the boundary. Avoiding that means rendering the document properly rather than photographing it.
Is there a client side way to get real text?
Yes: @react-pdf/renderer draws real text and vectors in the browser. The trade-off is that you write its components rather than HTML.
Can I call a PDF API from the browser instead?
Not directly, because your API key would be in the bundle for anyone to take. Proxy it through your own backend route.
Try it before you decide
New accounts get 10 free credits, no card. Send the HTML you already have and compare the output against whatever you are using now.
Other comparisons
- html4pdf vs PuppeteerRun Puppeteer yourself if PDFs are high volume and you already operate containers. Use an API if PDFs are a feature of your product and you do not want Chrome in your deployment.
- html4pdf vs wkhtmltopdfwkhtmltopdf is still fine for simple documents you already have working. For anything new, or anything using modern CSS, its archived WebKit engine is the problem you will eventually hit.
- html4pdf vs WeasyPrintIf your document is HTML and CSS with no scripts, WeasyPrint is excellent and free, and you should use it. If anything on the page is drawn by JavaScript, you need a browser.
- html4pdf vs DompdfDompdf is the right answer for a simple document on shared hosting with no extra services. It is the wrong answer for anything designed with modern CSS, because it implements CSS 2.1.
- html4pdf vs @react-pdf/rendererUse react-pdf when the document is small, stable, and worth building as its own component tree. Use a renderer when the PDF should be the same thing your users already see on screen.
- html4pdf vs printing from the browserIf the user is the one who wants the PDF and is sitting in front of the page, use window.print() and a print stylesheet. If your system needs the file, you need a renderer.
- html4pdf vs PlaywrightPlaywright is the better browser automation library. It is not a better PDF pipeline, because the cost was never the library: it is operating the browser. Use it if you already run it, call an API if you do not.
- html4pdf vs running Chromium in a LambdaIt works, it is free, and it is the single most fiddly part of most codebases that do it. If your function is already near the size limit or your users wait on the first request, the trade has stopped paying.
- html4pdf vs GotenbergGotenberg is the right answer if you want this capability inside your own infrastructure and have somewhere to run a container. We are the right answer if you want it without running anything.
- html4pdf vs mPDFmPDF is the best free answer in PHP for long, table-heavy documents written as documents. It is still its own engine, so a design built in a browser will not arrive intact.
- html4pdf vs ReportLabReportLab is the right tool when the document is a fixed, precise artefact and you want no dependencies. It is the wrong tool when the design changes often, because every change is a code change.
- html4pdf vs iTextIf you need deep PDF manipulation on the JVM and can accept AGPL or buy a licence, iText is the strongest tool available. If you only need HTML turned into a document, the licence question is one you can skip entirely.
- html4pdf vs QuestPDFQuestPDF is excellent for documents you are happy to describe in C#. If the document already exists as a web page, you would be rebuilding it, and that duplication is the whole cost.
- html4pdf vs PDFKitPDFKit is a good fit for simple, fixed documents generated at volume, where no browser and no network is worth writing the layout by hand. Beyond that the hand-written layout becomes the problem.