183

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?

pts
  • 7,200
  • 10
  • 38
  • 49
  • 1
    If you just have a few images to convert you might find it easier to use some of the online converters. I tried [CloudConvert](https://cloudconvert.com/svg-to-pdf) and it did a very good job with half the file size of the SVG. – Frank Breitling Jun 13 '17 at 17:28

12 Answers12

191

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.

notpeter
  • 1,117
  • 9
  • 19
pts
  • 7,200
  • 10
  • 38
  • 49
  • 4
    That's a lot of dependencies I'm seeing here: cairo, libgphoto, gtk3, libsane...Oh well, if it does the job... – Volker Stolz Apr 16 '13 at 15:47
  • NOT TRUE! First line of "man rsvg-convert": "turn SVG files into raster images.". Misleading, it DOES rasterization, -1! – peterh Jul 09 '14 at 11:03
  • 1
    @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 to `rsvg-convert -f pdf`. – pts Jul 09 '14 at 21:30
  • I 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... – Joachim Breitner Nov 07 '14 at 22:03
  • `rsvg-convert` mangled my document prepared with inkscape beyond recognition. – Ayberk Özgür Aug 21 '15 at 14:22
  • rsvg-convert worked fine, can confirm it doesn't rasterize. But ALL text in the svg is GONE in the pdf... – Quandary Oct 21 '15 at 08:21
  • 6
    @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. – AJMansfield Nov 07 '15 at 23:18
  • 2
    Might save some time searching: On Suse-Systems the package containing `rsvg-convert` is called `rsvg-view`. – Trendfischer Jan 22 '16 at 16:14
  • 2
    It 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. – Pratik Soni May 26 '16 at 09:08
  • 1
    On my Mac the output of rsvg-convert is has a bunch of weird artifacts. – Alper Dec 13 '16 at 11:37
  • It resizes the PDF. I wanted it to be the same size of the SVG – hola Jun 03 '17 at 11:14
  • 3
    Worked perfectly for me in macOS. No rasterisation. `brew install librsvg` then used `rsvg-convert -f pdf -o t.pdf t.svg` as above. – Benjamin R Mar 20 '18 at 04:45
  • 1
    Unfortunately, this does not embed the fonts on macOS. – oarfish Oct 12 '18 at 14:26
  • I used "urpmi librsvg" under Mageia Linux. @pts Please consider updating your answer as numerous distros use a different name for this library: https://pkgs.org/download/librsvg I'm going to upvote your answer ;) Thanks. – gouessej Nov 30 '19 at 10:59
  • Unfortunateley, this does not work for SVG 1.1. – stackprotector Oct 26 '20 at 10:12
  • The name of the package in Arch Linux is `librsvg`. – Archisman Panigrahi Dec 12 '21 at 16:40
  • Did not work for a "plain" SVG output from Inkscape. The problem though appears to be Inkscape... – Adam Erickson Jul 06 '22 at 15:23
116

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
pts
  • 7,200
  • 10
  • 38
  • 49
  • 2
    Unfortunately this doesn’t seem to work on OS X. Still, nice answer. – Konrad Rudolph Nov 26 '13 at 23:12
  • 3
    The OP specified that Inkscape had rendering bugs; this matches my experience. – Dylan Thurston Oct 22 '14 at 17:17
  • 3
    On 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. – Alex Schröder Dec 31 '15 at 22:27
  • 2
    Inkscape 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 for `inkscape-bin` (assuming `~/bin` is in your `$PATH`). – 0 _ Dec 21 '16 at 13:08
  • 2
    You can use the `-z` (or `--without-gui`) flag with Inkscape to run it in batch mode only (no window will open at all). – Artefact2 Jan 06 '17 at 18:48
  • 1
    For macOS: `brew cask install xquartz` then `brew cask install inkscape`. – Jonny Jun 29 '18 at 09:06
  • I just found out that inkscape cannot open SVGs exported from Figma. – Jonny Jun 29 '18 at 09:12
  • Seems it rasterizes vector `path`s – Pavel Oct 21 '19 at 11:01
  • Note also that inkscape has changed its command line interface, so this won't work with a more recent inkscape. – David Roundy May 23 '20 at 01:57
  • 4
    It does work it's just that the inkscape cli currently uses the option `--export-filename` https://inkscape.org/doc/inkscape-man.html – Cameron Smith Oct 22 '20 at 01:16
16

I have used CairoSVG successfully on OSX and Ubuntu.

pip install cairosvg
cairosvg in.svg -o out.pdf

CairoSVG Documentation

marc-medley
  • 437
  • 1
  • 7
  • 11
SYK
  • 260
  • 2
  • 5
  • 2
    There 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. – bodo Mar 12 '17 at 10:09
13

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)

adius
  • 937
  • 8
  • 8
Avael Kross
  • 231
  • 2
  • 5
  • 1
    Welcome 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. – fixer1234 Jul 02 '19 at 03:47
  • from WSL, you need some extra flags ```chromium --no-sandbox --disable-setuid-sandbox --headless --disable-gpu --print-to-pdf=output.pdf input.svg``` – ticapix Jul 20 '19 at 20:31
  • Rendering with custom fonts is better than other solution, great ! – themadmax Jun 01 '20 at 13:49
  • 1
    For Red Hat derivates (here Fedora 32) use `chromium-browser` on the command line ... – Erich Kuester Aug 27 '20 at 09:55
  • Why 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... – stackprotector Oct 26 '20 at 10:20
  • @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 tag). – Avael Kross Oct 27 '20 at 01:40
  • @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? – stackprotector Oct 27 '20 at 05:52
10

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.

user unknown
  • 1,802
  • 1
  • 14
  • 25
  • 22
    Thank you for your suggestions. FYI `convert` is not an answer to the original question, because `convert` rasterizes the SVG to a bitmap image, and the original question was looking for a solution which doesn't do that. – pts Jan 22 '12 at 20:15
  • And sometimes you can get the rasterized XML code of the SVG... – Tomasz Gandor May 22 '21 at 21:27
5

https://superuser.com/a/79064/19956 mentions gsvg, part of GhostPDL.

I've tried gsvg ghostpdl-9.06 on Ubuntu Lucid, but it failed for two SVGs generated by Inkscape. One SVG had text in it, the other had only vector graphics. It also failed for simple graphics without Inkscape extensions or clip-path. So I don't consider gsvg a usable SVG-to-PDF converter.

pts
  • 7,200
  • 10
  • 38
  • 49
4

On Mac OS (considering that you already have brew installed) I do:

$ brew install cairo libffi python3
$ pip3 install cairosvg

$ cairosvg -o blah.pdf ./blah.svg 

Same should work on Linux, but with apt-get instead.

Adriel Jr
  • 141
  • 4
4

The answer by @pts doesn't work with recent versions of Inkscape ( 1.0 or newer). To convert an SVG file to PDF:

inkscape --export-type=pdf my_file.svg

This will export it to my_file.pdf.

If you would like to specify a different name for your PDF document, use:

inkscape my_file.svg -o new_name.pdf
s.ouchene
  • 201
  • 1
  • 5
  • works on macos. – Clumsy cat Jan 25 '22 at 13:02
  • You could also add the batch version of this command; as an example: `for f in t.svg u.svg; do echo "file-open:$f; export-type:pdf; export-do"; done | inkscape --shell`. (Syntax is described in the man page, `man inkscape`.) – David Z Aug 10 '22 at 00:10
3

Awesome solution, Avael Kross (https://superuser.com/users/1057244/avael-kross)!! Headless Chrome worked great for me.

I automated the procedure to remove a header and footer from the output PDF and make it fit to the size of the SVG. I uploaded the shell script to the following gist.

svg2pdf.bash: https://gist.github.com/s417-lama/84bf66de1096c4587e8187092fb41684

Usage:

$ ./svg2pdf.bash input.svg output.pdf
s417-lama
  • 31
  • 1
1

Open the svg file with Image Viewer (Also called Eye of Gnome eog) and print it to a PDF file (say image.pdf) and convert that pdf to eps using pdf2ps command. Simple!

Elaborated steps:

  1. Install Image Viewer if not yet done (highly unlikely step if you use gnome)

    sudo apt-get install eog
    
  2. Open svg file with eog and print it to image.pdf file.

  3. (Optional) Remove surrounding whitespace from the pdf file:

    pdfcrop image.pdf
    

    This will generate image-crop.pdf with surrounding whitespace removed.

  4. Convert cropped pdf to eps (use image.pdf directly if you didn't crop the pdf)

    pdf2ps image-crop.pdf image.eps
    

Thats it!

codeman48
  • 111
  • 3
1

Most of the tools mentioned here have problems when displaying SVG 1.1. Therefor, I used the following workaround:

  1. Most browsers seem to be the best viewers for SVG. Firefox and Chrome have full support for SVG 1.1. I just displayed the SVG in the browser.
  2. Then printed the page to PDF, while:
    • fitting the image into the page
    • setting all page borders to 0
    • leaving all headers and footers empty
  3. As you cannot differ from the predefined page sizes, I used pdfcrop afterwards, to get a PDF with the exact dimensions of the SVG.
stackprotector
  • 1,086
  • 1
  • 12
  • 24
0

This can be done using Inkscape

inkscape input.svg -o output.pdf

You can also crop the svg to its content like this:

inkscape input.svg -o output.pdf --export-area-drawing