| 1 |
.. -*- mode: rst -*- |
| 2 |
|
| 3 |
====== |
| 4 |
Svglib |
| 5 |
====== |
| 6 |
|
| 7 |
--------------------------------------------------------------------------- |
| 8 |
A pure-Python library for reading and converting SVG |
| 9 |
--------------------------------------------------------------------------- |
| 10 |
|
| 11 |
.. image:: https://github.com/deeplook/svglib/workflows/CI/badge.svg |
| 12 |
:target: https://github.com/deeplook/svglib/actions?query=workflow%3ACI |
| 13 |
|
| 14 |
.. image:: https://results.pre-commit.ci/badge/github/deeplook/svglib/master.svg |
| 15 |
:target: https://results.pre-commit.ci/latest/github/deeplook/svglib/master |
| 16 |
:alt: pre-commit.ci status |
| 17 |
|
| 18 |
.. image:: https://pyup.io/repos/github/deeplook/svglib/shield.svg |
| 19 |
:target: https://pyup.io/repos/github/deeplook/svglib/ |
| 20 |
|
| 21 |
.. image:: https://img.shields.io/pypi/implementation/svglib.svg |
| 22 |
:target: https://pypi.org/project/svglib |
| 23 |
|
| 24 |
.. image:: https://img.shields.io/pypi/pyversions/svglib.svg |
| 25 |
:target: https://pypi.org/project/svglib |
| 26 |
|
| 27 |
.. image:: https://img.shields.io/pypi/dm/svglib.svg |
| 28 |
:target: https://pepy.tech/project/svglib |
| 29 |
|
| 30 |
.. image:: https://img.shields.io/pypi/v/svglib.svg |
| 31 |
:target: https://pypi.org/project/svglib |
| 32 |
|
| 33 |
.. image:: https://img.shields.io/conda/vn/conda-forge/svglib.svg |
| 34 |
:target: https://github.com/conda-forge/svglib-feedstock |
| 35 |
|
| 36 |
.. image:: https://img.shields.io/conda/dn/conda-forge/svglib.svg |
| 37 |
:target: https://github.com/conda-forge/svglib-feedstock |
| 38 |
|
| 39 |
.. image:: https://img.shields.io/conda/pn/conda-forge/svglib.svg |
| 40 |
:target: https://pypi.org/project/svglib |
| 41 |
|
| 42 |
.. image:: https://img.shields.io/pypi/l/svglib.svg |
| 43 |
:target: https://pypi.org/project/svglib |
| 44 |
|
| 45 |
.. image:: https://static.streamlit.io/badges/streamlit_badge_black_white.svg |
| 46 |
:target: https://share.streamlit.io/deeplook/streamlit-svglib/master/streamlit_app.py |
| 47 |
|
| 48 |
|
| 49 |
About |
| 50 |
----- |
| 51 |
|
| 52 |
``Svglib`` is a pure-Python library for reading SVG_ files and converting |
| 53 |
them (to a reasonable degree) to other formats using the ReportLab_ Open |
| 54 |
Source toolkit. |
| 55 |
|
| 56 |
Used as a package you can read existing SVG files and convert them into |
| 57 |
ReportLab ``Drawing`` objects that can be used in a variety of contexts, |
| 58 |
e.g. as ReportLab Platypus ``Flowable`` objects or in RML_. |
| 59 |
As a command-line tool it converts SVG files into PDF ones (but adding |
| 60 |
other output formats like bitmap or EPS is really easy and will be better |
| 61 |
supported, soon). |
| 62 |
|
| 63 |
Tests include a huge `W3C SVG test suite`_ plus ca. 200 `flags from |
| 64 |
Wikipedia`_ and some selected `symbols from Wikipedia`_ (with increasingly |
| 65 |
less pointing to missing features). |
| 66 |
|
| 67 |
|
| 68 |
Features |
| 69 |
-------- |
| 70 |
|
| 71 |
- convert SVG_ files into ReportLab_ Graphics ``Drawing`` objects |
| 72 |
- handle plain or compressed SVG files (.svg and .svgz) |
| 73 |
- allow patterns for output files on command-line |
| 74 |
- install a Python package named ``svglib`` |
| 75 |
- install a Python command-line script named ``svg2pdf`` |
| 76 |
- provide a PyTest_ test suite with over 90% code coverage |
| 77 |
- test entire `W3C SVG test suite`_ after pulling from the internet |
| 78 |
- test all SVG `flags from Wikipedia`_ after pulling from the internet |
| 79 |
- test selected SVG `symbols from Wikipedia`_ after pulling from the net |
| 80 |
- support Python 3.7+ and PyPy3 |
| 81 |
|
| 82 |
|
| 83 |
Known limitations |
| 84 |
----------------- |
| 85 |
|
| 86 |
- @import rules in stylesheets are ignored. CSS is supported, but the range |
| 87 |
of supported attributes is still limited |
| 88 |
- clipping is limited to single paths, no mask support |
| 89 |
- color gradients are not supported (limitation of reportlab) |
| 90 |
- SVG ``ForeignObject`` elements are not supported. |
| 91 |
|
| 92 |
|
| 93 |
Examples |
| 94 |
-------- |
| 95 |
|
| 96 |
You can use ``svglib`` as a Python package e.g. like in the following |
| 97 |
interactive Python session: |
| 98 |
|
| 99 |
.. code:: python |
| 100 |
|
| 101 |
>>> from svglib.svglib import svg2rlg |
| 102 |
>>> from reportlab.graphics import renderPDF, renderPM |
| 103 |
>>> |
| 104 |
>>> drawing = svg2rlg("file.svg") |
| 105 |
>>> renderPDF.drawToFile(drawing, "file.pdf") |
| 106 |
>>> renderPM.drawToFile(drawing, "file.png", fmt="PNG") |
| 107 |
|
| 108 |
Note that the second parameter of ``drawToFile`` can be any |
| 109 |
`Python file object`_, like a ``BytesIO`` buffer if you don't want the result |
| 110 |
to be written on disk for example. |
| 111 |
|
| 112 |
In addition a script named ``svg2pdf`` can be used more easily from |
| 113 |
the system command-line. Here is the output from ``svg2pdf -h``:: |
| 114 |
|
| 115 |
usage: svg2pdf [-h] [-v] [-o PATH_PAT] [PATH [PATH ...]] |
| 116 |
|
| 117 |
svg2pdf v. x.x.x |
| 118 |
A converter from SVG to PDF (via ReportLab Graphics) |
| 119 |
|
| 120 |
positional arguments: |
| 121 |
PATH Input SVG file path with extension .svg or .svgz. |
| 122 |
|
| 123 |
optional arguments: |
| 124 |
-h, --help show this help message and exit |
| 125 |
-v, --version Print version number and exit. |
| 126 |
-o PATH_PAT, --output PATH_PAT |
| 127 |
Set output path (incl. the placeholders: dirname, |
| 128 |
basename,base, ext, now) in both, %(name)s and {name} |
| 129 |
notations. |
| 130 |
|
| 131 |
examples: |
| 132 |
# convert path/file.svg to path/file.pdf |
| 133 |
svg2pdf path/file.svg |
| 134 |
|
| 135 |
# convert file1.svg to file1.pdf and file2.svgz to file2.pdf |
| 136 |
svg2pdf file1.svg file2.svgz |
| 137 |
|
| 138 |
# convert file.svg to out.pdf |
| 139 |
svg2pdf -o out.pdf file.svg |
| 140 |
|
| 141 |
# convert all SVG files in path/ to PDF files with names like: |
| 142 |
# path/file1.svg -> file1.pdf |
| 143 |
svg2pdf -o "%(base)s.pdf" path/file*.svg |
| 144 |
|
| 145 |
# like before but with timestamp in the PDF files: |
| 146 |
# path/file1.svg -> path/out-12-58-36-file1.pdf |
| 147 |
svg2pdf -o {{dirname}}/out-{{now.hour}}-{{now.minute}}-{{now.second}}-%(base)s.pdf path/file*.svg |
| 148 |
|
| 149 |
issues/pull requests: |
| 150 |
https://github.com/deeplook/svglib |
| 151 |
|
| 152 |
Copyleft by Dinu Gherman, 2008-2021 (LGPL 3): |
| 153 |
http://www.gnu.org/copyleft/gpl.html |
| 154 |
|
| 155 |
|
| 156 |
Dependencies |
| 157 |
------------ |
| 158 |
|
| 159 |
``Svglib`` depends mainly on the ``reportlab`` package, which provides |
| 160 |
the abstractions for building complex ``Drawings`` which it can render |
| 161 |
into different fileformats, including PDF, EPS, SVG and various bitmaps |
| 162 |
ones. Other dependancies are ``lxml`` which is used in the context of SVG |
| 163 |
CSS stylesheets. |
| 164 |
|
| 165 |
|
| 166 |
Installation |
| 167 |
------------ |
| 168 |
|
| 169 |
There are three ways to install ``svglib``. |
| 170 |
|
| 171 |
1. Using ``pip`` |
| 172 |
++++++++++++++++ |
| 173 |
|
| 174 |
With the ``pip`` command on your system and a working internet |
| 175 |
connection you can install the newest version of ``svglib`` with only |
| 176 |
one command in a terminal:: |
| 177 |
|
| 178 |
$ pip install svglib |
| 179 |
|
| 180 |
You can also use ``pip`` to install the very latest version of the |
| 181 |
repository from GitHub, but then you won't be able to conveniently |
| 182 |
run the test suite:: |
| 183 |
|
| 184 |
$ pip install git+https://github.com/deeplook/svglib |
| 185 |
|
| 186 |
|
| 187 |
2. Using ``conda`` |
| 188 |
++++++++++++++++++ |
| 189 |
|
| 190 |
If you use Anaconda_ or Miniconda_ you are surely using its respective package |
| 191 |
manager, Conda_, as well. In that case you should be able to install ``svglib`` |
| 192 |
using these simple commands:: |
| 193 |
|
| 194 |
$ conda config --add channels conda-forge |
| 195 |
$ conda install svglib |
| 196 |
|
| 197 |
``Svglib`` was kindly packaged for ``conda`` by nicoddemus_. See here more about |
| 198 |
`svglib with conda`_. |
| 199 |
|
| 200 |
|
| 201 |
3. Manual installation |
| 202 |
+++++++++++++++++++++++ |
| 203 |
|
| 204 |
Alternatively, you can install a tarball like ``svglib-<version>.tar.gz`` |
| 205 |
after downloading it from the `svglib page on PyPI`_ or the |
| 206 |
`svglib releases page on GitHub`_ and executing a sequence of commands |
| 207 |
like shown here:: |
| 208 |
|
| 209 |
$ tar xfz svglib-<version>.tar.gz |
| 210 |
$ cd svglib-<version> |
| 211 |
$ python setup.py install |
| 212 |
|
| 213 |
This will install a Python package named ``svglib`` in the |
| 214 |
``site-packages`` subfolder of your Python installation and a script |
| 215 |
tool named ``svg2pdf`` in your ``bin`` directory, e.g. in |
| 216 |
``/usr/local/bin``. |
| 217 |
|
| 218 |
|
| 219 |
Testing |
| 220 |
------- |
| 221 |
|
| 222 |
The ``svglib`` tarball distribution contains a PyTest_ test suite |
| 223 |
in the ``tests`` directory. There, in ``tests/README.rst``, you can |
| 224 |
also read more about testing. You can run the testsuite e.g. like |
| 225 |
shown in the following lines on the command-line:: |
| 226 |
|
| 227 |
$ tar xfz svglib-<version>.tar.gz |
| 228 |
$ cd svglib-<version> |
| 229 |
$ PYTHONPATH=. py.test |
| 230 |
======================== test session starts ========================= |
| 231 |
platform darwin -- Python 3.7.3, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 |
| 232 |
rootdir: /Users/dinu/repos/github/deeplook/svglib, inifile: |
| 233 |
plugins: cov-2.4.0 |
| 234 |
collected 36 items |
| 235 |
|
| 236 |
tests/test_basic.py ............................ |
| 237 |
tests/test_samples.py .s.s.s.s |
| 238 |
|
| 239 |
=============== 32 passed, 4 skipped in 49.18 seconds ================ |
| 240 |
|
| 241 |
|
| 242 |
Bug reports |
| 243 |
----------- |
| 244 |
|
| 245 |
Please report bugs on the `svglib issue tracker`_ on GitHub (pull |
| 246 |
requests are also appreciated)! |
| 247 |
If necessary, please include information about the operating system, as |
| 248 |
well as the versions of ``svglib``, ReportLab and Python being used! |
| 249 |
|
| 250 |
|
| 251 |
.. _SVG: http://www.w3.org/Graphics/SVG/ |
| 252 |
.. _W3C SVG test suite: |
| 253 |
http://www.w3.org/Graphics/SVG/WG/wiki/Test_Suite_Overview |
| 254 |
.. _flags from Wikipedia: |
| 255 |
https://en.wikipedia.org/wiki/Gallery_of_sovereign_state_flags |
| 256 |
.. _symbols from Wikipedia: |
| 257 |
https://en.wikipedia.org/wiki/List_of_symbols |
| 258 |
.. _ReportLab: https://www.reportlab.com/opensource/ |
| 259 |
.. _RML: https://www.reportlab.com/software/rml-reference/ |
| 260 |
.. _svglib issue tracker: https://github.com/deeplook/svglib/issues |
| 261 |
.. _PyTest: http://pytest.org |
| 262 |
.. _svglib page on PyPI: https://pypi.org/project/svglib/ |
| 263 |
.. _svglib releases page on GitHub: https://github.com/deeplook/svglib/releases |
| 264 |
.. _Python file object: https://docs.python.org/3/glossary.html#term-file-object |
| 265 |
.. _Anaconda: https://www.anaconda.com/download/ |
| 266 |
.. _Miniconda: https://conda.io/miniconda.html |
| 267 |
.. _Conda: https://conda.io |
| 268 |
.. _svglib with conda: https://github.com/conda-forge/svglib-feedstock |
| 269 |
.. _nicoddemus: https://github.com/nicoddemus |