Generate a PDF in Python

The simplest way to generate a PDF in Python is to call the Docweave API with requests: POST your HTML to /api/v1/pdf and write the returned bytes to a file — no headless browser to install or run yourself.

Python HTML to PDF, using requests

Send an HTML string in the request body and write the PDF response to disk:

import requests

API_KEY = "dw_live_your_key"

html = """
<h1>Invoice INV-042</h1>
<p>Billed to Acme Inc.</p>
<p>Total due: $1,280.00</p>
"""

response = requests.post(
    "https://docweave.dev/api/v1/pdf",
    headers={
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json",
        "Accept": "application/pdf",
    },
    json={
        "source": {"type": "html", "html": html},
        "options": {"format": "A4"},
        "idempotencyKey": "inv-042",
    },
    timeout=30,
)
response.raise_for_status()

with open("invoice.pdf", "wb") as f:
    f.write(response.content)

print("Wrote invoice.pdf")

No dependencies? Use the standard library

If you'd rather not add requests as a dependency, urllib.request from the Python standard library works the same way:

import json
import urllib.request

API_KEY = "dw_live_your_key"

payload = json.dumps({
    "source": {
        "type": "html",
        "html": "<h1>Invoice INV-042</h1><p>Total due: $1,280.00</p>",
    },
    "options": {"format": "A4"},
    "idempotencyKey": "inv-042",
}).encode("utf-8")

req = urllib.request.Request(
    "https://docweave.dev/api/v1/pdf",
    data=payload,
    method="POST",
    headers={
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json",
        "Accept": "application/pdf",
    },
)

with urllib.request.urlopen(req, timeout=30) as resp:
    pdf_bytes = resp.read()

with open("invoice.pdf", "wb") as f:
    f.write(pdf_bytes)

print("Wrote invoice.pdf")

…or from a Python AI agent, via MCP

If your agent runs in Python (LangChain, an MCP-native framework, or a custom client), the Docweave MCP server exposes a generate_pdf tool — the same underlying render, no HTTP boilerplate to write:

# From a Python-based AI agent, over MCP — no HTTP plumbing:
generate_pdf({
  "source": {
    "type": "html",
    "html": "<h1>Invoice INV-042</h1><p>Total due: $1,280.00</p>"
  },
  "outputPath": "/tmp/invoice-042.pdf"
})

# Any MCP client (Claude, an agent framework, a custom Python MCP client)
# can call this tool once the server is registered — start it with:
# npx @docweave/mcp

FAQ

What's the simplest way to generate a PDF in Python?

Call a PDF-generation API with the requests library: POST your HTML to /api/v1/pdf with an Authorization: Bearer header, then write response.content to a file. There's no headless browser to install, configure, or keep patched in your own Python environment.

Do I need Playwright, WeasyPrint, or wkhtmltopdf installed to generate PDFs in Python?

No. Those libraries render HTML to PDF locally and require you to manage a browser or rendering engine as a dependency. Calling the Docweave API from Python offloads that rendering to the service — your code only needs an HTTP client.

Can I generate a PDF from a URL instead of raw HTML in Python?

Yes. Set "source": {"type": "url", "url": "https://example.com"} in the same JSON payload instead of the "html" field, and POST it the same way with requests or urllib.

How do I avoid double-billing if my Python script retries a failed request?

Pass an idempotencyKey (for example an invoice number or job ID) in the request body. A retry with the same key returns the original PDF instead of rendering — and billing — again. Rendering is billed per document, not per page.

Ready to generate PDFs from Python in production?

Get an API key