SVG to JPG Java Servlet Program Example

Java Servlet – SVG to JPEG

In this post, we will provide a Java servlet program, that accepts an user uploaded SVG image, and returns a JPEG output, by transcoding the SVG input using Apache Batik library.We will cover the HTML form and servlet program in this article. We would also require Apache Commons library, to convert SVG to JPG, and will introduce them as we move on this tutorial.

Upload SVG Image to Servlet

The first step in the servlet design, is to create a HTML form that will accept an user uploaded SVG Image and post it to a Java program. You can design your own form, or you can use the non-flashy HTML form code below:
<html xmlns="">
<title>SVG to JPEG Conversion - Java Program</title>
<body >
<h1><a>Upload your SVG document</a></h1>
<form id="form_533088" class="appnitro" enctype="multipart/form-data" method="post" action="">
<ul >
<li id="li_1" >
<label for="element_1">Select SVG File To Convert</label>
<input id="element_1" name="element_1" type="file"/> 
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />

Convert SVG to JPG- Full Java Servlet Program

Once the uploaded file is posted to servlet, the program can convert the input SVG document to JPG by using Batik. The Java program that transcodes this and returns a JPEG file in the output stream is provided below:

import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload; 
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.*;

public class SVG2JPGServlet extends HttpServlet {
public SVG2JPGServlet() {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
        OutputStream out = response.getOutputStream();  
        try {
                //we will read the SVG File into the input Stream
                InputStream filecontent=null;           
                List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);          
                for (FileItem item : items) {
                        if (item.isFormField()) {                               
                                String fieldname = item.getFieldName();
                                //NA, you may use it if you want to have more 
                                //inputs to the form
                        } else {
                        //The uploaded file is processed in this section
                        String fieldname = item.getFieldName();
                        String filename = FilenameUtils.getName(item.getName());
                        filecontent = item.getInputStream();
                        //Uploaded file is obtained into Inputstream object at this step                
         //we have obtained the SVG Image to InputStream. We can now convert it into JPEG.
         // Read input to TranscoderInput
         TranscoderInput input_svg_image = new TranscoderInput(filecontent);    
         //Define output
         TranscoderOutput output_jpg_image = new TranscoderOutput(out);     
         //Create a JPEG Transcoder
         JPEGTranscoder my_converter = new JPEGTranscoder();
         my_converter.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,new Float(.9));
         //do the conversion
         //return the output JPG image
         my_converter.transcode(input_svg_image, output_jpg_image);
        catch (Exception e) {
         finally {
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
        doGet(request, response);

Configure SVG to JPG – Servlet

Once you have compiled the servlet code, you have to configure the servlet in TomCat. Use the reference tutorial and configure the servlet on the same lines. You should also change the action against the form to point to the servlet.

Java Servlet – Output Example

A snapshot of the screen at runtime is provided below:

SVG to JPG - Java Servlet Output Example

Against the “Choose File”, you need to select your SVG Image and hit Submit. If you have done all the configurations correctly, you will see an output JPEG image back on the screen.

A small note about JAR files. For this example to work successfully, you will need the following JAR files in TomCat classpath.

batik-anim.jar                 batik-awt-util.jar
batik-bridge.jar               batik-codec.jar
batik-css.jar                  batik-dom.jar
batik-ext.jar                  batik-gvt.jar
batik-parser.jar               batik-rasterizer.jar
batik-script.jar               batik-slideshow.jar
batik-squiggle.jar             batik-svg-dom.jar
batik-svgpp.jar                batik-transcoder.jar
batik-ttf2svg.jar              batik-util.jar
batik-xml.jar                  batik.jar
commons-fileupload-1.2.2.jar   commons-io-2.4.jar
servlet-api.jar                xml-apis-ext.jar

That completes a very quick tutorial to convert SVG to JPG through a Java Servlet program, in Apache Batik. You can try this example yourself and if you have any questions, you can post it in the comments section.

No comments:

Post a Comment