Generate certificate PDFs in Python
The simplest way to generate certificate PDFs in Python is to POST a certificate HTML template plus recipient JSON to the Docweave API — no headless browser to install, manage, or keep patched.
Certificate template example
Any HTML works as a template. Use {{ placeholders }} for the recipient name, course or award title, issue date, and issuer:
<article style="font-family: 'Georgia', serif; text-align:center; padding: 64px; border: 8px double #c08a2e;">
<p style="letter-spacing: 4px; text-transform: uppercase; color:#8a8a8a;">Certificate of Completion</p>
<h1 style="font-size: 36px; margin: 24px 0 8px;">{{ recipient_name }}</h1>
<p style="font-size: 16px; color:#555;">has successfully completed</p>
<h2 style="font-size: 24px; margin: 12px 0 32px;">{{ course_title }}</h2>
<div style="display:flex; justify-content:space-between; margin-top: 48px;">
<div>
<p style="border-top: 1px solid #333; padding-top: 4px;">{{ issue_date }}</p>
<p style="color:#888; font-size: 12px;">Date issued</p>
</div>
<div>
<p style="border-top: 1px solid #333; padding-top: 4px;">{{ issuer_name }}</p>
<p style="color:#888; font-size: 12px;">Issued by</p>
</div>
</div>
</article>Generate the PDF with Python
Use requests to POST the template and data to /api/v1/pdf, then write the raw response bytes to a file:
import requests
url = "https://docweave.dev/api/v1/pdf"
headers = {
"Authorization": "Bearer dw_live_your_key",
"Content-Type": "application/json",
"Accept": "application/pdf",
}
template = """
<article style="font-family: 'Georgia', serif; text-align:center; padding: 64px; border: 8px double #c08a2e;">
<p style="letter-spacing: 4px; text-transform: uppercase; color:#8a8a8a;">Certificate of Completion</p>
<h1 style="font-size: 36px; margin: 24px 0 8px;">{{ recipient_name }}</h1>
<p style="font-size: 16px; color:#555;">has successfully completed</p>
<h2 style="font-size: 24px; margin: 12px 0 32px;">{{ course_title }}</h2>
<div style="display:flex; justify-content:space-between; margin-top: 48px;">
<div>
<p style="border-top: 1px solid #333; padding-top: 4px;">{{ issue_date }}</p>
<p style="color:#888; font-size: 12px;">Date issued</p>
</div>
<div>
<p style="border-top: 1px solid #333; padding-top: 4px;">{{ issuer_name }}</p>
<p style="color:#888; font-size: 12px;">Issued by</p>
</div>
</div>
</article>
"""
payload = {
"source": {
"type": "template",
"template": template,
"data": {
"recipient_name": "Jordan Reyes",
"course_title": "Advanced Python for Data Engineering",
"issue_date": "July 16, 2026",
"issuer_name": "Docweave Academy",
},
},
"idempotencyKey": "cert-jordan-reyes-data-eng-101",
}
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
with open("certificate.pdf", "wb") as f:
f.write(response.content)
…or from an AI agent
An AI agent doesn't need the Python script at all. Add the Docweave MCP server (npx @docweave/mcp) and the agent gets a generate_pdf tool that wraps the same orchestration:
// From an AI agent over MCP — no HTTP plumbing:
// npx @docweave/mcp
generate_pdf({
source: {
type: "template",
template: "<article>...certificate markup with {{ placeholders }}...</article>",
data: {
recipient_name: "Jordan Reyes",
course_title: "Advanced Python for Data Engineering",
issue_date: "July 16, 2026",
issuer_name: "Docweave Academy"
}
},
outputPath: "/tmp/certificate.pdf"
})FAQ
How do I generate a certificate PDF in Python?
Send a POST request from Python (using the requests library) to /api/v1/pdf with a source of type "template": pass your certificate HTML and a data object with the recipient's name, course or award title, issue date, and issuer. Write the raw response bytes to a file and you have a PDF.
What does the Python code need to send and read?
Send JSON with source.type "template", the template string, and the data to bind, plus an idempotencyKey. Set the Accept header to application/pdf, then write response.content (the raw bytes) directly to a .pdf file — don't try to parse it as JSON or text.
Can I reuse one certificate template for many recipients?
Yes. Keep the HTML template constant and change only the data object — recipient name, course title, issue date, and issuer — per request. That's how you batch-issue certificates from a Python script or loop.
How is certificate PDF generation priced?
Pricing is per document, not per page. Whether the certificate is a single page or includes an extra page of details, it counts as one document.
Related
Ready to generate certificate PDFs in production?
Get an API key