CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-xhtml2pdf

PDF generator using HTML and CSS

Overview
Eval results
Files

css-processing.mddocs/

CSS Processing and Styling

Advanced CSS parsing, cascade processing, and style application system supporting CSS 2.1 and select CSS 3 features for precise document styling and layout control.

Capabilities

CSS Builder and Parser

Specialized CSS processing classes that handle CSS parsing, cascade resolution, and style application for PDF generation.

class pisaCSSBuilder:
    """
    CSS builder specialized for xhtml2pdf PDF generation.
    
    Processes CSS rules and applies them to document elements,
    handling PDF-specific features like page rules and frames.
    """
    def atFontFace(self, declarations):
        """
        Process @font-face CSS rules for custom font loading.
        
        Args:
            declarations: CSS font-face declarations
        """
    
    def atPage(self):
        """
        Process @page CSS rules for page layout and margins.
        
        Handles page size, margins, headers, footers, and page breaks.
        """
    
    def atFrame(self, name, declarations):
        """
        Process @frame CSS rules for advanced page layout.
        
        Creates custom frames for complex multi-column layouts.
        
        Args:
            name (str): Frame name identifier
            declarations: CSS frame declarations
        
        Returns:
            tuple: Processed frame rules and data
        """

    def atPage(self, name, pseudopage, data, *, isLandscape, pageBorder):
        """
        Process @page CSS rules for page layout and margins.
        
        Handles page size, margins, headers, footers, and page breaks.
        
        Args:
            name (str): Page name or None for default
            pseudopage (str): Pseudo-page selector (:first, :left, :right)
            data (dict): Page rule declarations
            isLandscape (bool): Whether page is in landscape orientation
            pageBorder: Page border specifications
        
        Returns:
            tuple: Processed page rules and data
        """

    def get_background_context(data):
        """
        Process background context from CSS data.
        
        Args:
            data (dict): CSS background declarations
        
        Returns:
            dict: Processed background context with position, size, opacity
        """

class pisaCSSParser:
    """
    CSS parser for xhtml2pdf with external stylesheet support.
    
    Extends standard CSS parsing with PDF-specific features
    and external resource loading capabilities.
    """
    def parseExternal(self, cssResourceName):
        """
        Parse external CSS file and integrate with document styles.
        
        Args:
            cssResourceName (str): Path or URL to CSS file
        """

Page Layout and Formatting

CSS support for PDF-specific page layout features including page sizes, margins, headers, footers, and page breaks.

@page {
    size: A4 portrait;
    margin: 2cm;
    
    @top-center {
        content: "Document Title";
    }
    
    @bottom-right {
        content: counter(page);
    }
}

@page :first {
    margin-top: 5cm;
}

@page :left {
    margin-left: 3cm;
    margin-right: 2cm;
}

@page :right {
    margin-left: 2cm;
    margin-right: 3cm;
}

Font Management

Comprehensive font handling system supporting custom fonts, font fallbacks, and international character sets.

@font-face {
    font-family: "Custom Font";
    src: url("path/to/font.ttf");
    font-weight: normal;
    font-style: normal;
}

/* Usage in styles */
.custom-text {
    font-family: "Custom Font", Arial, sans-serif;
    font-size: 12pt;
}

Supported CSS Features

CSS 2.1 Support

  • Box Model: margins, padding, borders, dimensions
  • Typography: font properties, text alignment, line height
  • Colors: named colors, hex values, RGB/RGBA
  • Layout: positioning, floats, display properties
  • Lists: list-style properties and numbering
  • Tables: table layout and styling

CSS 3 Features

  • Border Radius: rounded corners for elements
  • Text Shadow: text shadow effects
  • Background Images: background image positioning and sizing
  • Web Fonts: @font-face for custom fonts

PDF-Specific Extensions

  • Page Rules: @page for page layout control
  • Frame Rules: @frame for multi-column layouts
  • PDF Links: automatic link generation from anchor elements
  • Page Breaks: page-break-before/after properties

CSS Integration Example

from xhtml2pdf import pisa

# Custom CSS with PDF-specific features
css_content = """
@page {
    size: A4;
    margin: 1in;
    
    @top-center {
        content: "Report Header";
        font-size: 10pt;
        color: #666;
    }
}

@font-face {
    font-family: "Report Font";
    src: url("fonts/report.ttf");
}

body {
    font-family: "Report Font", Arial, sans-serif;
    font-size: 11pt;
    line-height: 1.4;
}

.header {
    background-color: #f0f0f0;
    padding: 10pt;
    border-bottom: 2pt solid #333;
    page-break-after: avoid;
}

.section {
    page-break-inside: avoid;
    margin-bottom: 20pt;
}

.footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    font-size: 9pt;
    color: #999;
}
"""

html_content = """
<html>
    <head>
        <style>""" + css_content + """</style>
    </head>
    <body>
        <div class="header">
            <h1>Annual Report</h1>
        </div>
        
        <div class="section">
            <h2>Executive Summary</h2>
            <p>Content here...</p>
        </div>
        
        <div class="footer">
            Page <pdf:pagenumber>
        </div>
    </body>
</html>
"""

# Convert with integrated CSS
with open("styled_report.pdf", "wb") as dest:
    result = pisa.pisaDocument(html_content, dest)

Types

class pisaCSSBuilder:
    """
    CSS builder for PDF-specific style processing.
    
    Handles CSS rule parsing and application with support
    for PDF layout features and custom styling options.
    """

class pisaCSSParser:
    """
    CSS parser with external resource loading capabilities.
    
    Processes CSS files and integrates them with document
    styles while handling PDF-specific CSS extensions.
    """

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