Registry / serialization / svglib

svglib

JSON →
library2.2.0pypypi✓ verified 24d ago

Svglib is a pure-Python library (current version 1.6.0) for reading SVG files and converting them into ReportLab Drawing objects. These objects can then be rendered to various output formats like PDF, PNG, or EPS using the ReportLab Open Source toolkit. It also includes a command-line tool, `svg2pdf`, for direct SVG to PDF conversion, offering a robust solution for integrating SVG graphics into document generation workflows.

pip install svglib
INSTALL
IMPORT
SIG · SVGLIB
S
svglib
serializationpythonv2.2.0
Install
3.8s avg
Import
1039ms
Disk
58MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 1.068s · 58.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.8s · import 1.010s · 60MB
58MB installed
● package 58MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

svg2rlg
from svglib.svglib import svg2rlg
Primary function to convert an SVG file into a ReportLab Drawing object.
renderPDF
from reportlab.graphics import renderPDF
Used for rendering ReportLab Drawing objects to PDF. Part of the 'reportlab' dependency.
renderPM
from reportlab.graphics import renderPM
Used for rendering ReportLab Drawing objects to pixelmap (e.g., PNG). Part of the 'reportlab' dependency.

This quickstart demonstrates how to convert an SVG file to a ReportLab Drawing object, and then render it to both PDF and PNG formats. It requires a local 'example.svg' file to run, which is created dynamically in the code.

import os from svglib.svglib import svg2rlg from reportlab.graphics import renderPDF, renderPM # Create a dummy SVG file for demonstration dummy_svg_content = ''' <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <rect x="10" y="10" width="180" height="180" fill="blue" stroke="black" stroke-width="3" /> <text x="50" y="100" font-family="sans-serif" font-size="20" fill="white">Hello svglib!</text> </svg> ''' with open("example.svg", "w") as f: f.write(dummy_svg_content) # Convert SVG to ReportLab Drawing object drawing = svg2rlg("example.svg") # Render to PDF renderPDF.drawToFile(drawing, "example.pdf") print("Generated example.pdf") # Render to PNG renderPM.drawToFile(drawing, "example.png") print("Generated example.png") # Clean up dummy file (optional) os.remove("example.svg")
svg2pdf --version
Debug
Known issues
breakingOlder versions (prior to 0.9.4) were vulnerable to XML External Entity (XXE) Injection. Ensure you are using a patched version (0.9.4 or higher) to avoid security risks.
fix
Upgrade `svglib` to version `0.9.4` or newer: `pip install --upgrade svglib`.
affects: <0.9.4
gotchaSvglib does not support all SVG features due to limitations in the underlying ReportLab library. Specifically, color gradients, patterns, markers, clipping paths (beyond single paths), `textPath`, and `ForeignObject` elements are not supported. This can lead to unexpected rendering or missing elements in the output.
fix
Simplify your SVG files to avoid unsupported features or use alternative conversion libraries if full SVG fidelity is required for these specific elements.
affects: All versions
gotchaCSS `@import` rules within SVG stylesheets are ignored by `svglib`. While versions 1.5.0+ avoid crashing, the imported styles will simply not be applied.
fix
Embed CSS directly within the SVG file using `<style>` tags or inline styles instead of using `@import` rules.
affects: All versions
breakingUpgrading `svglib` from very old versions (e.g., 0.8.1 to 0.9.2) could lead to SVG images being rendered in black and white or encountering font errors in the output PDF.
fix
Test rendering thoroughly after upgrading. If issues occur, consult the `svglib` GitHub issues or consider adjusting `reportlab` versions. The current stable `1.x` versions have improved compatibility.
affects: e.g., 0.9.0 - 0.9.3 when upgrading from <0.9.0
deprecatedSupport for Python 3.8 was dropped in `svglib` 1.6.0. Users on older Python environments will need to use an earlier `svglib` version.
fix
Upgrade to Python 3.9+ or pin `svglib` to `<1.6.0` if Python 3.8 support is required (e.g., `pip install 'svglib<1.6.0'`).
affects: 1.6.0 and later
Errors
Common errors & fixes
command not found: svg2pdf
The `svg2pdf` command-line script is not in your system's PATH, often because a virtual environment is not activated or the script's installation location is not recognized.
fix
Activate your virtual environment (if used) or run the script using `python -m svglib.svg2pdf input.svg output.pdf`.
ImportError: cannot import name 'svg2rlg' from 'svglib'
The `svg2rlg` function is located within the `svglib.svglib` submodule, not directly under the top-level `svglib` package.
fix
The correct import statement is `from svglib.svglib import svg2rlg`.
AttributeError: 'Drawing' object has no attribute 'save'
The `Drawing` object returned by `svg2rlg` is a ReportLab graphic representation; it does not have a direct `save` method for file output.
fix
Use a ReportLab renderer like `reportlab.graphics.renderPDF.drawToFile` or a `reportlab.pdfgen.canvas.Canvas` to write the drawing to a file.
```python
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF

drawing = svg2rlg('input.svg')
renderPDF.drawToFile(drawing, 'output.pdf')
```
error: command 'gcc' failed with exit status 1
This error occurs during `pip install svglib` because `svglib` depends on `lxml`, which is a C extension and requires a C compiler (like `gcc`) to be available on your system.
fix
Install the necessary build tools and C compiler for your operating system (e.g., `build-essential` on Debian/Ubuntu, Xcode Command Line Tools on macOS, or Visual C++ Build Tools on Windows) before installing `svglib`.
Upgrade
Version history
2.2.0latest on PyPI · released Aug 14, 2026
Audit
Dependencies
reportlabrequiredCore dependency for converting SVG to ReportLab Drawing objects and rendering to various formats.
lxmlrequiredUsed for parsing and handling SVG CSS stylesheets.
cssselect2requiredUsed for a wider range of CSS selectors.
tinycss2requiredUsed for parsing CSS.
Agent activity
9 hits · last 30 days
node
8
Resources
svglib — pip install svglib · libregistry