Skip to main content

A Python library for generating pdfmake document definitions using Pydantic models

Project description

PyPDFMake

PyPI version Python Support License: MIT Development Status Coverage Pydantic v2

A Python library for generating pdfmake document definitions using Pydantic models.

Overview

PyPDFMake provides a type-safe way to create pdfmake document definitions in Python. It uses Pydantic for validation and type checking, making it easier to build complex PDF documents without worrying about the JSON structure expected by pdfmake.

Features

  • Type-safe document definition creation with Pydantic models
  • Support for all pdfmake content types (text, tables, lists, images, etc.)
  • Helper functions for easier creation of common elements
  • Export to JSON for use with pdfmake
  • Python-friendly API with proper typing support

Installation

pip install pypdfmake

Alternatively, if you are using uv:

uv add pypdfmake

Usage

Here's a simple example of creating a PDF document definition:

from pypdfmake import TDocumentDefinitions, ContentText, ContentColumns, Style
import json # used for testing

# Create a document definition
doc_definition = TDocumentDefinitions(
    content=[
        "First paragraph",
        "Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines",
        ContentColumns(
            alignment="justify",
            columnGap=20.0,
            columns=[
                ContentText(text="Lorem ipsum dolor"),
                ContentText(text="Lorem ipsum dolor"),
            ],
        ),
    ],
    styles={
        "header": Style(fontSize=18, bold=True),
        "bigger": Style(fontSize=15, italics=True),
    },
    defaultStyle=Style(fontSize=10)
)

# Convert to JSON for use with pdfmake
json_data = doc_definition.model_dump_json(exclude_none=True)

assert json.loads(json_data) == {
    "content": [
        "First paragraph",
        "Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines",
        {
            "alignment": "justify",
            "columns": [{"text": "Lorem ipsum dolor"}, {"text": "Lorem ipsum dolor"}],
            "columnGap": 20.0,
        },
    ],
    "styles": {
        "header": {"fontSize": 18.0, "bold": True},
        "bigger": {"fontSize": 15.0, "italics": True},
    },
    "defaultStyle": {"fontSize": 10.0},
    "compress": True,
    "creator": "pypdfmake",
}

# Now you can write it to a file or return it in an API response to a front-end

Advanced Usage

The library supports all content types and features available in pdfmake:

  • Text with various formatting options
  • Tables with custom layouts
  • Lists (ordered and unordered)
  • Columns and stacks for layout
  • Images and SVGs
  • QR codes
  • Table of contents
  • Page and text references
  • Canvas for custom drawing
  • Headers, footers, and backgrounds
  • Document metadata

Integration with JavaScript

Since pdfmake is a JavaScript library, you'll need to use the JSON output from PyPDFMake with pdfmake in a JavaScript environment. Here's a simple example:

// In your JavaScript file
const fs = require('fs');
const pdfMake = require('pdfmake');

// Load the JSON created by PyPDFMake
const docDefinition = JSON.parse(fs.readFileSync('document.json', 'utf8'));

// Create the PDF
const printer = new pdfMake.Printer(fonts);
const pdfDoc = printer.createPdfKitDocument(docDefinition);

// Write to a file
pdfDoc.pipe(fs.createWriteStream('document.pdf'));
pdfDoc.end();

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypdfmake-0.3.0.tar.gz (45.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pypdfmake-0.3.0-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

Details for the file pypdfmake-0.3.0.tar.gz.

File metadata

  • Download URL: pypdfmake-0.3.0.tar.gz
  • Upload date:
  • Size: 45.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pypdfmake-0.3.0.tar.gz
Algorithm Hash digest
SHA256 75a7640c6bd0411dbf9daeca54f9aa5a61c3f3e30f52b51f30a661a6e08e8af5
MD5 526df0d568679104b5a81abb8c6c9f28
BLAKE2b-256 7fe033264a477af3f241fcc6be387d8ec7084b3fab6808b7796a4b45711c3b45

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypdfmake-0.3.0.tar.gz:

Publisher: publish.yml on Prose-Eng/pypdfmake

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypdfmake-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pypdfmake-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 28.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pypdfmake-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d74344a7f8956b2a2dab545c034a6b8fbd7b7515be040b4f31e1d6ea6ef70c31
MD5 cdfda8c4f87228ce6999281bd5a55965
BLAKE2b-256 7409391b8a8bc2087e4f1d9dec9777137b87e1983139a675275405fba15e9dfd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypdfmake-0.3.0-py3-none-any.whl:

Publisher: publish.yml on Prose-Eng/pypdfmake

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page