catalogue / tools / Documents / HTML to PDF

Convert HTML to PDF

To convert HTML to PDF, send one POST to /v1/html-to-pdf with the HTML source. Each call costs $0.005 in USDC on Base via x402, with no account and no API key. The source file can be up to 5 MB.

Endpoint
POST /v1/html-to-pdf
Price
$0.005 per call

01Limits

LimitValue
Largest source file5 MB (5,242,880 bytes)
Timeout12 s
Source formatsHTML
Output formatsPDF
Option formatone of "A3", "A4", "A5", "Letter", "Legal", "Tabloid"; default "A4"
Option landscapedefault false

02Example

A real HTML to PDF run of html-to-pdf.

Request
POST /v1/html-to-pdf
content-type: application/json
payment-signature: <base64 x402 payload>

{
  "url": "https://grist.tools/samples/web/render-print.html",
  "format": "A4",
  "landscape": false
}
Response
200 OK
payment-response: <base64 settlement receipt>

{
  "source_url": "https://grist.tools/samples/web/render-print.html",
  "pages": 1,
  "bytes": 38404,
  "format": "A4",
  "landscape": false,
  "pdf_base64": "JVBERi0xLjcKJYGBgYEKCjYgMCBvYmoKPDwKL0ZpbHRlciAvRmxhdGVEZWNvZGUK...",
  "rendered_at": "2026-09-01T12:00:00.000Z"
}

Source file: web/render-print.html, 1.4 KB (1,467 bytes). Printable one-page HTML table with inline CSS: a weekly rain gauge reading schedule. The request sets format to "A4" and landscape to false. The recorded run returned 37.5 KB (38,404 bytes) of PDF, 26.2 times the source size, with 1 page. Decoded, the PDF in pdf_base64 opens with 25 50 44 46 2D 31 2E 37 0A 25 81 81 (ASCII PDF). Recorded with chromium 153.0.8010.36.

In the sample itself, the document declares language en and the title "Rain Gauge Reading Schedule"; 1 style element carries 8 CSS rules; the body holds 28 td, 8 tr, 4 th, 1 a, 1 footer, 1 h1, 1 p, 1 table, 1 tbody and 1 thead elements; no script element; its heading reads "Rain Gauge Reading Schedule"; the table header cells are Day, Reader, Reading (mm) and Notes; its one link points to render-article.html. Across the whole file, the markup opens 53 tags, 3 of them inside the head. In the returned PDF: the file opens with the header line %PDF-1.7, then a comment of four bytes above 0x7F that marks the file as binary. The first object in the file is number 6, generation 0, a stream compressed with FlateDecode.

03Format notes

HTML as a source

HTML can be supplied inline in the html field or fetched from a URL, and exactly one of the two is required. html-clean-text turns it into Markdown and text, and html-to-pdf renders it. When html-to-pdf fetches a page, it refuses a declared content type other than HTML or XHTML, looks past a byte order mark and leading whitespace, and rejects a body that starts with a recognised binary signature such as an archive, image or audio header. The character set is taken from a byte order mark, then the HTTP charset, then a meta tag near the start. The browser never navigates to the caller's URL and runs no page JavaScript. For inline HTML, html-clean-text accepts a base_url to resolve relative links, which is not allowed together with url, where the final fetched address serves as the base.

PDF as a target

HTML and Markdown reach PDF through one shared Chromium renderer. The document is handed to the browser as a string, never as a URL to navigate, JavaScript is disabled, and every interceptable sub-resource request is aborted, so external stylesheets, web fonts and remote images do not appear in the output. Paper size comes from a closed enum shared by both services, and a landscape flag switches orientation. For Markdown, markdown-it escapes raw HTML, and a built-in print stylesheet using system fonts supplies the page margins, since the renderer sets none. After rendering, pdf-lib saves the file again with pinned creation and modification dates and grist.tools as producer and creator, dropping the document ID, so the same input yields the same bytes for a given Chromium version.

04Errors

CodeHTTP
invalid_input400
blocked_target403
unreachable_target424
upstream_timeout424
too_large413
unprocessable422
internal500

When each is raised, and what it means for payment: /docs/html-to-pdf.

05Questions

What does it cost to convert HTML to PDF?
$0.005 per call, paid in USDC on Base via x402.
How large can the HTML file be?
Up to 5 MB (5,242,880 bytes). Past a limit the call answers too_large (413).
What happens if the conversion fails?
The tool answers with a typed JSON error from the errors table. Payment is settled only after the tool has produced its result; a call that fails inside the tool is never settled.

06Related