html4pdf vs WeasyPrint

This comparison is unusually easy to settle, because the deciding question has a yes or no answer: does your page need JavaScript to look finished?

The short answer

If 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.

What WeasyPrint is

WeasyPrint renders HTML and CSS to PDF in pure Python. It has the best support for CSS Paged Media of anything in that ecosystem: running headers and footers, page counters, named pages, margin boxes. For print-shaped documents written as print documents, it often produces better results than a browser does.

Side by side

 WeasyPrinthtml4pdf
Rendering engineIts own, written in PythonHeadless Chrome
JavaScriptNot executed at allRuns before the page is captured
CSS Paged MediaExcellent: margin boxes, counters, named pagesGood: margins, format, headers via options
Modern layout CSSFlexbox and grid supported, with gaps against ChromeWhatever Chrome does
Installpip, plus system libraries such as PangoNothing
Where it runsIn your process, offlineAn HTTPS call
LanguagesPythonAny
CostFreeFrom $0.033 per document
SpeedFast, no browser to startThree to five seconds including the round trip

Choose WeasyPrint when

  • Your document is static HTML and CSS: invoices, statements, reports rendered from a template.
  • You need real print features: repeating table headers, page X of Y, margin boxes.
  • You are in Python and want no outbound calls and no per-document cost.
  • Your documents must stay inside your own process.

Choose html4pdf when

  • Something on the page is drawn by JavaScript: a chart, a map, a widget.
  • The document is an existing web page, built for screens, that must come out looking the same.
  • You are not writing Python.
  • You would rather not add Pango, cairo and their friends to a container image.

See the code

The working example, in the language this decision is being made in.

WeasyPrint: common questions

Does WeasyPrint support JavaScript?

No, and this is by design rather than an omission. It parses HTML and CSS and lays out the result. Anything a script would have drawn is simply not in the output.

Is WeasyPrint better than headless Chrome for PDFs?

For documents designed as documents, often yes: its CSS Paged Media support is better than Chrome's. For reproducing a web page, no, because it is not the browser the page was built for.

Can I use WeasyPrint for charts?

Only if the chart is already an image or inline SVG by the time WeasyPrint sees it. Render it server side first, or use a browser-based renderer.

Which is faster?

WeasyPrint, usually, because there is no browser to start and no network round trip.

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.

Get API key

Other comparisons