CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-xhtml2pdf

PDF generator using HTML and CSS

Overview
Eval results
Files

utilities.mddocs/

Utilities

Collection of utility functions for size conversion, color handling, coordinate calculation, text processing, and other common operations used throughout the xhtml2pdf conversion pipeline.

Capabilities

Color and Style Utilities

Functions for handling color values, border styles, and other visual styling attributes.

def getColor(value, default=None):
    """
    Convert color string/value to ReportLab color object.
    
    Args:
        value: Color specification (name, hex, rgb, etc.)
        default: Default color if conversion fails
    
    Returns:
        Color object or default value
    """

def getBorderStyle(value, default=None):
    """
    Get border style from CSS value.
    
    Args:
        value (str): CSS border style value
        default: Default style if not recognized
    
    Returns:
        Border style constant
    """

Size and Dimension Conversion

Functions for converting various size units (px, pt, %, em, etc.) to numeric values.

def getSize(value, relative=0, base=None, default=0.0):
    """
    Convert size strings to numeric values.
    
    Args:
        value: Size specification (e.g., '12px', '1.5em', '100%')
        relative (float): Relative size base
        base: Base size for relative calculations
        default (float): Default value if conversion fails
    
    Returns:
        float: Numeric size value
    """

def getCoords(x, y, w, h, pagesize):
    """
    Calculate coordinates for page positioning.
    
    Args:
        x, y, w, h: Position and dimension values
        pagesize: Page size tuple (width, height)
    
    Returns:
        tuple: Calculated coordinates
    """

def getBox(box, pagesize):
    """
    Parse CSS box dimensions.
    
    Args:
        box: Box specification
        pagesize: Page size for calculations
    
    Returns:
        Box dimensions object
    """

Text and Language Processing

Utilities for text alignment, language detection, and RTL text processing.

def getAlign(value, default=TA_LEFT):
    """
    Get text alignment from CSS value.
    
    Args:
        value (str): CSS text-align value
        default: Default alignment
    
    Returns:
        Alignment constant
    """

def arabic_format(text, language):
    """
    Format Arabic/RTL text properly.
    
    Args:
        text (str): Input text
        language (str): Language code
    
    Returns:
        str: Formatted text for RTL display
    """

def detect_language(name):
    """
    Detect language from text/font name.
    
    Args:
        name (str): Text or font name
    
    Returns:
        str: Detected language code
    """

Data Conversion Utilities

Basic data type conversion functions.

def getBool(s):
    """
    Convert string to boolean value.
    
    Args:
        s: Input value to convert
    
    Returns:
        bool: Boolean value
    """

def getFloat(s):
    """
    Convert string to float value.
    
    Args:
        s: Input value to convert
    
    Returns:
        float: Float value
    """

def toList(value, *, cast_tuple=True):
    """
    Convert value to list format.
    
    Args:
        value: Input value to convert to list
        cast_tuple (bool): Whether to cast tuples to lists
    
    Returns:
        list: Value converted to list format
    """

Advanced Attribute and Object Utilities

Functions for manipulating object attributes and applying transformations.

def transform_attrs(obj, keys, container, func, extras=None):
    """
    Apply function to set of object attributes with container checking.
    
    Args:
        obj: Target object to modify
        keys: List of attribute names to process
        container: Container to check for key existence
        func: Function to apply to attribute values
        extras: Additional parameters for function
    """

def copy_attrs(obj1, obj2, attrs):
    """
    Copy list of attributes from one object to another.
    
    Args:
        obj1: Destination object
        obj2: Source object
        attrs: List of attribute names to copy
    """

def set_value(obj, attrs, value, *, do_copy=False):
    """
    Set same value to multiple object attributes.
    
    Args:
        obj: Target object
        attrs: List of attribute names to set
        value: Value to assign
        do_copy (bool): Whether to copy the value
    """

Frame and Position Utilities

Advanced positioning and frame dimension calculations.

def getFrameDimensions(data, page_width, page_height):
    """
    Calculate frame dimensions for page layout.
    
    Args:
        data: Frame specification data
        page_width (float): Page width in points
        page_height (float): Page height in points
    
    Returns:
        tuple: Frame dimensions (x, y, width, height)
    """

def getPos(position, pagesize):
    """
    Parse position coordinates from string specification.
    
    Args:
        position: Position specification string
        pagesize: Page size tuple (width, height)
    
    Returns:
        tuple: Parsed coordinates (x, y)
    """

Asian Font Support

Utilities for handling Asian fonts and language detection.

def get_default_asian_font():
    """
    Get default Asian font configuration.
    
    Returns:
        str: Default Asian font name
    """

def set_asian_fonts(fontname):
    """
    Configure Asian font mappings.
    
    Args:
        fontname (str): Asian font name to configure
    """

def frag_text_language_check(context, frag_text):
    """
    Check and format text fragment based on language context.
    
    Args:
        context: Processing context with language settings
        frag_text: Text fragment to check
    
    Returns:
        str: Language-appropriate formatted text
    """

Constants

# Size conversion constants
MM = 72.0 / 25.4  # Millimeters to points
DPI96 = 72.0 / 96.0  # 96 DPI to points

# Font size tables
ABSOLUTE_SIZE_TABLE = {  # CSS absolute font sizes
    'xx-small': 6.0,
    'x-small': 7.5,
    'small': 8.9,
    'medium': 10.0,
    'large': 12.0,
    'x-large': 15.0,
    'xx-large': 20.0
}

RELATIVE_SIZE_TABLE = {  # CSS relative font sizes
    'smaller': 0.8,
    'larger': 1.2
}

# Text alignments
ALIGNMENTS = {
    'left': TA_LEFT,
    'center': TA_CENTER,
    'right': TA_RIGHT,
    'justify': TA_JUSTIFY
}

# Color name mappings
COLOR_BY_NAME = {
    'black': (0, 0, 0),
    'white': (1, 1, 1),
    'red': (1, 0, 0),
    # ... extensive color name mappings
}

Install with Tessl CLI

npx tessl i tessl/pypi-xhtml2pdf

docs

command-line.md

context-management.md

css-processing.md

document-processing.md

file-handling.md

index.md

pdf-features.md

utilities.md

wsgi-integration.md

tile.json