Read more

Converting SVG to other vector formats without Inkscape

Arne Hartherz
September 07, 2022Software engineer at makandra GmbH

If you need to convert an SVG source to PS or EPS, the most common suggestion on the interwebs is to use Inkscape from the commandline.
Inkscape is a fairly resource-heavy tool with lots of dependencies Show archive.org snapshot . A great alternative for converting is CairoSVG Show archive.org snapshot .

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

CairoSVG is available on most Linux distros through their package management systems, e.g. apt install cairosvg on Ubuntu.
It has few dependencies (most importantly Python 3 and some related packages, but really not much) and does its job well and quickly.

SVG to PS (PostScript)

Exporting to PS is built into CairoSVG.

cairosvg -o output.ps input.svg

You can also pipe the SVG body into the command (e.g. when your Ruby application generated an SVG string). - references stdin.

cat input.svg | cairosvg -o output.ps -

SVG to PDF

This is also built-in:

cairosvg -o output.pdf input.svg

Note that the PDF document's size is the image's size, not a "human" page size like A4 or US Letter.

SVG to EPS (Encapsulated PostScript)

EPS is ancient (though less ancient than PS), but still relevant for e.g. designers and often preferred over PS.

You can use tools like ps2eps to build upon the PostScript conversion by CairoSVG.
Here, we use the -f switch to tell CairoSVG to generate PS output, but omit the -o switch so CairoSVG writes to stdout. We then pipe the result into ps2eps.

cat input.svg | cairosvg -f ps - | ps2eps -q > output.eps

ps2eps by default reads from stdin and writes to stdout. You can either capture that output in your own application or use > output.eps to write to a file.

Note

ps2eps is actually just a wrapper around the GhostScript tool gs. Technically, you can call gs yourself, but ps2eps does lots of magic for you (it's a Perl script, so you may take a look yourself).

SVG to proprietary file formats

Converting to proprietary formats like .ai often requires the corresponding proprietary software (e.g. Adobe Illustrator).

But Inkscape does a better job for X

If CairoSVG does not work well enough for you and Inkscape does, you probably need to use Inkscape.

SVG to raster image formats

CairoSVG supports writing to PNG: cairosvg -o output.png input.svg. You can specify image dimensions using the --output-width and --output-height flags.

ImageMagick's convert also does a great job turning an SVG into any raster image format. We have a separate card on that topic.

Posted by Arne Hartherz to makandra dev (2022-09-07 08:55)