Skip to main content

A lightweight library to convert TOON format files to PDF using pdfme

Project description

TOON to PDF

A lightweight Python library that converts TOON format files to PDF documents using pdfme as the backend PDF generation engine.

Overview

TOON to PDF provides a simple, declarative way to create PDF documents using the TOON syntax. It acts as a wrapper around pdfme, allowing you to describe PDF structures in a human-readable TOON format and automatically generate professional PDF files.

Installation

Install the library using pip:

pip install toon-to-pdf

Dependencies

The library requires:

  • pdfme - PDF generation engine

The TOON parser is built into the library, so no additional parsing dependencies are needed. The pdfme package will be automatically installed when you install toon-to-pdf.

Quick Start

Hello World Example

Create a simple TOON file (hello_world.toon):

(
  schemas: [
    (
      content: ( type: "text", text: "Hello World" )
      position: ( x: 50, y: 50 )
    )
  ]
  basePdf: "blank"
)

Generate the PDF:

from toon_to_pdf import generate_from_toon_file

generate_from_toon_file("hello_world.toon", "hello_world.pdf")

Or use a TOON string directly:

from toon_to_pdf import generate_from_toon

toon_content = """
(
  schemas: [
    (
      content: ( type: "text", text: "Hello World" )
      position: ( x: 50, y: 50 )
    )
  ]
  basePdf: "blank"
)
"""

generate_from_toon(toon_content, "output.pdf")

TOON Schema Design

The TOON schema is designed to map directly to pdfme's JSON structure. Here's how it works:

Basic Structure

Every TOON document must have:

  • schemas: A list of content elements (text, images, tables, etc.)
  • basePdf: The base PDF template (use "blank" for a blank page)

Text Elements

Each schema entry in the schemas list represents a content element with:

  • content: The content definition (type, text, styling, etc.)
  • position: The position on the page (x, y coordinates)

Example: Simple Invoice

(
  schemas: [
    (
      content: ( type: "text", text: "INVOICE", fontSize: 24, bold: true )
      position: ( x: 50, y: 50 )
    )
    (
      content: ( type: "text", text: "Invoice #: INV-001" )
      position: ( x: 50, y: 100 )
    )
    (
      content: ( type: "text", text: "Date: 2024-01-15" )
      position: ( x: 50, y: 130 )
    )
    (
      content: ( type: "text", text: "Total: $100.00", fontSize: 16, bold: true )
      position: ( x: 50, y: 250 )
    )
  ]
  basePdf: "blank"
)

API Reference

generate_from_toon(toon_input: str, output_path: Union[str, Path]) -> None

Generate a PDF file from a TOON format string.

Parameters:

  • toon_input: TOON format string describing the PDF structure
  • output_path: Path where the generated PDF file will be saved

Raises:

  • ImportError: If required dependencies are not installed
  • ValueError: If the TOON input cannot be parsed or is invalid
  • IOError: If the output file cannot be written

Example:

from toon_to_pdf import generate_from_toon

toon_content = """
(
  schemas: [
    (
      content: ( type: "text", text: "Hello World" )
      position: ( x: 50, y: 50 )
    )
  ]
  basePdf: "blank"
)
"""

generate_from_toon(toon_content, "output.pdf")

generate_from_toon_file(toon_file_path: Union[str, Path], output_path: Union[str, Path]) -> None

Generate a PDF file from a TOON format file.

Parameters:

  • toon_file_path: Path to the input TOON file
  • output_path: Path where the generated PDF file will be saved

Raises:

  • FileNotFoundError: If the TOON file does not exist
  • ValueError: If the TOON input cannot be parsed or is invalid
  • IOError: If the output file cannot be written

Example:

from toon_to_pdf import generate_from_toon_file

generate_from_toon_file("document.toon", "document.pdf")

How It Works

The library performs a simple three-step process:

  1. Parse: Converts the TOON input string into a Python dictionary using the python-toon parser
  2. Transform: Validates and normalizes the data structure to match pdfme's expected format
  3. Generate: Creates the PDF file using pdfme's build_pdf function

Mapping to pdfme Format

The TOON schema directly maps to pdfme's JSON structure:

pdfme JSON:

{
  "schemas": [{
    "content": { "type": "text", "text": "Hello World" },
    "position": { "x": 50, "y": 50 }
  }],
  "basePdf": "blank"
}

Equivalent TOON:

(
  schemas: [
    (
      content: ( type: "text", text: "Hello World" )
      position: ( x: 50, y: 50 )
    )
  ]
  basePdf: "blank"
)

Examples

Check the examples/ directory for more examples:

  • hello_world.toon - Basic "Hello World" example
  • simple_invoice.toon - A simple invoice document

Development

Setup Development Environment

# Clone the repository
git clone https://github.com/yourusername/toon-to-pdf.git
cd toon-to-pdf

# Install in development mode
pip install -e ".[dev]"

Running Tests

pytest

Future Enhancements

  • Support for more pdfme features (tables, images, etc.)
  • Convenience functions for common PDF patterns
  • JavaScript/Node.js version using pdfmake
  • CLI tool for command-line usage
  • Advanced transformation features

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details

Acknowledgments

Support

For issues, questions, or contributions, please visit the GitHub repository.

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

toon_to_pdf-0.1.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

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

toon_to_pdf-0.1.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for toon_to_pdf-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a1568d2ad481d0bcbc0b4720ea0c0db8ba5f14036f03111fc4e5fe1d914a4cd4
MD5 94d3a829033987f8053ed007115cb782
BLAKE2b-256 447364f5aaf3994cb6a6525df66355ad36a21358531cdf1f4ac599285d5a6fcf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: toon_to_pdf-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for toon_to_pdf-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fed6c6cfa8e7c4028f97dc20f87a57cc7c42c665d4ac3aa0eb7c0e40f1e14dba
MD5 d311b420edfeb0625660fe1d37d6ab8a
BLAKE2b-256 e20063b22e70fccc8a8b5b9091800c8c6ed2555ce0efbe0c6139e842656ea334

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