views:

585

answers:

4

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 paths - if it did, it would be perfect.

There are a bunch or related question here, 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

A: 

have you looked at imagemagick? I suspect you also need ghostscript to complete the loop, which might make installation difficulty and performance a problem.

Alex Brown
Going via ImageMagick - which is available - rasterizes (afaik), so you end up with a PDF with a bitmap in. I want a scalable vector based PDF, which has the advantage of being infinitely scalable, printable and tiny, file size wise.
dflock
Have another had a play with ImageMagick and it does rasterize, creating a pdf with a bitmap inside. For future reference, it is easy to do though:convert test.svg test.pdfor like this if you want to specify the output resolution:convert -density 1200 test.svg test.pdffor 1200dpi, for example. This tends to create pretty large pdf files though.
dflock
A: 

I'd suggest giving princexml a try, they provide various addons (including one for PHP) and can output PDF from SVG/HTML/XML.

Erik Dahlström
Yeah, I thought someone would suggest that - but a server license for it is 3800 USD - waaay to much money for this, I'm afraid. Thanks for the suggestion though :)
dflock
A: 

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

download link

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.

Alex Brown
note, I haven't used this directly myself, but I have done a bunch of docbook work that uses batik, and it works really well.
Alex Brown
Thanks for the suggestion - I'm aware of Batik (see the question) and would like to try it but I haven't been able to get xmlgraphics.apache.org to load for a week or so now, although http://ismysiteup.org/check/xmlgraphics.apache.org claims it's working; not sure what's up with that.
dflock
+1  A: 

I stumbled across TCPDF today which would have been perfect for this, had I known about it at the time. It's just a collections 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 this example:

http://www.tecnick.com/pagefiles/tcpdf/example_058.phps

Main project page is here:

http://www.tcpdf.org/

Sourceforge page is here:

http://sourceforge.net/projects/tcpdf/

dflock