Format Parameter
The format parameter switches the output between a PNG image (the default) and a PDF document. It works on both the HTML/CSS API and the Screenshot API.
Specifications
| Property | Value |
|---|---|
| Type | string |
| Required | No |
| Default | png |
| Allowed values | png, pdf |
| API | Both HTML/CSS and Screenshot APIs |
Description
When format is set to pdf:
- The result is a real, vector PDF - text stays selectable and searchable, and fonts (including webfonts) are embedded in the document
- Content is laid out on A4 portrait pages and paginates automatically, so long content flows across as many pages as it needs
- Background colours, gradients and images are included
- The page renders with your normal screen CSS, so the PDF looks like the image output would -
@media printrules are not applied - The response
urlpoints to a.pdffile served with theapplication/pdfcontent type
Only genuine raster content (<img> elements, canvases) is embedded as pixels; everything else in the PDF is vector.
Sizing parameters do not apply to PDF output. Pages are A4 portrait, so width, height, dpi, fullpage and selector have no effect when format is pdf. There is no quality reason to want a higher DPI either: vector output is sharp at any zoom level.
Examples
HTML to PDF
{
"html": "<h1>Invoice #1042</h1><p>Due within 30 days.</p>",
"css": "h1 { color: #4f46e5; }",
"format": "pdf"
}
Web page to PDF
{
"url": "https://example.com",
"format": "pdf"
}
Async PDF with a webhook
{
"html": "<h1>Monthly report</h1>",
"format": "pdf",
"webhook_url": "https://your-domain.com/webhook"
}
Response
{
"success": true,
"id": "9d5f9b52-6b32-4a1c-a9c5-1f0b2a9e4c11",
"credits_remaining": 499,
"url": "https://i.html2img.com/image-1784019129398-416501.pdf"
}
A PDF conversion costs the same single credit as an image conversion.
Common values
png(default) - a PNG image sized by width and height, with optional dpi scaling.pdf- an A4 portrait PDF document with selectable text and automatic pagination.
When to use
Use format: "pdf" when the output is a document rather than a picture: invoices, receipts, reports, certificates, tickets, or printable versions of generated content. Anyone opening the file can select and copy the text, search inside it, and print it at full quality.
Stay with the default PNG for social cards, OG images, thumbnails and anywhere the output is displayed inside another page at fixed pixel dimensions.
Common mistakes
- Expecting
widthandheightto size the PDF. PDF pages are A4 portrait and content reflows to the page width. Design your markup to flow like a document rather than to fixed pixel dimensions. - Raising
dpito sharpen the output. PDF output is vector, so it is already sharp at any zoom level. Thedpivalue is ignored in PDF mode. - Relying on
@media printstyles. The PDF is rendered with screen CSS so it matches the image output. Put everything the PDF needs in your normal styles. - Using
selectororfullpage. Neither applies to PDFs - the whole document is captured and paginated automatically.
See also: getting started guide, webhook_url, css.
The templates endpoint currently outputs PNG only. To produce a PDF of document-shaped content like an invoice or a certificate, send your own markup through the HTML/CSS API with format: "pdf".