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.1.tar.gz (9.1 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.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tpdf-0.1.1.tar.gz
  • Upload date:
  • Size: 9.1 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.1.tar.gz
Algorithm Hash digest
SHA256 e4b8b9ede8d1e04ed39ad3c152acd20216818bc3ca61440ea57704ba0951dddf
MD5 0a6b5f13ce1c127e95fd2d7c89977b3f
BLAKE2b-256 550c8d1489eae2c5b6610dc090896a26a03d54dbaf0cd8fd704d66f9f5a0628f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tpdf-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dbb771a85f640d58463555ae318fb57cb837712a1bcf022ac24c37982077d067
MD5 4fc016815920ab9a33a7fc88ded43fea
BLAKE2b-256 b9fd74365595e83da9a556efe8fcc3393c75d8dc6c1bd1cfddb0c0a375bed6e1

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