Convert SVG to PDF
Some time ago I registered for a virtual training. The training material was made available in a web app. No PDF or a downloadable version of the material. Not a big issue, as long as I had internet access and only wanted to read the material. Problems started when I tried to access the material without a stable Internet connection. Depending where I am online is not an option. Plus point for having training material in PDF format. I can print PDF, making it easier to learn: offline, annotate pages, mark words, easier to read for the eyes. While a cloud solution is good for the vendor, it’s not always a very good option for the consumer.
Taking a closer look I found out that the material was loaded by the web app as SVG files. All pages where available as SVG: page1.svg to pageX.svg. So I could save the SVG files to my computer. Not something feasible for all content, but for selected chapters or pages this can make sense. Now I wanted to have these SVG files as PDF. It’s possible to transform SVG to PDF, especially on Linux. The most used tools available are
- convert
- Inkscape
- rsvg-convert
The challenge was to convert the files to PDF in an acceptable quality. All tools transform SVG to PDF, but the quality differs, and most important: how the images are included. The only tool that rendered the embedded images correctly in the PDF was rsvg-convert. A sample of a page that shows perfectly the problem with the image is this one. The SVG as shown in the browser:
Font is clear and easy to read, the image in correctly embedded and shows all content. Now I wanted to have this page in PDF, with the exact same information included and in a good quality. For this, I tried all three tools and compared the result. All tools were installed and tested in Raspbian.
Convert
Convert is part of Image Magick. To get the tool under Debian you must install imagemagick.
Installation
apt-get install imagemagick
Run
convert page1.svg page1.pdf
Result
After the conversion from SVG to PDF, the result was far away from being useful. The font is not sharp, low quality, and the image is only showing half of the content. I think it’s possible to increase the quality of the font, but that the image isn’t correctly shown is a no-go.
Inkscape
Inkscape is an image editor, and comes with a GUI for end users. But there is a command line option available that can be used to transform a SVG to PDF.
Installation
sudo apt-get install inkscape
Run
inkscape page1.svg --export-area-page --without-gui --export-pdf=page1.pdf
Result
The quality of the font is better. Easy to read, sharp and clear. The image however is still not complete. This disqualifies Inkscape also as an acceptable solution.
rsvg-convert
This tool is part of librsvg. It comes from Gnome and has some “heavy” dependencies. The tool can convert SVG files to images like PNG or PDF.
Installation
sudo apt-get install librsvg2-bin
Run
rsvg-convert -f pdf -o page1.pdf page1.svg
Result
The quality of the font is good, easy to read and clear. The image is correctly shown. Definitely the best solution.
Conclusion
A lot of tools are available to convert SVG files to PDF. I did not even mention libraries available for Java or Javascript to do the job. The difference between the tools is how more complex SVG files are converted into PDF and if all information is included. Especially images can be a challenge for the tools. Based on my tests, I can recommend rsvg-convert. It is fast and gives a very good result.
4 Comments
Delta · December 8, 2018 at 02:04
I’m also happened to be in your situation today. The eLearning site is still not that good and can’t be viewed on my table via Google Book. Thanks for the tips, the PDF looks really great btw.
frank · September 11, 2019 at 09:43
Thanks for your great article, I recommend the VeryUtils SVG to PDF Converter Command Line software, which can be downloaded from this web page, this software works better for me,
https://veryutils.com/svg-to-pdf-converter-command-line
Claus · October 1, 2019 at 23:47
I had the same problem. I was surprised by CairoSVG
LehLeh · August 25, 2020 at 14:47
Finally a real solution for that simple problem. Thank a lot