Changelog

Custom CSS

The new css parameter adds your own stylesheet to the page before the PDF is generated, so you can hide or restyle parts of a page you can't edit.

NewAPI

Set css to a stylesheet to add it to the page before the PDF is generated. It is for pages you can't change: a URL, or an HTML file exported from another tool. Hide a cookie banner or a chat widget, change fonts, add page breaks, or fix a layout that doesn't print well.

curl --fail --show-error --output page.pdf --request POST \
  --url https://api.html2pdf.app/v1/generate \
  --header 'Content-Type: application/json' \
  --header 'X-API-Key: <your-api-key>' \
  --data '{
    "html": "https://example.com",
    "css": "#cookie-banner { display: none !important; }"
  }'

You can also try it in the playground, under Custom CSS.

How it works

  • After the page's own styles — the stylesheet is added once the page has loaded. Where your rule and the page's are equally specific, yours wins. A more specific page rule, or one marked !important, still wins; add !important to yours to override it.
  • Fonts and images — @import, web fonts and background images work, and what they load is waited for before the PDF is generated.
  • URLs and HTML — it works for HTML sent in html and for URLs that return a web page, and a page's Content Security Policy does not block it.
  • With other options — lazyLoad and singlePage see the page with your styles applied.

Things to know

  • It can be up to 100 KB.
  • It styles the page only. Style headerTemplate and footerTemplate with inline styles inside them.
  • It does not reach into frames (iframe) on the page.

If only the first screen of a page ends up in the PDF, the page probably scrolls its content inside a panel of its own. A few lines of CSS let that panel grow to its full height; see When only the first screen is in the PDF.

See Custom CSS, Parameters and Troubleshooting in the documentation.