Svg to pdf linux

SVG to PDF on a shared linux server

I have a website which uses SVG for an interactive client side thingamabob. I would like to provide the option to download a PDF of the finished output. I can pass the final SVG output back to the server, where I want to convert to PDF, then return it to the client for download. This would need to work on a headless shared linux server, where installation or compilation is either an enormous pain, or impossible. The website is PHP, so the ideal solution would be PHP, or use software that’s easily installed on a shared webserver. Python, perl and ruby are available, along with the usual things you might expect on a linux box. Solutions that involve cairo, scripting inkscape, or installation more complex than ‘FTP it up’ are probably out. Spending large amounts of money are also out, naturally. As this is a shared server, memory and/or CPU hungry solutions are also out, as they will tend to get killed; this more or less rules out Batik. The nearest that I’ve got so far is this XSL transform which I can drive from PHP and then squirt the resulting postscript through ps2pdf (which is already installed). The only problem with this is that it doesn’t support SVG paths — if it did, it would be perfect. There are a bunch or related questions on StackOverflow, all of which I’ve read through, but they all assume that you can either install stuff, spend money, or both. Does anyone have an off-the-shelf solution to this, or should I just spend some downtime trying to add paths support to that XSL transform? Thanks, Dunc

5 Answers 5

I stumbled across TCPDF today which would have been perfect for this, had I known about it at the time. It’s just a collection of pure PHP classes, no external dependencies for most things.

It can build PDF’s from scratch and you can include pretty much anything you want in there, including SVG (amongst many, many other things), as shown in these examples:

Main project page is here:

You can use Apache FOP’s free Batik SVG toolkit which has a transcoder api to transform SVG to PDF.

You will need to write a tiny bit of java. There are code examples here – note you will need to set the transcoder to org.apache.fop.svg.PDFTranscoder instead of Java.

You should be able to do this without installing anything on your machine – just drag the jars on there and run a script. I quote:

All other libraries needed by Batik are included in the distribution. As a consequence the Batik archive is quite big, but after you have downloaded it, you will not need anything else.

Источник

Читайте также:  Matrox g200 linux drivers

free linux command line tool to convert SVG to PDF and/or some commonly-used bitmap format? [closed]

Questions seeking product, service, or learning material recommendations are off-topic because they become outdated quickly and attract opinion-based answers. Instead, describe your situation and the specific problem you’re trying to solve. Share your research. Here are a few suggestions on how to properly ask this type of question.

Is there a free linux command line tool to convert SVG to PDF and/or some commonly-used bitmap format (for example PNG)?

4 Answers 4

Imagemagick is great when rasterized (pixelated) output is what you want (or is at least acceptable), but is a bad choice otherwise, since it effectively embeds in the pdf a rasterized version of whatever you are trying to convert. The whole point of svg/pdf is that it can be vectorized, thereby smaller in size, while remaining smooth at any resolution.

So, I would definitely recommend using either Inkscape or CarioSVG. The latter has several command line utilities precisely for this purpose ( svg2pdf , svg2ps and svg2png ). The only hitch is that it is basically just a python egg, so if you don’t have a python environment set up and aren’t savvy enough (or don’t care enough) to set one up, then that option is a no go. I tried myself, but had problems setting up the required libcairo (not that I tried too hard).

Inkscape is awesome, but the cli is a little clunky if you want just a quick little command to do all the work for you. I put together a couple of little scripts for taking care of this all for me:

#!/bin/bash for i in $@; do inkscape --without-gui --export-pdf="$(basename $i .svg).pdf" $i done 
#!/bin/bash for i in $@; do inkscape --without-gui --export-png="$(basename $i .svg).png" $i done 

Put the first one in ~/bin/svg2pdf and the latter in ~/bin/svg2png , run chmod +x on both of them to make them executable, and boom! You have a quick and easy shortcut for these often wanted operations that doesn’t require you to think or remember how Inkscape’s CLI works. (Obviously you need Inkscape installed before this will work)

Источник

How do I convert an SVG to a PDF on Linux

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.

Читайте также:  Kyocera fs 1060dn linux driver

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 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

The manpage for these programs should explain, wether the program is useful for converting the svg to pdf.

Batch Convert SVG to PDF via command line CLI

Ziconn SVG to PNG converter (Linux)

How do I convert an SVG to a PDF on Linux? (9 Solutions!!)

Convert Images to PDF on Linux

[How to] Convert SVG to PDF (100% FREE & Secure)

pts

Updated on September 18, 2022

Comments

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?

Читайте также:  Отличие потоков от процессов linux

If 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.

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.

That’s a lot of dependencies I’m seeing here: cairo, libgphoto, gtk3, libsane. Oh well, if it does the job.

peterh

NOT TRUE! First line of «man rsvg-convert»: «turn SVG files into raster images.». Misleading, it DOES rasterization, -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 .

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.

Ayberk Özgür

rsvg-convert worked fine, can confirm it doesn’t rasterize. But ALL text in the svg is GONE in the pdf.

@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.

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.

Might save some time searching: On Suse-Systems the package containing rsvg-convert is called rsvg-view .

Pratik Soni

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.

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 ).

You can use the -z (or —without-gui ) flag with Inkscape to run it in batch mode only (no window will open at all).

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.

Benjamin R

Worked perfectly for me in macOS. No rasterisation. brew install librsvg then used rsvg-convert -f pdf -o t.pdf t.svg as above.

Источник

Оцените статью
Adblock
detector