html4pdf vs printing from the browser

Before adding a dependency, it is worth asking whether the browser already does this. Often it does, and the honest answer to the whole question is a stylesheet.

The short answer

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

What the browser's print dialog is

Every browser can print to PDF, and window.print() opens that dialog from JavaScript. With a print stylesheet, @page rules for margins and page-break properties, the output is produced by the same engine that rendered the page, which makes it as accurate as it gets. It costs nothing and needs no library.

Side by side

 the browser's print dialoghtml4pdf
AccuracyPerfect, it is the user's own browserHeadless Chrome, effectively the same
CostFreeFrom $0.033 per document
Who triggers itThe user, in the dialogYour code, any time
Do you get the fileNo, it is saved by the userYes, in the response
Email or archive itNoYes
Scheduled or bulk outputNoYes
Headers and footersBrowser defaults the user can changeControlled by you
ConsistencyVaries by browser, OS and user settingsSame engine and settings every time

Choose the browser's print dialog when

  • A person is looking at the page and wants a copy of it.
  • You want to ship today with a stylesheet and no backend.
  • Nothing downstream needs the file.
  • The content is sensitive enough that it should never be transmitted.

Choose html4pdf when

  • The PDF is attached to an email, stored, or sent to someone else's system.
  • It is generated by a job, a webhook or an invoice run with nobody watching.
  • Every copy must be byte-for-byte consistent, not shaped by browser settings.
  • You need the file on the server, where the user's browser cannot help you.

See the code

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

the browser's print dialog: common questions

Can JavaScript save a page as PDF without a library?

It can open the print dialog with window.print(), and the user chooses Save as PDF. Your code never receives the file: browsers do not expose it, for good reason.

How do I control margins and page breaks when printing?

With CSS: @page for size and margins, break-inside: avoid to keep a block together, and break-before to start a new page. A print stylesheet gets you most of the way.

Why does my printed page include a header with the URL?

That is the browser adding it, and the user can turn it off in the dialog. You cannot control it from the page, which is one of the reasons server rendering exists.

Is the output the same across browsers?

No. Margins, default headers and font handling differ by browser and operating system, so two users printing the same page get slightly different files.

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