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
Solution 4
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)
Solution 5
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.
Related videos on Youtube
pts
Updated on September 18, 2022Comments
-
pts over 1 year
How do I convert an SVG (containing a few words of latin text and some simple vector graphics) to a PDF on Linux?
I tried Inkscape 0.47 on Ubuntu Lucid, but it moves some sub-graphics randomly, and it makes some lines shorter in the output PDF. So its output is useless, because the graphics looks completely different.
I tried opening the SVG in Google Chrome 16 and printing it to PDF, but it distorts all the colors, and it also removes some elements. (The SVG appears fine on screen, but it's already bad in the print preview and the generated PDF is also bad)
I don't want to rasterize or render the SVG. A solution which converts the SVG to a bitmap image and then creates a PDF with the image embedded is not an answer to my question. (FYI Inscape 0.47 renders the text is a very ugly way, without antialiasing, when rendering to PNG)
Qre there any other options?
-
Frank Breitling almost 7 yearsIf you just have a few images to convert you might find it easier to use some of the online converters. I tried CloudConvert and it did a very good job with half the file size of the SVG.
-
-
pts over 12 yearsThank you for your suggestions. FYI
convert
is not an answer to the original question, becauseconvert
rasterizes the SVG to a bitmap image, and the original question was looking for a solution which doesn't do that. -
Volker Stolz about 11 yearsThat's a lot of dependencies I'm seeing here: cairo, libgphoto, gtk3, libsane...Oh well, if it does the job...
-
Konrad Rudolph over 10 yearsUnfortunately this doesn’t seem to work on OS X. Still, nice answer.
-
peterh almost 10 yearsNOT TRUE! First line of "man rsvg-convert": "turn SVG files into raster images.". Misleading, it DOES rasterization, -1!
-
pts almost 10 years@PeterHorvath: Thanks for the feedback. I've changed the sentence to:
rsvg-convert -f pdf
doesn't rasterize the SVG. This is true now. Please reconsider your downvote. The first line of the man page (turn SVG files into raster images) is inaccurate, it doesn't apply torsvg-convert -f pdf
. -
Dylan Thurston over 9 yearsThe OP specified that Inkscape had rendering bugs; this matches my experience.
-
Joachim Breitner over 9 yearsI just tried it, and I thought it would rasterize the SVG: A very fine dotpattern got turned into something blurry. But it turns out a problem with my PDF viewer...
-
Ayberk Özgür over 8 years
rsvg-convert
mangled my document prepared with inkscape beyond recognition. -
Quandary over 8 yearsrsvg-convert worked fine, can confirm it doesn't rasterize. But ALL text in the svg is GONE in the pdf...
-
AJMansfield over 8 years@AyberkÖzgür That's inkscape's fault - when you save an Inkscape project, it will by default save it as a SVG, but the SVG it saves includes a bunch of nonstandard inkscape-specific data that can frequently mess up other programs. You need to export as an SVG rather than just saving as a SVG.
-
Alex Schröder over 8 yearsOn OSX using Homebrew, you can install Inkscape using
brew install inkscape
these days. The resulting/usr/local/bin/inkscape
worked for me without having to run X11.app. -
Trendfischer over 8 yearsMight save some time searching: On Suse-Systems the package containing
rsvg-convert
is calledrsvg-view
. -
Pratik Soni almost 8 yearsIt worked for me and the quality of pdf is same as svg. Before this was using imagemagick to convert to pdf and the quality was poor especially for svg.
-
Alper over 7 yearsOn my Mac the output of rsvg-convert is has a bunch of weird artifacts.
-
0 _ over 7 yearsInkscape can be installed on OS X from the
dmg
distributed at its own website, and then called from the command line after creating two symbolic links:ln -s ~/Applications/Inkscape.app/Contents/Resources/bin/inkscape ~/bin/inkscape
and similarly forinkscape-bin
(assuming~/bin
is in your$PATH
). -
Artefact2 over 7 yearsYou can use the
-z
(or--without-gui
) flag with Inkscape to run it in batch mode only (no window will open at all). -
bodo about 7 yearsThere are even python-bindings you can use. Unfortunately I found that this method is rather limited, i.e. a lot of svg features are not supported.
-
hola almost 7 yearsIt resizes the PDF. I wanted it to be the same size of the SVG
-
Benjamin R about 6 yearsWorked perfectly for me in macOS. No rasterisation.
brew install librsvg
then usedrsvg-convert -f pdf -o t.pdf t.svg
as above. -
Jonny almost 6 yearsFor macOS:
brew cask install xquartz
thenbrew cask install inkscape
. -
Jonny almost 6 yearsI just found out that inkscape cannot open SVGs exported from Figma. 😵
-
oarfish over 5 yearsUnfortunately, this does not embed the fonts on macOS.
-
oarfish over 5 yearsThis works with pdf output as well. In contrast to
svg2pdf
andrsvg-convert
, this preserves the fonts. -
Adriel Jr over 5 yearsThanks, I put the wrong extension. I updated the example.
-
mayrop almost 5 yearsWorked like a charm!
-
fixer1234 almost 5 yearsWelcome to Super User! External links can break or be unavailable, in which case your answer would be just a teaser. Even while links still work, the content can't be indexed to help people find the solution. Please include the essential information within your answer and use the link for attribution and further reading. Thanks.
-
ticapix almost 5 yearsfrom WSL, you need some extra flags
chromium --no-sandbox --disable-setuid-sandbox --headless --disable-gpu --print-to-pdf=output.pdf input.svg
-
Pavel over 4 yearsSeems it rasterizes vector
path
s -
gouessej over 4 yearsI used "urpmi librsvg" under Mageia Linux. @pts Please consider updating your answer as numerous distros use a different name for this library: pkgs.org/download/librsvg I'm going to upvote your answer ;) Thanks.
-
David Roundy almost 4 yearsNote also that inkscape has changed its command line interface, so this won't work with a more recent inkscape.
-
themadmax almost 4 yearsRendering with custom fonts is better than other solution, great !
-
Erich Kuester over 3 yearsFor Red Hat derivates (here Fedora 32) use
chromium-browser
on the command line ... -
Cameron Smith over 3 yearsIt does work it's just that the inkscape cli currently uses the option
--export-filename
inkscape.org/doc/inkscape-man.html -
stackprotector over 3 yearsUnfortunateley, this does not work for SVG 1.1.
-
stackprotector over 3 yearsWhy don't you just share your solution for a custom size? How do I have to modify the command to pass the dimensions? Your linked answer does not tell that...
-
Avael Kross over 3 years@Thomas You can't just modify the command to pass the dimensions, you need to modify the svg file itself (according to my linked answer - you need to add styles for
@page
and add width&height in the <svg> tag). -
stackprotector over 3 years@AvaelKross So you have to embed the SVG into a HTML file, correct? And then you pass the HTML file to chrome instead of the SVG?
-
Tomasz Gandor almost 3 yearsAnd sometimes you can get the rasterized XML code of the SVG...
-
Archisman Panigrahi over 2 yearsThe name of the package in Arch Linux is
librsvg
. -
Clumsy cat over 2 yearsworks on macos.
-
Admin almost 2 yearsDid not work for a "plain" SVG output from Inkscape. The problem though appears to be Inkscape...