12

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...).

4
  • Did you ever find a decent solution to this?
    – Tom W Hall
    Feb 7, 2013 at 23:36
  • @TomHall: Except for PStill, not any solution. I think it may be too complex for any code to convert many polylines (paths) into simpler shapes.
    – Optavius
    Feb 10, 2013 at 12:18
  • The best option I found so far is to use the "Flatten Form Fields" option in Adobe PDF when saving it as "Optimized PDF" (it is part of "Discard Objects"). This at least removes the nesting, even if it does not necessarily reduce the number of objects.
    – Optavius
    Sep 23, 2013 at 12:25
  • I've been looking for something like too. I absolutely hate the rubbish markup that things like Adobe Illustrator put into their SVGs. I wish there was a developer-friendly tool for creating SVGs with clean markup. May 17 at 11:30

4 Answers 4

6

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.

2
  • jakearchibald.github.io/svgomg built on svgo allows this - try transforming <svg xmlns="http://www.w3.org/2000/svg" width="137.34" height="350"> <g transform="rotate(90,267.715,566.295)"> <g transform="translate(1.42,-2.83)"> <path d="M -300,700 H 50" stroke="tomato" /> </g> </g> <g transform="translate(-10,2.64)"> <path d="m 10,201 h 90" stroke="lime" /> </g> </svg>
    – Ruskin
    Jan 5, 2018 at 8:14
  • I get <svg xmlns="http://www.w3.org/2000/svg" width="137.34" height="350"> <path d="M137 0v350" stroke="tomato"/> <path d="M0 204h90" stroke="#0f0"/> </svg> ... so likely possible via command-line SVGO.
    – Ruskin
    Jan 5, 2018 at 8:14
1

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.

1
  • 1
    Unfortunately, SVG cleaner does not go that far. I really wish there was some ready tool to flatten all this nonsense like <g transform="matrix(0,1,-1,0,845.43,295.94)"> <g transform="translate(1.42,-2.83)"> <path d="m0,841.61h5.67"/> </g> <g transform="translate(1.42,2.27374E-013)"> <path d="m0,841.61h5.67" /> </g>... Dec 21, 2012 at 4:55
1

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 :-)

0

There is an option to flatten transformations in Inkscape Graphics Editor.
So you can use the Inkscape terminal commands to achieve it but you will have to look if such a command exist.

1
  • 1
    Inkscape seems to do the job (at least in the GUI) as far as I can tell, but it is a very limited function: I need to select all objects, then ungroup them, then reselect them and ungroup them, and repeat until I am sure there are no further groups to be ungrouped. Each time this takes a certain processing time, so it becomes quite a tedious process.
    – Optavius
    Aug 14, 2013 at 12:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.