{"id":2299,"library":"svglib","title":"svglib - SVG to ReportLab Graphics Converter","description":"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.","status":"active","version":"1.6.0","language":"en","source_language":"en","source_url":"https://github.com/deeplook/svglib","tags":["svg","graphics","conversion","pdf","png","reportlab","document generation"],"install":[{"cmd":"pip install svglib","lang":"bash","label":"Install latest stable version"}],"dependencies":[{"reason":"Core dependency for converting SVG to ReportLab Drawing objects and rendering to various formats.","package":"reportlab","optional":false},{"reason":"Used for parsing and handling SVG CSS stylesheets.","package":"lxml","optional":false},{"reason":"Used for a wider range of CSS selectors.","package":"cssselect2","optional":false},{"reason":"Used for parsing CSS.","package":"tinycss2","optional":false}],"imports":[{"note":"Primary function to convert an SVG file into a ReportLab Drawing object.","symbol":"svg2rlg","correct":"from svglib.svglib import svg2rlg"},{"note":"Used for rendering ReportLab Drawing objects to PDF. Part of the 'reportlab' dependency.","symbol":"renderPDF","correct":"from reportlab.graphics import renderPDF"},{"note":"Used for rendering ReportLab Drawing objects to pixelmap (e.g., PNG). Part of the 'reportlab' dependency.","symbol":"renderPM","correct":"from reportlab.graphics import renderPM"}],"quickstart":{"code":"import os\nfrom svglib.svglib import svg2rlg\nfrom reportlab.graphics import renderPDF, renderPM\n\n# Create a dummy SVG file for demonstration\ndummy_svg_content = '''\n<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\">\n  <rect x=\"10\" y=\"10\" width=\"180\" height=\"180\" fill=\"blue\" stroke=\"black\" stroke-width=\"3\" />\n  <text x=\"50\" y=\"100\" font-family=\"sans-serif\" font-size=\"20\" fill=\"white\">Hello svglib!</text>\n</svg>\n'''\nwith open(\"example.svg\", \"w\") as f:\n    f.write(dummy_svg_content)\n\n# Convert SVG to ReportLab Drawing object\ndrawing = svg2rlg(\"example.svg\")\n\n# Render to PDF\nrenderPDF.drawToFile(drawing, \"example.pdf\")\nprint(\"Generated example.pdf\")\n\n# Render to PNG\nrenderPM.drawToFile(drawing, \"example.png\")\nprint(\"Generated example.png\")\n\n# Clean up dummy file (optional)\nos.remove(\"example.svg\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade `svglib` to version `0.9.4` or newer: `pip install --upgrade svglib`.","message":"Older 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.","severity":"breaking","affected_versions":"<0.9.4"},{"fix":"Simplify your SVG files to avoid unsupported features or use alternative conversion libraries if full SVG fidelity is required for these specific elements.","message":"Svglib 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Embed CSS directly within the SVG file using `<style>` tags or inline styles instead of using `@import` rules.","message":"CSS `@import` rules within SVG stylesheets are ignored by `svglib`. While versions 1.5.0+ avoid crashing, the imported styles will simply not be applied.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Upgrading `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.","severity":"breaking","affected_versions":"e.g., 0.9.0 - 0.9.3 when upgrading from <0.9.0"},{"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'`).","message":"Support for Python 3.8 was dropped in `svglib` 1.6.0. Users on older Python environments will need to use an earlier `svglib` version.","severity":"deprecated","affected_versions":"1.6.0 and later"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}