How to generate a PDF with Python

Generate PDFs in Python using a clean SDK designed for Django, Flask, and FastAPI apps. Convert HTML or URLs into PDFs that render with modern CSS.

import { GeneratePDFs } from '@generatepdfs/node-sdk';

const client = GeneratePDFs.connect('YOUR_API_TOKEN');

// Generate from HTML file with CSS
const pdf = await client.generateFromHtml(
    '/path/to/file.html',
    '/path/to/file.css'
);

// Or from a URL
const pdf = await client.generateFromUrl('https://example.com');
from generatepdfs import GeneratePDFs

client = GeneratePDFs.connect('YOUR_API_TOKEN')

# Generate from HTML file with CSS
pdf = client.generate_from_html(
    '/path/to/file.html',
    '/path/to/file.css'
)

# Or from a URL
pdf = client.generate_from_url('https://example.com')
<?php
use GeneratePDFs\GeneratePDFs;

$client = GeneratePDFs::connect('YOUR_API_TOKEN');

// Generate from HTML file with CSS
$pdf = $client->generateFromHtml(
    '/path/to/file.html',
    '/path/to/file.css'
);

// Or from a URL
$pdf = $client->generateFromUrl('https://example.com');
<?php
use GeneratePDFs\Laravel\Facades\GeneratePDFs;

// Generate from HTML file with CSS
$pdf = GeneratePDFs::generateFromHtml(
    '/path/to/file.html',
    '/path/to/file.css'
);

// Or from a URL
$pdf = GeneratePDFs::generateFromUrl('https://example.com');
require 'generatepdfs'

client = GeneratePDFs::GeneratePDFs.connect('YOUR_API_TOKEN')

# Generate from HTML file with CSS
pdf = client.generate_from_html(
    '/path/to/file.html',
    '/path/to/file.css'
)

# Or from a URL
pdf = client.generate_from_url('https://example.com')

Generate PDFs in Python for web apps and data tasks

Convert Django or Flask templates to PDF

Render templates in Django, Flask, or Jinja2 and send the HTML to the SDK. The browser based renderer keeps your CSS and fonts aligned with the web output. For HTML to PDF conversion, see the main guide. If you use WeasyPrint, see the Alternative to WeasyPrint.

Generate PDFs from URLs in Python

Send a URL to the API to capture reports, receipts, or portal pages without managing headless browser processes.

Background processing with Celery

Generate PDFs in Celery, RQ, or Dramatiq workers, then store the output in S3 or your storage layer. For CSS Grid and Flexbox support and pixel perfect HTML to PDF rendering, see our guides. Use the PDF generation API with the Python SDK.

Install the Python SDK with pip

  1. Install the GeneratePDFs Python SDK with pip in your virtual environment.
  2. Store the API token in environment variables and load it in settings.
  3. Add a small service class that wraps GeneratePDFs.connect() for reuse.
  4. Call generate_from_html or generate_from_url from a view or task.
  5. Write the PDF to object storage or return it from your API endpoint.

Why Python teams use GeneratePDFs

๐Ÿงช

Readable SDK

Clean method names that fit Python service layers.

๐ŸŒ

No headless setup

A hosted API avoids Chromium installs and system deps.

๐Ÿ“ฆ

Browser accurate output

HTML and CSS render the same in the PDF.

Common Python PDF workflows

๐Ÿงพ

Billing documents

Generate PDFs from Django templates and invoice data.

๐Ÿ“Š

Data reports

Create PDF exports from analytics dashboards and charts.

๐Ÿ“„

Batch generation

Produce large PDF batches in background workers.

Generate PDFs in Python with Django or Flask HTML

Use the Python SDK to convert HTML or URLs into PDFs that keep your fonts, colours, and layout.