A Python library for generating pdfmake document definitions using Pydantic models.
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.
- 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
pip install pypdfmake
Alternatively, if you are using uv:
uv add pypdfmake
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-endThe 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
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();Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.