0xDE (11011110) wrote,
0xDE
11011110

An unsuccessful attempt to use CairoSVG to generate small vector-graphics PDF files

The following image shows the onion layers of a 6 × 6 grid (see Sariel's post for context). It consists of 42 circles, 36 with a fill and a stroke and 6 with only a stroke.


My usual workflow is to draw this sort of thing in Adobe Illustrator, but one drawback of doing things this way is huge files: as a PDF file suitable for inclusion in LaTeX, these 42 circles take up 155k bytes. I've recently started cutting my file size by opening the Illustrator PDF in the Apple Preview application, and then using Preview's "export" command with the "reduce file size" filter. This cuts it down to 41k, still nothing impressive but a lot better. It does have the drawback that going between Illustrator and Preview multiple times has sometimes messed up my fonts, so I think it's best viewed as a one-way filter: if I want to re-edit the same file, I need to keep the original.

But if I save as an SVG file from Illustrator (like the one above) it's only 3.3K, a reasonable size for such a simple image. Reading the SVG back into Illustrator and saving as PDF blows it back up to 88k, which is still way too big. So I thought: maybe there's a good command line tool for converting SVG to PDF? I could have a workflow where I use Illustrator only to read and edit SVG files (keeping them around so that I can re-edit them if I want, without as much confusion as keeping two different PDF versions of the same file) and use a different program to convert them to small PDFs.

After some searching, I found CairoSVG, which (after a lot of hassle installing, see below) seemed to work well: it produces a 3.5k byte PDF file from the same image.

The installation process was a bit of a mess. Very different from typical OS X download-and-run installation processes:
  1. Install pip from https://pip.pypa.io/en/stable/installing/
  2. Use pip to install CairoSvg per http://cairosvg.org/
  3. Try running cairosvg from the command line, but it doesn't work because it is unable to load the cairo library. After much searching, discover what the CairoSVG web site never tells you: that cairo is a separate piece of software that you also need to install.
  4. Install MacPorts from https://www.macports.org/install.php
  5. Try using MacPorts to install cairo, but discover that it requires the App version of Xcode and not just the command-line developer tools I already had installed.
  6. Install Xcode from the Apple App store
  7. Try MacPorts again, but discover that the App store install is not the real install.
  8. Run Xcode to install it for real.
  9. Use MacPorts to install cairo per https://www.cairographics.org/
  10. Try running cairosvg from the command line, but it still can't find the library.
  11. Searching the net for the error message eventually leads to https://github.com/Kozea/WeasyPrint/issues/79 which advises setting the environment variable DYLD_FALLBACK_LIBRARY_PATH to /opt/local/lib
  12. It used to be the case that setting environment variables globally was done by editing the file ~/.MacOSX/environment.plist but that no longer works — see http://stackoverflow.com/questions/603785/environment-variables-in-mac-os-x/ — so instead I've been putting them in my .login file.
  13. After editing .login to set the environment variable I need to make a new terminal window because the old ones won't have it set yet.
After all this, the command-line cairosvg runs, and the command line "cairosvg grid-onion.svg -o grid-onion.pdf" produces the small PDF file reported above.

But we're not done...

It turns out that the resulting PDF has a different image size than the original file. After some more groveling on the net I discovered that it's using a default value of 96 dots per inch rather than respecting the 72dpi value used by Illustrator. So when I view the resulting pdf files in Illustrator, Preview, or TeX, they don't have the same scale as when I drew them. This is a problem, because I've been using relative image sizes (the scale= parameter of graphicx) when including images in some of my LaTeX documents.

The cairosvg command line program has two options for changing the scale. We can either set the program to use 72 dpi, or we can tell it to scale the image by a factor of 1.33. Both generate images that have the correct size. But now the bounding box of the image has been destroyed, and it's instead using a bounding box that's way too big. This turns out to be true for svg to png conversion as well: if I try to use cairosvg to make a png from my svg file, it also loses the bounding box. There are no options for telling cairosvg to avoid destroying the bounding box, and no suggestion on the cairosvg issue tracking site that anyone knows or cares about this problem.

After all this, I'm ready to give up on cairosvg as being worth what I've paid for it (nothing). Does anyone know of an svg-to-pdf conversion pipeline that produces small pdf files but actually works?
Tags: tools
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 26 comments