Skip to main content

A pure-Python library for reading and converting SVG

Project description

https://github.com/deeplook/svglib/actions/workflows/ci-ubuntu.yml/badge.svg pre-commit.ci status https://pyup.io/repos/github/deeplook/svglib/shield.svg https://img.shields.io/pypi/implementation/svglib.svg https://img.shields.io/pypi/pyversions/svglib.svg https://img.shields.io/pypi/dm/svglib.svg https://img.shields.io/pypi/v/svglib.svg https://img.shields.io/conda/vn/conda-forge/svglib.svg https://img.shields.io/conda/dn/conda-forge/svglib.svg https://img.shields.io/conda/pn/conda-forge/svglib.svg https://img.shields.io/pypi/l/svglib.svg https://static.streamlit.io/badges/streamlit_badge_black_white.svg

About

Svglib is a Python library for reading SVG files and converting them (to a reasonable degree) to other formats using the ReportLab Open Source toolkit.

Used as a package you can read existing SVG files and convert them into ReportLab Drawing objects that can be used in a variety of contexts, e.g. as ReportLab Platypus Flowable objects or in RML. As a command-line tool it converts SVG files into PDF ones (but adding other output formats like bitmap or EPS is really easy and will be better supported, soon).

Tests include a huge W3C SVG test suite plus ca. 200 flags from Wikipedia and some selected symbols from Wikipedia (with increasingly less pointing to missing features).

Features

  • convert SVG files into ReportLab Graphics Drawing objects

  • handle plain or compressed SVG files (.svg and .svgz)

  • allow patterns for output files on command-line

  • install a Python package named svglib

  • install a Python command-line script named svg2pdf

  • provide a PyTest test suite with over 90% code coverage

  • test entire W3C SVG test suite after pulling from the internet

  • test all SVG flags from Wikipedia after pulling from the internet

  • test selected SVG symbols from Wikipedia after pulling from the net

  • support Python 3.9+ and PyPy3

Known limitations

  • @import rules in stylesheets are ignored. CSS is supported, but the range of supported attributes is still limited

  • clipping is limited to single paths, no mask support

  • color gradients are not supported (limitation of reportlab)

  • SVG ForeignObject elements are not supported.

Examples

You can use svglib as a Python package e.g. like in the following interactive Python session:

>>> from svglib.svglib import svg2rlg
>>> from reportlab.graphics import renderPDF, renderPM
>>>
>>> drawing = svg2rlg("file.svg")
>>> renderPDF.drawToFile(drawing, "file.pdf")
>>> renderPM.drawToFile(drawing, "file.png", fmt="PNG")

Note that the second parameter of drawToFile can be any Python file object, like a BytesIO buffer if you don’t want the result to be written on disk for example.

In addition a script named svg2pdf can be used more easily from the system command-line. Here is the output from svg2pdf -h:

usage: svg2pdf [-h] [-v] [-o PATH_PAT] [PATH [PATH ...]]

svg2pdf v. x.x.x
A converter from SVG to PDF (via ReportLab Graphics)

positional arguments:
  PATH                  Input SVG file path with extension .svg or .svgz.

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         Print version number and exit.
  -o PATH_PAT, --output PATH_PAT
                        Set output path (incl. the placeholders: dirname,
                        basename,base, ext, now) in both, %(name)s and {name}
                        notations.

examples:
  # convert path/file.svg to path/file.pdf
  svg2pdf path/file.svg

  # convert file1.svg to file1.pdf and file2.svgz to file2.pdf
  svg2pdf file1.svg file2.svgz

  # convert file.svg to out.pdf
  svg2pdf -o out.pdf file.svg

  # convert all SVG files in path/ to PDF files with names like:
  # path/file1.svg -> file1.pdf
  svg2pdf -o "%(base)s.pdf" path/file*.svg

  # like before but with timestamp in the PDF files:
  # path/file1.svg -> path/out-12-58-36-file1.pdf
  svg2pdf -o {{dirname}}/out-{{now.hour}}-{{now.minute}}-{{now.second}}-%(base)s.pdf path/file*.svg

issues/pull requests:
    https://github.com/deeplook/svglib

Dependencies

Svglib depends mainly on the reportlab package, which provides the abstractions for building complex Drawings which it can render into different fileformats, including PDF, EPS, SVG and various bitmaps ones. Other dependancies are lxml which is used in the context of SVG CSS stylesheets.

Previous versions of this package included a way to run cairo without explicit installation by the user; the dependency that took care of that no longer does this installation, and as such, the user must install cairo themselves. For installation instructions, see the official website: https://www.cairographics.org/download/

Installation

There are three ways to install svglib.

1. Using pip

With the pip command on your system and a working internet connection you can install the newest version of svglib with only one command in a terminal:

$ pip install svglib

You can also use pip to install the very latest version of the repository from GitHub, but then you won’t be able to conveniently run the test suite:

$ pip install git+https://github.com/deeplook/svglib

2. Using conda

If you use Anaconda or Miniconda you are surely using its respective package manager, Conda, as well. In that case you should be able to install svglib using these simple commands:

$ conda config --add channels conda-forge
$ conda install svglib

Svglib was kindly packaged for conda by nicoddemus. See here more about svglib with conda.

3. Manual installation

Alternatively, you can install a tarball like svglib-<version>.tar.gz after downloading it from the svglib page on PyPI or the svglib releases page on GitHub and installing it via pip:

$ pip install svglib-<version>.tar.gz

This will install a Python package named svglib in the site-packages subfolder of your Python installation and a script tool named svg2pdf in your bin directory, e.g. in /usr/local/bin.

Development and Testing

To develop svglib, install uv and run uv sync to install the requirements and development dependencies. To run the test suite with pytest, run: uv run pytest.

Distribution Testing

The svglib tarball distribution contains a PyTest test suite in the tests directory. There, in tests/README.rst, you can also read more about testing. You can run the testsuite e.g. like shown in the following lines on the command-line:

$ make test
========================= test session starts =========================
platform darwin -- Python 3.9.6, pytest-8.4.2, pluggy-1.6.0
rootdir: /Users/dinu/dev/svglib
configfile: pyproject.toml
plugins: cov-7.0.0
collected 115 items

tests/test_basic.py ............................................ [ 43%]
..................                                               [ 53%]
tests/test_fonts.py ............s.....................s.....     [ 88%]
tests/test_samples.py .s.s.s.s.....                              [100%]

============== 109 passed, 6 skipped, 1 warning in 33.32s =============

Bug reports

Please report bugs on the svglib issue tracker on GitHub (pull requests are also appreciated)! If necessary, please include information about the operating system, as well as the versions of svglib, ReportLab and Python being used!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

svglib-1.6.0.tar.gz (1.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

svglib-1.6.0-py3-none-any.whl (39.2 kB view details)

Uploaded Python 3

File details

Details for the file svglib-1.6.0.tar.gz.

File metadata

  • Download URL: svglib-1.6.0.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for svglib-1.6.0.tar.gz
Algorithm Hash digest
SHA256 4c38a274a744ef0d1677f55d5d62fc0fb798819f813e52872a796e615741733d
MD5 d8ed1b50cb0dc544d935f12980037955
BLAKE2b-256 88166b5f9f8c8b18d9ab5112d11e2d6c40c965e3d7ab2948b744685fe6d2f9f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for svglib-1.6.0.tar.gz:

Publisher: publish-to-pypi.yml on deeplook/svglib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file svglib-1.6.0-py3-none-any.whl.

File metadata

  • Download URL: svglib-1.6.0-py3-none-any.whl
  • Upload date:
  • Size: 39.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for svglib-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9aea8e2e81cbbf9c844460e4c7dc90e0a06aea7983bc201975ccd279d7b2d194
MD5 6d0082ca97c5e9469cd08366bc1d522f
BLAKE2b-256 6d9301273a1b8d8454d45f2e18b3d6098c7be13a0864a55fbd0ebda7815c201a

See more details on using hashes here.

Provenance

The following attestation bundles were made for svglib-1.6.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on deeplook/svglib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page