Ok, this one is a little tricky as there isn’t a lot of authoritative and clear information online about how to do this, so I thought it would help to clarify how to do this as it’s a problem I just came across myself and spent a fair amount of time to get this working. Thankfully, this blog post should allow you to just read through it and be able to start converting SVG image to a PNG image in no time.
SVG to PNG Libraries
Firstly let’s look at the core libraries you need to do this. Thankfully there is just the one which is the Apache Batik library which is part of the Apache XML Graphics Project. In summary, Apache Batik is a Java-based toolkit for applications or applets that want to use images in the Scalable Vector Graphics (SVG) format for various purposes, such as display, generation or manipulation.
Now, this is where things start to get a little more challenging. I would recommend downloading the Bin ZIP file from the download link just mentioned. If you take a look around the Maven Repository you’ll notice that there are a lot of different versions which look all very similar, and all of them are generally last updated around 10 years ago. Sure, these things of tools rarely change once built, but this always worries me.
Once you’ve extracted the ZIP, you’re looking for the file titled batik-all-1.12.jar (Note, the version number may differ when you are reading this…) which will be in the folder, /batik-bin-1.12/batik-1.12/lib/batik-all-1.12.jar.
Add that JAR file to your project.
Now you would think that the project developers would have included all of the dependencies already bundled up. But no. And the documentation isn’t clear about all the dependencies which makes life even more difficult.
Dependencies
So, you need a few dependencies to get this up and running.
XML Commons from the Apache Xerces project
Download XML Commons and add the two JAR files to your project called, xml-apis.jar and xml-apis-ext.jar.
XML Graphics Commons from the Apache XML Graphics Project
Yes, this is the same project as where Batik lives, but hey. Download XML Graphics Commons and add the xmlgraphics-commons-2.4.jar file to your project.
Java Code to Convert an SVG into a PNG
Ok, so now we’ve got all the libraries and dependencies added to your project, it’s now time to actually write the code to convert an SVG to a PNG. Use this code as a starting point to get you going;
try { // Use this to Read a Local Path // String svgUriImputLocation = Paths.get("https://www.contradodigital.com/logo.svg").toUri().toURL().toString(); // Read Remote Location for SVG String svgUriImputLocation = "https:// www.contradodigital.com/logo.svg"; TranscoderInput transcoderInput = new TranscoderInput(svgUriImputLocation); // Define OutputStream Location OutputStream outputStream = new FileOutputStream("C:\\Users\\{{Your User Name on Windows}}\\Documents\\logoAsPngFile.png"); TranscoderOutput transcoderOutput = new TranscoderOutput(outputStream); // Convert SVG to PNG and Save to File System PNGTranscoder pngTranscoder = new PNGTranscoder(); pngTranscoder.transcode(transcoderInput, transcoderOutput); // Clean Up outputStream.flush(); outputStream.close(); } catch (IOException | TranscoderException ex) { System.out.println("Exception Thrown: " + ex); }
Hope that helps to get you going in the right direction if you’re working on a similar problem to this.