Generate PDFs with Python

Complete Python code examples for the PixDoc API

Install

Terminal
pip install requests

Generate a PDF

Send HTML to the /api/v1/pdf endpoint and save the binary response.

Python
import requests

response = requests.post(
    "https://pixdoc.dev/api/v1/pdf",
    headers={"Authorization": "Bearer YOUR_API_KEY"},
    json={"html": "<h1>Hello World</h1>"}
)

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

Take a Screenshot

The /api/v1/screenshot endpoint returns a PNG image.

Python
import requests

response = requests.post(
    "https://pixdoc.dev/api/v1/screenshot",
    headers={"Authorization": "Bearer YOUR_API_KEY"},
    json={"html": "<h1>Hello World</h1>"}
)

with open("screenshot.png", "wb") as f:
    f.write(response.content)

Use a Template

Pass a template_id and variables instead of raw HTML.

Python
import requests

response = requests.post(
    "https://pixdoc.dev/api/v1/pdf",
    headers={"Authorization": "Bearer YOUR_API_KEY"},
    json={
        "template_id": "invoice",
        "variables": {
            "company": "Acme Corp",
            "total": "$1,250.00"
        }
    }
)

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

Next Steps