RenderPDFs/Tutorials/Django
📘 Quickstart · 5 minutes

Generate PDFs in Django

Generate PDFs from HTML, Markdown, URLs or templates in a Django view — using requests and the RenderPDFs REST API.

1. Install

RenderPDFs uses a plain REST API — no SDK required. For Django, install the dependency below and grab your API key from renderpdfs.com/signup (free, 100 PDFs/month, no credit card).

Installbash
pip install requests
# Add your key to settings.py or .env:
# RENDERPDFS_KEY = "rpdf_your_key"

2. Convert HTML to PDF

The simplest case: send an HTML string, get back a PDF binary. Anything Chromium renders works — Flexbox, Grid, web fonts, SVG, JavaScript.

HTML → PDF viewpython
# views.py
from django.conf import settings
from django.http import HttpResponse
from django.template.loader import render_to_string
import requests

def invoice_pdf(request, invoice_id):
    invoice = Invoice.objects.get(pk=invoice_id)
    html = render_to_string("invoices/detail.html", {"invoice": invoice})

    response = requests.post(
        "https://api.renderpdfs.com/v1/generate",
        headers={"X-API-Key": settings.RENDERPDFS_KEY},
        json={"html": html},
        timeout=60,
    )

    return HttpResponse(
        response.content,
        content_type="application/pdf",
        headers={"Content-Disposition": f'attachment; filename="invoice-{invoice.id}.pdf"'},
    )

3. Convert a URL to PDF

Pass url instead of html and RenderPDFs fetches the page, waits for JS, and snapshots it. Useful for archiving dashboards, public pages, or invoices served from your app.

URL → PDF (snapshot any page)python
response = requests.post(
    "https://api.renderpdfs.com/v1/generate",
    headers={"X-API-Key": settings.RENDERPDFS_KEY},
    json={
        "url": request.build_absolute_uri(f"/invoices/{invoice_id}/preview/"),
        "options": {"format": "A4", "printBackground": True},
    },
)

4. Use a built-in template

Skip the design work. RenderPDFs ships six battle-tested templates — invoice, receipt, report, contract, certificate, offer_letter. Send JSON, get a styled PDF.

Built-in invoice templatepython
response = requests.post(
    "https://api.renderpdfs.com/v1/generate",
    headers={"X-API-Key": settings.RENDERPDFS_KEY},
    json={
        "template": "invoice",
        "data": {
            "company": "Acme Inc.",
            "invoice_number": invoice.number,
            "items": [
                {"description": item.name, "quantity": item.qty, "unit_price": item.price}
                for item in invoice.items.all()
            ],
        },
    },
)

5. Custom paper, margins, headers

Control page format, orientation, margins, and running headers/footers via the options object. All standard Chromium PDF settings are supported.

Email the PDF to the customerpython
from django.core.mail import EmailMessage

pdf_bytes = requests.post(
    "https://api.renderpdfs.com/v1/generate",
    headers={"X-API-Key": settings.RENDERPDFS_KEY},
    json={"template": "invoice", "data": invoice.to_dict()},
).content

msg = EmailMessage(
    subject="Your invoice",
    body="See the attached invoice.",
    from_email="[email protected]",
    to=[invoice.customer.email],
)
msg.attach(f"invoice-{invoice.number}.pdf", pdf_bytes, "application/pdf")
msg.send()

6. Store the PDF and share a link

For emailable links or webhook payloads, set store: true in the body. The response becomes { url, expires_in } — the PDF is hosted on our CDN for 24 hours by default.

Use Celery for async generationpython
# tasks.py
from celery import shared_task

@shared_task
def generate_invoice_pdf(invoice_id):
    invoice = Invoice.objects.get(pk=invoice_id)
    result = requests.post(
        "https://api.renderpdfs.com/v1/generate",
        headers={"X-API-Key": settings.RENDERPDFS_KEY},
        json={"template": "invoice", "data": invoice.to_dict(), "store": True},
    ).json()
    invoice.pdf_url = result["url"]
    invoice.save(update_fields=["pdf_url"])

7. Notes & gotchas

Authentication

Every request needs an X-API-Key header. Grab a free key at renderpdfs.com/signup — 100 PDFs/month, no credit card. Treat the key like a password: keep it server-side, never expose it in browser code.

Response format

By default the endpoint streams back the raw PDF binary (Content-Type: application/pdf). Set `store: true` in the body and the response becomes { url, expires_in } — useful for emailing links or attaching to webhooks.

Handling errors

Non-2xx responses return JSON: { error: string }. The most common cases are 401 (bad API key), 402 (over plan quota), and 422 (invalid HTML or URL). Always parse the error body before retrying.

Other languages

Generate your first PDF in 60 seconds

100 free PDFs per month. No credit card. HTML, Markdown, URLs, templates, MCP — all included.