Converting an SVG to a PDF programmatically

By Adrian Smith20 Aug 2011400 words2 mins to read

Searching for this topic on the web led to various libraries, all of which were no doubt very powerful, all of which were extremely large and difficult to use if one didn't know how. Plenty of advice existed on the internet, "use library X", but when the library has hundreds of thousands of lines that isn't really enough information. I didn't even mind in which programming language the solution was to be written.

After hours of searching, reading Javadocs, reading source, and experimenting, I finally came up with the following lines of Java code.

Alas, this method can only convert from a SVG file to a PDF file, i.e. these have to be files existing on the disk, they cannot be URLs, in memory, cannot be streamed, and the SVG cannot be an in-memory DOM object. My SVG is dynamic: I'm altering/creating it at runtime (after all, if I wasn't doing that, I could just save the PDF somewhere statically in the first place and wouldn't need to do the conversion). And I want to deliver the PDF to the browser so streaming would be good for me. But no matter, I use File.createTemporaryFile together with deleteOnExit to create these files, and just ignore the fact that if the JVM exits abnormally, e.g. the computer suffers from a power failure, these files will never get deleted, and, over a long enough time-span, will fill up the disk until the computer fails.

Here are the lines:

import org.apache.batik.apps.rasterizer.DestinationType;
import org.apache.batik.apps.rasterizer.SVGConverter;
import ...

// SVG is created programatically as a DOM Document 
Document svgXmlDoc = ...

// Save this SVG into a file 
File svgFile = File.createTempFile("graphic-", ".svg");
svgFile.deleteOnExit();
TransformerFactory tFac = TransformerFactory.newInstance();
Transformer transformer = tFac.newTransformer();
DOMSource source2 = new DOMSource(svgXmlDoc);
FileOutputStream fOut = new FileOutputStream(svgFile);
try { transformer.transform(source2, new StreamResult(fOut)); }
finally { fOut.close(); }

// Convert the SVG into PDF 
File outputFile = File.createTempFile("result-", ".pdf");
outputFile.deleteOnExit();
SVGConverter converter = new SVGConverter();
converter.setDestinationType(DestinationType.PDF);
converter.setSources(new String[] { svgFile.toString() });
converter.setDst(outputFile);
converter.execute();

And I have the following JARs (search using Google to find the projects and download them):

This article was written by Adrian Smith on 20 Aug 2011

Follow me: Facebook | Twitter | Email

More on: Java | Graphics Algorithms