A pure-Python library for reading and converting SVG files to ReportLab Graphics drawings
npx @tessl/cli install tessl/pypi-svglib@1.5.0A pure-Python library for reading and converting SVG files to ReportLab Graphics drawings. SVGLib enables developers to convert SVG graphics into ReportLab Drawing objects that can be used for PDF generation, bitmap conversion, and integration with ReportLab's document generation framework.
pip install svglibfrom svglib.svglib import svg2rlgFor font management:
from svglib.svglib import register_font, find_fontFor utility functions:
from svglib.utils import normalise_svg_pathFor package metadata:
from svglib.svglib import __version__, __author__, __license__, __date__from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF, renderPM
# Convert SVG file to ReportLab Drawing
drawing = svg2rlg("input.svg")
# Render to PDF
renderPDF.drawToFile(drawing, "output.pdf")
# Render to PNG bitmap
renderPM.drawToFile(drawing, "output.png", fmt="PNG")
# Use with compressed SVG files (.svgz)
drawing = svg2rlg("compressed.svgz") # Automatically handles decompressionSVGLib includes svg2pdf, a command-line tool for converting SVG files to PDF:
# Convert single SVG file to PDF
svg2pdf input.svg
# Convert multiple files
svg2pdf file1.svg file2.svgz
# Specify output filename
svg2pdf -o output.pdf input.svg
# Use pattern for batch conversion
svg2pdf -o "%(base)s.pdf" path/*.svg
# Include timestamp in filename
svg2pdf -o "{dirname}/out-{now.hour}-{now.minute}-{now.second}-%(base)s.pdf" path/*.svgSVGLib uses a three-layer architecture for SVG processing and conversion:
The library integrates with ReportLab's graphics system, enabling converted drawings to be used in PDF documents, bitmap rendering, and ReportLab's Platypus framework for document layout.
Core functionality for converting SVG files and streams to ReportLab Drawing objects, with support for compressed files and flexible input formats.
def svg2rlg(path, resolve_entities=False, **kwargs):
"""
Convert an SVG file to an RLG Drawing object.
Parameters:
- path: str, pathlib.Path, or file-like object - SVG file path or stream
- resolve_entities: bool - Enable XML entity resolution
- **kwargs: Additional arguments passed to SvgRenderer
Returns:
Drawing: ReportLab Drawing object
"""Font registration and lookup system for proper text rendering in converted SVG files, with automatic font discovery and ReportLab integration.
def register_font(font_name, font_path=None, weight='normal', style='normal', rlgFontName=None):
"""
Register a font for use in SVG conversion.
Parameters:
- font_name: str - Font family name
- font_path: str, optional - Path to font file
- weight: str - Font weight ('normal', 'bold')
- style: str - Font style ('normal', 'italic')
- rlgFontName: str, optional - ReportLab font name override
Returns:
bool: True if font registered successfully
"""
def find_font(font_name, weight='normal', style='normal'):
"""
Find a registered font by family name, weight, and style.
Parameters:
- font_name: str - Font family name
- weight: str - Font weight
- style: str - Font style
Returns:
str or None: ReportLab font name if found
"""Utility functions for parsing, normalizing, and converting SVG path data and mathematical operations for arc and bezier curve processing.
def normalise_svg_path(attr):
"""
Normalize SVG path data by adding operation codes.
Parameters:
- attr: str - SVG path data string
Returns:
list: Normalized path operations and coordinates
"""A command-line script svg2pdf for direct SVG to PDF conversion from the terminal, with support for batch processing and flexible output patterns.
def svg2pdf(path, outputPat=None):
"""
Convert an SVG file to a PDF file via command line.
Parameters:
- path: str - Input SVG file path (.svg or .svgz)
- outputPat: str, optional - Output filename pattern with placeholders
Available placeholders:
- %(dirname)s: Input file directory
- %(basename)s: Input filename with extension
- %(base)s: Input filename without extension
- %(ext)s: Input file extension
- {now}: Datetime object for timestamps
"""Command Line Arguments:
-o, --output PATH_PAT: Output path pattern-v, --version: Print version and exit-h, --help: Show help messageAccess to package version and metadata information.
__version__ = "1.5.1"
__author__ = "Dinu Gherman"
__license__ = "LGPL 3"
__date__ = "2023-01-07"