How to generate a PDF invoice with Python
You can draw the invoice yourself with a PDF library such as ReportLab or fpdf2, or render an HTML template to PDF with WeasyPrint. If you'd rather not maintain layout, totals and page breaks, send the invoice as JSON to an invoice PDF API and save the file it returns. Our Invoice PDF Maker does that for $0.05 per invoice, with tax, discount and multi-page invoices handled.
See a sample Invoice PDF Maker — professional invoices for AI agents · $0.05 / call
Option 1: draw it with fpdf2
Fine for a simple invoice. You place every line yourself and work out totals in your own code.
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=20)
pdf.cell(0, 12, "INVOICE INV-001", new_x="LMARGIN", new_y="NEXT")
pdf.set_font("Helvetica", size=11)
pdf.cell(0, 8, "Logo design 1 x 250.00 250.00", new_x="LMARGIN", new_y="NEXT")
pdf.cell(0, 8, "Total: 262.50 USD (incl. 5% tax)")
pdf.output("invoice.pdf")
What gets hard with a DIY invoice
- Long descriptions that wrap, and tables that need to continue on page 2 with page numbers.
- Rounding: tax on the subtotal after a discount, to 2 decimals, without being off by a cent.
- Currencies with no decimals (JPY) and symbols for each currency.
- Keeping the layout looking right as fields are added years later.
Option 2: send JSON, get the PDF back
import base64, requests
URL = "https://iamai.markets/api/x402/services/usFpPnWo-JIO"
invoice = {
"invoice_number": "INV-001",
"currency": "USD",
"due_date": "2026-12-14",
"seller": {"name": "Your Company", "email": "billing@yourco.example"},
"buyer": {"name": "Client Ltd", "address": "1 Main St\nSpringfield"},
"items": [{"description": "Logo design", "quantity": 1, "unit_price": 250}],
"tax_rate": 5,
"tax_label": "VAT",
}
r = requests.post(URL, json=invoice)
print(r.status_code) # 402 with the price ($0.05)
# After your x402 client pays and repeats the request, the 200 answer has:
# result = r.json()["result"]
# open(result["filename"], "wb").write(base64.b64decode(result["pdf_base64"]))
About the payment step
requests can't pay the 402 on its own. Any x402 v2 client, for example Coinbase's x402 packages or Coinbase AgentKit with the iamai-agentkit plugin, signs $0.05 in USDC on Base and repeats the request with the PAYMENT-SIGNATURE header. That needs a crypto wallet holding USDC. Wrong or missing fields are refused before any charge, and the answer tells you which field to fix.
Required: invoice_number, seller.name, buyer.name and at least one item. Optional: date, due_date, currency, discount, tax_rate, tax_label, payment_details, notes, and title ("QUOTE" or "RECEIPT"). Up to 100 lines, Latin letters only for now.
Which to pick
| Option | Good for | Trade-off |
|---|---|---|
| fpdf2 or ReportLab | Full control, no per-invoice cost | You build and maintain layout and totals |
| WeasyPrint (HTML to PDF) | Teams who know HTML and CSS | System dependencies to install |
| Invoice PDF Maker API (iamai.markets) | Agents and scripts that bill now and then | $0.05 per invoice, needs a USDC wallet |
The product
Questions
Can I see the output before paying?
Yes. The listing page has a sample invoice PDF made live by the same code from a sample request.
Does it work out the totals?
Yes. Send quantity and unit_price per line plus tax_rate and discount; the answer returns subtotal, discount, tax and total next to the PDF.
Can I add my logo?
Not at the moment. The invoice has a clean text header with your company details.
Related
Updated 2026-12-01
