Inkspan › Guides › How to generate a PDF from a template in Django

How to generate a PDF from a template in Django

Call the Inkspan /v1/generate/template endpoint from Django to generate a PDF from a template — no headless browser, font stack, or rendering infrastructure in your deploy. Keep your API key in the environment and POST.

Keep the key in settings loaded from the environment, not in source. Returning HttpResponse with the PDF content type streams it straight to the browser.

Django example

# views.py
import requests
from django.conf import settings
from django.http import HttpResponse

def make_pdf(request):
    r = requests.post(
        'https://api.inkspan.dev/v1/generate/template',
        headers={'X-API-Key': settings.INKSPAN_KEY},
        json={"template": "<h1>{{ name }}</h1>", "data": {"name": "Ada"}},
    )
    r.raise_for_status()
    return HttpResponse(r.content, content_type='application/pdf')

Request parameters

NameTypeRequiredDescription
templatestringyesA Jinja2 template. Supports {{ variables }}, {% for %} loops, {% if %} conditionals, and filters.
dataobjectyesJSON object whose top-level keys become template variables.
filenamestringnoFilename in the response's Content-Disposition.

Response

Returns a PDF as a binary stream with Content-Type: application/pdf and Content-Disposition: attachment; filename=document.pdf. Write the response body straight to disk — do not parse it as JSON.

Production notes

For anything slow, hand the call to Celery and email or store the result — a synchronous request ties up a worker for the duration of the render.

Errors

StatusMeaningFix
401Invalid or missing API keySend your key in the X-API-Key header.
402Monthly quota exceededThe response reports used/quota. Upgrade at inkspan.dev/#pricing.
422Missing or invalid parameterCheck required fields in the table above — the body names the offending field.

Failed requests do not count against your quota; only successful ones are metered.

Get a free API key →

Same operation in cURL · Python · Node.js · Other frameworks: Next.js · Laravel · Ruby on Rails · PHP · All guides