I wonder if there is any command-line tool available to flatten nested groups with transformations in an SVG?
In my particular case, I am converting a CAD-software produced PDF to SVG and then adding some elements and publishing the modified SVG to a web page for viewing. The response time of the SVG is very sluggish (for panning and zooming) and I found that it is due to a very large number of nested groups inside the SVG, often up to several 100 depth. For each element in the SVG, the browser needs to calculate the position across all its parent nodes...
Obviously this is ridiculous, as all these elements need is one single transformation (matrix). So I wonder if anyone knows of a tool to flatten this (or a C# or Delphi implementation that would do so...).
The best I have found is svgo https://github.com/svg/svgo But it is still not satisfactory. SVGO's goal is to produce a smaller svg file. That means if a transformation allows for a smaller svg file it is kept. My purposes is similar to OP's but slightly different. I am looking for something that can flatten out and completely simplify the SVG to just basic paths & elements.
You may have a go with SVG Cleaner. It's unfortunately a GUI tool, so it does not really suit your requirements but it offers a batch processing philosophy that could turn useful.
Thanks Delapouite,
meanwhile I found this one: (PStill). It does a perfect job of flattening the structure (PDF to PDF), but the resulting SVG still has a lot of elements in it making it quite unusable for a browser. It is really the trouble of what CAD systems produce to PDF: A lot of very small paths instead of one longer path. I'm pretty sure there is still room for optimization... one day :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With