render_pdf
Turn an HTML document into a print-quality PDF and get a link you can share.
render_pdf(html: str, filename: str = "document") -> dictRenders a complete HTML document to a print-quality PDF and returns a link you can share. It lets your automations hand people a real document. For example, an action can build a quote from your pricing data, and the AI agent shares the download link with the customer. Use it from Python actions, event handlers, recurring jobs or HTTP endpoints for invoices, quotes, order summaries, warranty certificates or periodic reports.
The HTML is rendered by a real browser engine (headless Chromium), so modern
CSS works: flexbox, grid, tables, web-safe fonts and @page rules. Backgrounds
and colors are printed.
Parameters
| Name | Type | Description |
|---|---|---|
html | str | A complete, self-contained HTML document (see below) |
filename | str | The name customers see when they download it, e.g. "Invoice 1042". .pdf is added, and names over 120 characters are shortened |
Returns
dict: {"url": str, "filename": str, "fileId": str}
urlis the link to share. It opens a small download page that starts the download. It does not expire.filenameis the final file name, ending in.pdf.fileIdidentifies the stored file.
Anyone with the link can download the PDF, so share it only with the person it is meant for, the same way you would share a private document link.
Writing the HTML
The renderer has no network access and JavaScript is disabled, so everything must be inside the document:
- Put all CSS in a
<style>block. External stylesheets are not loaded. - Embed images and logos as
data:URIs. Image URLs are not fetched. - Use system font stacks such as
Helvetica, Arial, sans-seriforGeorgia, serif, or embed a font as adata:URI. - Draw charts with inline SVG or plain HTML and CSS.
- Set the paper size and margins with CSS. Without
@page, the PDF uses US Letter.
@page {
size: A4; /* or "A4 landscape", "Letter" */
margin: 18mm;
}Use break-before: page or break-inside: avoid to control page breaks, for
example on table rows.
Limits
| Limit | Value | Error when exceeded |
|---|---|---|
| HTML size | 10 MB | HTTP 400 |
| PDF size | 25 MB | HTTP 413 |
| PDFs per hour | 100 for each organization | HTTP 429 |
| Renders at the same time | a few for each organization | HTTP 503, retry shortly |
| Rendering time | about 30 seconds | HTTP 504 |
The helper raises requests.HTTPError with the reason when a limit is hit.
Large embedded images are the usual cause of big PDFs: resize them before
embedding.
Example: let the bot send an order summary
Create a Python action with an orderNumber argument. The bot can then share
the returned link with the customer.
import html as html_lib
def execute_action(context):
order_number = context["args"]["orderNumber"]
order = get_order(order_number) # your own lookup
rows = "".join(
f"<tr><td>{html_lib.escape(item['name'])}</td>"
f"<td class='num'>{item['quantity']}</td>"
f"<td class='num'>{item['total']}</td></tr>"
for item in order["items"]
)
document = f"""<!doctype html>
<html><head><meta charset="utf-8"><style>
@page {{ size: A4; margin: 18mm; }}
body {{ font-family: Helvetica, Arial, sans-serif; color: #1a1a1a; }}
h1 {{ font-size: 22px; margin: 0 0 16px; }}
table {{ width: 100%; border-collapse: collapse; }}
th, td {{ padding: 8px; border-bottom: 1px solid #ddd; text-align: left; }}
.num {{ text-align: right; }}
tr {{ break-inside: avoid; }}
</style></head>
<body>
<h1>Order {html_lib.escape(order_number)}</h1>
<table>
<tr><th>Item</th><th class="num">Qty</th><th class="num">Total</th></tr>
{rows}
</table>
</body></html>"""
pdf = render_pdf(document, filename=f"Order {order_number}")
return {"pdfUrl": pdf["url"]}Escape customer-provided values (names, addresses, order notes) with
html.escape before putting them into the HTML, so they show up as text and
cannot change the document's layout.
Rendering takes a few seconds. If the same document is requested often, save
the returned url (for example in conversation
metadata or the
Organization store) instead of
rendering it again.