CairoSVG is a Python library that converts SVG files to other formats such as PNG, PDF, and EPS. It is built on top of the Cairo graphics library. The current version is 2.9.0, and it maintains a regular release cadence, including minor feature updates and critical security patches.
pip install cairosvgVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to convert a simple SVG byte string to a PNG and a PDF file using `cairosvg.svg2png` and `cairosvg.svg2pdf`. The `bytestring` parameter expects bytes, and `write_to` expects a file-like object open in binary write mode.
Upgrade your Python environment to at least 3.10 or ensure your CairoSVG version is compatible with your Python version. Always check the `requires_python` field on PyPI.
For unusually large or complex documents with many `<use>` tags, use the `--unsafe` option on the command line or the `unsafe=True` parameter in conversion functions. Review your SVG structure to reduce excessive nesting if possible.
To allow fetching external resources, use the `--unsafe` option on the command line or pass `unsafe=True` or a custom `url_fetcher` parameter to the conversion functions. Exercise caution when enabling this option with untrusted SVG sources.
Upgrade to CairoSVG 2.5.1 or newer immediately to mitigate ReDoS vulnerabilities. This is a critical security update.
Install the necessary system dependencies for Cairo. On Debian/Ubuntu-based systems, this typically involves `sudo apt-get update && sudo apt-get install -y libcairo2-dev libffi-dev pkg-config`. For other operating systems, consult the CairoSVG or cairocffi documentation for specific package names.
Ensure the cairo C library and its development headers are installed on your system. For Debian/Ubuntu-based systems, install `libcairo2-dev`. For Alpine Linux, install `cairo-dev`. Other distributions may have different package names (e.g., `cairo-devel` on RHEL/Fedora).
Install the Cairo graphics library using your operating system's package manager. For macOS, use Homebrew (`brew install cairo`). For Windows, you typically need to install a GTK+ bundle that includes Cairo. For Debian/Ubuntu Linux, use `sudo apt-get install libcairo2 libffi-dev`. For Alpine Linux, use `apk add cairo-dev`.
Change the import statement from `import CairoSVG` to `import cairosvg`.
Install Pillow by running `pip install Pillow`. If the error persists, you might need to install development headers for image libraries on your system, for example, `sudo apt-get install python3-dev libjpeg-dev zlib1g-dev` on Debian/Ubuntu, before reinstalling Pillow.