Generate receipt PDFs in Python
The simplest way to generate a receipt PDF in Python is to POST a receipt HTML template and your order JSON to the Docweave API — Docweave renders it with Chromium and hands back a PDF, so your script never has to run a headless browser itself.
Receipt template example
Any HTML works as a template. Use {{ placeholders }} for the order id, items, amount paid, payment method, and date:
<article style="font-family: sans-serif; max-width: 480px;">
<header style="display:flex; justify-content:space-between;">
<h1>Receipt</h1>
<div>{{ date }}</div>
</header>
<p>Order: {{ order_id }}</p>
<table style="width:100%; border-collapse:collapse;">
{{{ items }}}
</table>
<p style="text-align:right;"><strong>Amount paid: {{ amount_paid }}</strong></p>
<p style="text-align:right;">Payment method: {{ payment_method }}</p>
</article>Generate the PDF with Python
POST the template and data to /api/v1/pdf with requests, then save the raw response bytes to a file:
import requests
template = """
<article style="font-family: sans-serif; max-width: 480px;">
<header style="display:flex; justify-content:space-between;">
<h1>Receipt</h1>
<div>{{ date }}</div>
</header>
<p>Order: {{ order_id }}</p>
<table style="width:100%; border-collapse:collapse;">
{{{ items }}}
</table>
<p style="text-align:right;"><strong>Amount paid: {{ amount_paid }}</strong></p>
<p style="text-align:right;">Payment method: {{ payment_method }}</p>
</article>
"""
payload = {
"source": {
"type": "template",
"template": template,
"data": {
"order_id": "ORD-7731",
"date": "2026-07-14",
"items": "<tr><td>Wireless mouse</td><td>$24.00</td></tr>",
"amount_paid": "$24.00",
"payment_method": "Visa •••• 4242",
},
},
"idempotencyKey": "receipt-ORD-7731",
}
response = requests.post(
"https://docweave.dev/api/v1/pdf",
headers={
"Authorization": "Bearer dw_live_your_key",
"Content-Type": "application/json",
"Accept": "application/pdf",
},
json=payload,
)
response.raise_for_status()
with open("receipt.pdf", "wb") as f:
f.write(response.content)…or from an AI agent
An AI agent doesn't need the Python script above at all. Install the Docweave MCP server with npx @docweave/mcp and the agent gets a generate_pdf tool that wraps the same orchestration — no HTTP boilerplate:
npx @docweave/mcp
// From an AI agent over MCP — no HTTP plumbing:
generate_pdf({
source: {
type: "template",
template: "<h1>Receipt</h1><p>Order: {{ order_id }}</p><p>Amount paid: {{ amount_paid }}</p>",
data: { order_id: "ORD-7731", amount_paid: "$24.00" }
},
outputPath: "/tmp/receipt-7731.pdf"
})FAQ
How do I generate a receipt PDF in Python?
Use the requests library to POST a JSON body with a template source (your receipt HTML plus a data object) to /api/v1/pdf, then write the raw response bytes (r.content) to a .pdf file. No PDF library or headless browser needed on your end.
Why does the response come back as bytes instead of JSON?
Sending Accept: application/pdf tells the API to return the rendered PDF directly in the response body. Write response.content straight to a file opened in binary mode ("wb") — don't try to json.loads() it.
Can I reuse the same receipt template for every order?
Yes. Keep the HTML template the same across requests and just change the data object per order — order id, line items, amount paid, payment method, and date. The layout stays identical while the values change per call.
How do I avoid billing for a duplicate receipt if my script retries?
Pass an idempotencyKey (for example the order id, like "receipt-ORD-7731"). A retried request with the same key returns the already-generated result instead of rendering and billing again.
Related
Ready to generate receipt PDFs in production?
Get an API key