How do I convert an SVG to a PDF on Linux
Solution 1:
rsvg-convert did the trick for the SVG I wanted to convert:
$ sudo apt-get install librsvg2-bin
$ rsvg-convert -f pdf -o t.pdf t.svg
rsvg-convert -f pdf
doesn't rasterize the SVG, and it embeds and subsets fonts (at least it has embedded the used characters of the Arial font). Sometimes font embedding fails (e.g. for the LMRoman17 font), and the whole font file gets copied to the generated PDF.
Dependencies on Ubuntu Lucid:
- libcairo.so.2
- libgobject-2.0.so.0
- libgthread-2.0.so.0
- libglib-2.0.so.0
- librsvg-2.so.2
- libpthread.so.0
- libc.so.6
By default, libcairo needs libX11, so rsvg-convert may be hard to install to a headless system.
Note:
The man page of rsvg-convert
states that the tool always rasterizes, but this isn't true. The manual is simply obsolete. Sometimes your svg generating tool can partially rasterize the svg image, which can also mislead you.
Solution 2:
This works on Ubuntu Lucid:
$ sudo apt-get install inkscape
$ inkscape t.svg --export-pdf=t.pdf
The command-line Inkscape invocation above works even in headless mode, without a GUI (DISPLAY=
). However, installing Inscape installs lots of dependencies, including X11.
Please note that the exit status of Inskscape is always 0, even if an error occurs -- so watch out for its stderr.
There is also inkscape --shell
, suitable for converting many documents in a batch. This avoids the slow Inkscape startup time for each file:
$ (echo t.svg --export-pdf=t.pdf;
echo u.svg --export-pdf=u.pdf) |
DISPLAY= inkscape --shell
Inkscape is also useful for simplifying an SVG:
$ DISPLAY= inkscape t.svg --export-plain-svg=t.plain.svg
Solution 3:
I have used CairoSVG successfully on OSX and Ubuntu.
pip install cairosvg
cairosvg in.svg -o out.pdf
CairoSVG Documentation
Solution 4:
I get good results from printing from Inkscape (0.47 too) to PDF, and for saving as PDF (but slightly different), but this might depend on the graphic at hand.
An alternative with lower resolution (I did not try any switches to improve it) is
convert file.svgz file.pdf
convert
is part of the ImageMagick
package.
Rasterizer is another program:
rasterizer -m application/pdf file.svgz -d file.pdf
To find out, which programs which handle svgs are installed on your system, just try
apropos -s 1 svg
The manpage for these programs should explain, wether the program is useful for converting the svg to pdf.
Solution 5:
I'm wondering why it hasn't been mentioned before, but I tested a bunch of different svg->pdf converters and found that the best one is Headless Chrome. It produces the most precise results for me. Before switching to Chrome, I was trying to fight with Inkscape bugs, but many of them are too serious and I can't do much about it (transparency bugs, wrong fonts, etc).
chrome \
--headless \
--disable-gpu \
--print-to-pdf-no-header \
--print-to-pdf=output.pdf \
input.svg
It needs some tweaks to use custom PDF size(A4 is default), but I was able to set custom size after some googling and playing with CSS and SVG attributes (check out this answer on stackoverflow)