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.1.0.tar.gz (8.4 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.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tpdf-0.1.0.tar.gz
Algorithm Hash digest
SHA256 541da6a91835f3ee3220d6da84ab3ceb75b00c7ffbaf30c3095ea893ca19ea93
MD5 3bf7bed9aef946fa618f15af16629926
BLAKE2b-256 0a54e5df0284549665691adce4c889f7f83d267746f77a6efb0eb53094d2dc6b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tpdf-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec202aa95ce90734919a7eb732c311b110f1f69199add9d0f6cb3a53175bc282
MD5 cc660d1ccff6d2500a59452ce3480b2f
BLAKE2b-256 d57a94ceb738a5fb47d4248c0a44893c8837ad1abb8f9435b6bc1031d4f79a8b

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