cewe2pdf icon indicating copy to clipboard operation
cewe2pdf copied to clipboard

svglib for svg handling

Open Dobatymo opened this issue 1 year ago • 4 comments

Could https://pypi.org/project/svglib/ be used instead of cairosvg to handle svg/clp files? It would have the advantages that we can remove cairo as a dependency and also insert SVGs as vector files and not rastarized. Not sure if there are any issues I haven't thought about.

Dobatymo avatar Dec 12 '24 16:12 Dobatymo

I have to admit that the graphics handling is not my domain at all, but my initial reaction would be "if it works, leave it alone". I see the theoretical advantage of handling svg directly, but is there any other benefit? And isn't cairo there for more than just the svg handling? The tricky bit would be to make sure all our old albums still produced the "correct" result. But if you're interested from a purely theoretical point of view, try it on your fork and see how it works out!

AnEnglishmanInNorway avatar Dec 13 '24 08:12 AnEnglishmanInNorway

Hi @Dobatymo. Did you ever get around to trying svglib yourself? If so, then you might like the latest pixel-by-pixel test result verification, though I have to confess that I have refactored the code into smaller files which will make it a bit more difficult to merge with your own work. Alterrnatively, if you don't plan to look at svglib, perhaps we can just archive (close!) this issue for now and leave it for another day?

AnEnglishmanInNorway avatar Apr 14 '25 15:04 AnEnglishmanInNorway

Hi @AnEnglishmanInNorway I still would like to use svglib instead. I have been busy with other stuff, however I would like to come back to this here. Having good testing comparison will make it easier. Could we leave it open for now?

Dobatymo avatar Apr 14 '25 16:04 Dobatymo

Thought I'd just add a note here for future use. I just moved to a new win 11 machine, and of course ended up needing a gtk installation to support cairosvg (from https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases). I asked Copilot if it thought svglib would do the job. This is what it said (it even provided me with its "opinion" formatted in markdown for inclusion here!

Comparison: CairoSVG vs svglib for SVG to PDF Conversion

✅ Pros of svglib

  • Integrates with ReportLab to convert SVG to PDF
  • Handles basic shapes, text, and simple vector graphics
  • Easy to use via svg2rlg() and renderPDF.drawToFile()

⚠️ Limitations of svglib

  • Limited support for complex SVG features (e.g., filters, gradients, advanced CSS)
  • Font rendering and layout can be inconsistent with non-standard fonts
  • May misplace text or rasterize vector elements poorly
  • Output fidelity often falls short in high-complexity SVGs

🆚 CairoSVG Strengths

  • More robust rendering, especially for complex SVGs
  • Better support for styles, fonts, and embedded resources
  • Handles headless batch conversions gracefully
  • Paired with GTK runtime, it delivers high-quality vector exports

💡 Recommendation

For clean, simple SVGs, svglib may be sufficient. For high-fidelity PDF output, especially with advanced features, CairoSVG is the preferred choice.

AnEnglishmanInNorway avatar Jul 12 '25 13:07 AnEnglishmanInNorway