PDF generator using HTML and CSS
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.
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
"""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;
}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;
}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)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