PyPDFMake
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.
Metadata
Release files for pypdfmake 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pypdfmake-0.3.0.tar.gz | 45.9 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pypdfmake-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 74.7 kB
Release files / pypdfmake-0.3.0.tar.gz
| Download URL | pypdfmake-0.3.0.tar.gz |
|---|---|
| Size | 45.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
75a7640c6bd0411dbf9daeca54f9aa5a61c3f3e30f52b51f30a661a6e08e8af5
|
|
BLAKE2b-256 checksum How to use checksums |
7fe033264a477af3f241fcc6be387d8ec7084b3fab6808b7796a4b45711c3b45
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 14, 2025.
Transparency logRelease files / pypdfmake-0.3.0-py3-none-any.whl
| Download URL | pypdfmake-0.3.0-py3-none-any.whl |
|---|---|
| Size | 28.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d74344a7f8956b2a2dab545c034a6b8fbd7b7515be040b4f31e1d6ea6ef70c31
|
|
BLAKE2b-256 checksum How to use checksums |
7409391b8a8bc2087e4f1d9dec9777137b87e1983139a675275405fba15e9dfd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 14, 2025.
Transparency logSecurity for pypdfmake 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for pypdfmake-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 14, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.