or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

font-management.mdindex.mdsvg-conversion.mdsvg-path-processing.md
tile.json

tessl/pypi-svglib

A pure-Python library for reading and converting SVG files to ReportLab Graphics drawings

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/svglib@1.5.x

To install, run

npx @tessl/cli install tessl/pypi-svglib@1.5.0

index.mddocs/

SVGLib

A 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.

Package Information

  • Package Name: svglib
  • Language: Python
  • Installation: pip install svglib

Core Imports

from svglib.svglib import svg2rlg

For font management:

from svglib.svglib import register_font, find_font

For utility functions:

from svglib.utils import normalise_svg_path

For package metadata:

from svglib.svglib import __version__, __author__, __license__, __date__

Basic Usage

Python API Usage

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 decompression

Command Line Usage

SVGLib 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/*.svg

Architecture

SVGLib uses a three-layer architecture for SVG processing and conversion:

  • Parser Layer: Uses lxml to parse SVG DOM, handling both plain and compressed (.svgz) files
  • Converter Layer: Transforms SVG elements to ReportLab graphics through attribute and shape converters
  • Renderer Layer: Generates final ReportLab Drawing objects with proper coordinate transformations and styling

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.

Capabilities

SVG Conversion

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
    """

SVG Conversion

Font Management

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
    """

Font Management

SVG Path Processing

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
    """

SVG Path Processing

Command Line Tool

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:

  • Positional: Input SVG file paths (.svg or .svgz)
  • -o, --output PATH_PAT: Output path pattern
  • -v, --version: Print version and exit
  • -h, --help: Show help message

Package Metadata

Access to package version and metadata information.

__version__ = "1.5.1"
__author__ = "Dinu Gherman" 
__license__ = "LGPL 3"
__date__ = "2023-01-07"