Convert HTML to PDF with curl

Render HTML to PDF from the command line with one curl call to the hosted WeasyPrint. No API key is needed to start.

Last updated

Endpoint POST /v1/weasyprint/render with a JSON body: html (required), filename (optional), pdf_variant (optional, paid plans)
Response The PDF itself (application/pdf)

Send your HTML as JSON to POST /v1/weasyprint/render, and the response body is the PDF. This call works without an API key:

curl -X POST https://api.pagespool.com/v1/weasyprint/render \
  -H 'Content-Type: application/json' \
  -d '{"html": "<h1>Hello</h1><p>Rendered by WeasyPrint.</p>", "filename": "hello.pdf"}' \
  -o hello.pdf

html is the whole HTML document as a string. filename is optional and sets the name in the Content-Disposition header (default document.pdf).

Send a template file with an API key

For a real template, let jq build the JSON so quotes and line breaks are escaped. Then pass the file with -d @:

jq -n --rawfile html invoice.html '{html: $html, filename: "invoice.pdf"}' > request.json
curl -X POST https://api.pagespool.com/v1/weasyprint/render \
  -H 'Content-Type: application/json' \
  -H "X-API-Key: $PAGESPOOL_KEY" \
  -d @request.json \
  -o invoice.pdf -w '%{http_code}\n'

A status other than 200 means invoice.pdf holds a JSON error instead of a PDF (error codes). A missing or invalid key does not fail the call; it runs at the limit for calls without a key. curl -u x:$PAGESPOOL_KEY sends the same key as HTTP Basic.

Images, fonts and stylesheets go into the HTML as data: URIs (how). The CSS support page lists which CSS works.

Next step

Set the page size and margins.

FAQ

Do I need an API key to convert HTML to PDF with curl?
No. Without a key, POST /v1/weasyprint/render gives 10 free credits a day per IP address, and a render costs 1. A free key adds 100 credits a month, used after the 10 free credits.
How large can the HTML be?
The html field can hold 2 MB of UTF-8 text. A larger request fails with status 413 (html_too_large).