svglib
A pure-Python library for reading SVG files and converting them to PDF and other formats.
Repository Health
Technical Analysis
Svglib is a pure-Python library that reads SVG (and compressed SVGZ) files and converts them into ReportLab Drawing objects, which can then be rendered to PDF and other formats. Because it builds on the ReportLab toolkit, converted graphics slot directly into Platypus flowables and other ReportLab document workflows.
Beyond the importable API, svglib installs an svg2pdf command-line tool for one-shot conversions. It supports gradients, conditional <switch> rendering, CSS styling, and ships with an extensive test suite covering the W3C SVG conformance suite and hundreds of real-world SVGs.
What You Get
- A
svg2rlgfunction that parses SVG/SVGZ files into ReportLabDrawingobjects - An
svg2pdfcommand-line script for batch converting SVG files to PDF with output patterns - Support for linear and radial gradients, CSS styling, and conditional
<switch>rendering - Integration with ReportLab Platypus so drawings embed directly into generated documents
- A large PyTest suite covering the W3C SVG test suite and hundreds of real-world flags and symbols
Common Use Cases
- Embedding SVG artwork and charts into ReportLab-generated PDF documents
- Batch converting directories of SVG files to PDF from the command line
- Rendering vector graphics in pure Python without Cairo or system dependencies
- Turning SVG icons and logos into printable report assets
Under The Hood
Architecture - The core is src/svglib/svglib.py, which walks the parsed SVG DOM and translates each element into ReportLab graphics primitives, assembling them into a Drawing. Companion modules keep concerns separated: fonts.py handles font resolution, utils.py provides geometry and color helpers, and the package __init__.py exposes the svg2rlg entry point plus the main function wired to the svg2pdf console script.
Tech Stack - Pure Python (3.9+ and PyPy3) depending on ReportLab for the graphics/PDF backend and lxml for SVG parsing. It uses a modern hatchling build backend, uv.lock for reproducible dev environments, and tox for multi-version testing.
Code Quality - The project advertises and maintains over 90% coverage via a PyTest suite in tests/, including the full W3C SVG conformance suite and ~200 Wikipedia flags pulled at test time. It ships py.typed, runs pre-commit CI, and documents known limitations candidly in the README.
API Design - The public API is intentionally minimal: svg2rlg(path) returns a Drawing, and renderPDF (from ReportLab) writes it out, so a full SVG-to-PDF conversion is two lines. The svg2pdf CLI mirrors this for shell use, and output filename patterns make batch conversion ergonomic.