Convert SVG to PDF Java Servlet Example Program

Introduction - SVG to PDF Servlet


In this post, we will create a Java servlet program, that will convert an uploaded SVG Image document to PDF file, and return the PDF file as output. Writing this servlet is very easy, as we now know how to 
transform SVG to PDF through standalone Java program. This example will be based on our earlier SVG conversion tutorials, which you can find in the related post section of this blog.

PDF Servlet - Required JAR Files


You would need the complete Batik library in your classpath for this servlet to work properly. The important JAR file that will help the servlet to generate PDF document is pdf-transcoder.jar. So, make sure you have all these in your classpath.

Convert SVG to PDF - Full Java Servlet Program


Before coming to the Java program, make sure you create a HTML form and set the action on your form right, so that it reaches your servlet. The complete Java servlet program that converts SVG image to PDF document is provided below:
import java.io.*;
/* PDF Transcoder helps to create PDF output from the Servlet */
import org.apache.batik.transcoder.Transcoder;
import org.apache.fop.svg.PDFTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
/* basic class files required to support servlet */
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FilenameUtils; 
import java.util.*;
/* class files required to read uploaded SVG document */
import org.apache.commons.fileupload.servlet.ServletFileUpload; 
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.*;
public class SVG2PDFServlet extends HttpServlet {
public SVG2PDFServlet() {
}
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();                         
                        } else {
                        //The uploaded file is processed in this section
                        String fieldname = item.getFieldName();
                        String filename = FilenameUtils.getName(item.getName());
                        //We will have the uploaded SVG Image to filecontent object
                        // at this point
                        filecontent = item.getInputStream();                                    
                        }
                }
         //We can now convert SVG Image to PDF
         TranscoderInput input_svg_image = new TranscoderInput(filecontent);    
         //Output PDF Document points to OutputStream
         TranscoderOutput output_pdf_document = new TranscoderOutput(out);         
         //Create a PDF Transcoder
         Transcoder transcoder = new PDFTranscoder();       
         //set response type
         response.setContentType("application/pdf");
         //return the output PNG image
         transcoder.transcode(input_svg_image, output_pdf_document);
        }
        catch (Exception e) {
               System.err.println(e.toString()); 
         }
         finally {
                 out.close();
         }
        
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
        doGet(request, response);
         }
}

This servlet code generates PDF document from the uploaded SVG image. You can configure this servlet in your TomCat and see how the servlet works for you. If you have any questions, you can post it in the comments section.

No comments:

Post a Comment