Skip to main content

Text Portable Document Format - AI-friendly PDF generation

Project description

TPDF - Text Portable Document Format

AI-friendly PDF generation library with a simple JSON-based format.

Features

  • 🤖 AI-Friendly: Simple JSON format perfect for LLMs to generate
  • 📄 Multi-Page Support: Create documents with unlimited pages
  • 🎨 Styling: Custom fonts, colors, sizes, and weights
  • 🖼️ Images: Embed images from URLs
  • 🎯 Precise Positioning: X/Y coordinates for exact layout control
  • 🔄 Two-Way Conversion: JSON ↔ PDF

Installation

pip install tpdf

Quick Start

Single Page Document

from tpdf import TPDF

# Create document
doc = TPDF()

# Add content
doc.add_text('Hello, World!', 100, 100, fontSize=24, fontWeight='bold')
doc.add_text('This is TPDF', 100, 150, fontSize=14, color='#666666')
doc.add_image('https://example.com/logo.png', 100, 200, 200, 100)

# Generate PDF
doc.compile_to_pdf('output.pdf')

Multi-Page Document

from tpdf import TPDF

# Create multi-page document
doc = TPDF(multipage=True)

# Page 1
doc.add_page(background='#f0f9ff')
doc.add_text('Cover Page', 306, 400, fontSize=48, fontWeight='bold')

# Page 2
doc.add_page()
doc.add_text('Introduction', 50, 60, fontSize=24)
doc.add_text('Lorem ipsum...', 50, 100, fontSize=12)

# Generate PDF
doc.compile_to_pdf('document.pdf')

Save as TPDF Format

# Save as .tpdf (JSON) for later use
doc.save_tpdf('document.tpdf')

# Load and compile later
from tpdf import TPDFCompiler
import json

with open('document.tpdf', 'r') as f:
    data = json.load(f)

compiler = TPDFCompiler(data)
compiler.compile('output.pdf')

API Reference

TPDF Class

__init__(width=612, height=792, background='#ffffff', multipage=False)

Create a new TPDF document.

Parameters:

  • width: Page width in points (default: 612 = 8.5")
  • height: Page height in points (default: 792 = 11")
  • background: Hex color for background
  • multipage: Enable multi-page mode

add_text(content, x, y, **options)

Add text element.

Parameters:

  • content: Text string
  • x: X coordinate (from left)
  • y: Y coordinate (from top)
  • fontSize: Font size in points (default: 12)
  • fontFamily: 'Helvetica', 'Times', 'Courier' (default: 'Helvetica')
  • color: Hex color (default: '#000000')
  • fontWeight: 'normal' or 'bold' (default: 'normal')
  • fontStyle: 'normal' or 'italic' (default: 'normal')

add_image(url, x, y, width, height)

Add image element.

Parameters:

  • url: Image URL
  • x: X coordinate
  • y: Y coordinate
  • width: Image width in points
  • height: Image height in points

add_page(width=None, height=None, background=None)

Add new page (multipage mode only).

compile_to_pdf(filename)

Generate PDF file.

save_tpdf(filename)

Save as .tpdf JSON file.

TPDF Format Specification

Single Page Format

{
  "version": "1.0",
  "page": {
    "width": 612,
    "height": 792,
    "background": "#ffffff"
  },
  "elements": [
    {
      "type": "text",
      "content": "Hello",
      "x": 100,
      "y": 100,
      "fontSize": 12,
      "fontFamily": "Helvetica",
      "color": "#000000",
      "fontWeight": "normal",
      "fontStyle": "normal"
    },
    {
      "type": "image",
      "url": "https://example.com/image.png",
      "x": 100,
      "y": 200,
      "width": 200,
      "height": 150
    }
  ]
}

Multi-Page Format

{
  "version": "1.0",
  "pages": [
    {
      "width": 612,
      "height": 792,
      "background": "#ffffff",
      "elements": [...]
    },
    {
      "width": 612,
      "height": 792,
      "background": "#f0f0f0",
      "elements": [...]
    }
  ]
}

Use Cases

  • 🤖 AI-Generated Reports: LLMs can easily generate TPDF JSON
  • 📊 Dynamic Documents: Programmatically create PDFs from data
  • 📄 Document Templates: Store layouts as JSON
  • 🔄 Document Pipeline: Convert JSON → PDF in workflows

Examples

See the examples directory for more use cases:

  • Invoice generation
  • Certificate creation
  • Report templates
  • Multi-page proposals

License

MIT License - see LICENSE file for details.

Contributing

Contributions welcome! Please open an issue or submit a PR.

Credits

Built with ReportLab for reliable PDF generation.

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

tpdf-0.2.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

tpdf-0.2.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file tpdf-0.2.0.tar.gz.

File metadata

  • Download URL: tpdf-0.2.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for tpdf-0.2.0.tar.gz
Algorithm Hash digest
SHA256 260c62681e02b6ef78d72e55fc3c7299b80984a82d4c4e65dd78649e85fc46fb
MD5 ddc5e0ae3386e0239c6be098356f5996
BLAKE2b-256 71b19cde2bebc0cf52b93020b4c2d28ecf7619520ba441ca6ef3b8faa0633080

See more details on using hashes here.

File details

Details for the file tpdf-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tpdf-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for tpdf-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32b8cac9db5fb3dc99c0c1038109072ae7c599ef2a1c29309e2cbaff5faf94d4
MD5 c673d21d601881ccd334983595d581ba
BLAKE2b-256 415b1babf71b3979b79307c455f8e7ab05036a594700e321ecd3358ac94c2abd

See more details on using hashes here.

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