Tuesday, June 11, 2024

Using Batik Library for SVG to PNG Conversion in Software AG Integration Server

 

Prerequisites:

  1. Ensure you have the necessary JAR files for Apache Batik and its dependencies.
  2. The JAR files must be placed in the code/jars directory of your Integration Server package.

Required JAR Files:

Ensure you have the following JAR files:

  • batik-all-1.9.jar
  • xalan-2.7.2.jar
  • commons-logging-1.2.jar
  • commons-io-2.8.0.jar
  • xmlgraphics-commons-2.2.jar
to avoid any error copy all downloaded JARs into folder code/jars in the package

Steps to Integrate Batik Library

  1. Download JAR Files:

    • You can manually download the required JAR files from this link https://archive.apache.org/dist/xmlgraphics/batik/source/batik-src-1.9.zip
  2. Place JAR Files in Integration Server:

    • Copy the downloaded JAR files to the code/jars directory of your Integration Server package.
           

  1. Restart Integration Server (Optional):

    • Restart the Integration Server to load the new libraries.

Java Service Code for SVG to PNG Conversion

Ensure your Java service is correctly set up to use the Batik library. Here's the code:


import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import org.apache.batik.transcoder.TranscoderInput; import org.apache.batik.transcoder.TranscoderOutput; import org.apache.batik.transcoder.image.PNGTranscoder; import com.wm.data.IData; import com.wm.data.IDataCursor; import com.wm.data.IDataUtil; import com.wm.app.b2b.server.ServiceException; public static final void convertSVGToPNG(IData pipeline) throws ServiceException { // Retrieve input parameters from the pipeline IDataCursor cursor = pipeline.getCursor(); String svgFilePath = IDataUtil.getString(cursor, "svgFilePath"); String pngFilePath = IDataUtil.getString(cursor, "pngFilePath"); cursor.destroy(); if (svgFilePath == null || pngFilePath == null) { throw new ServiceException("Input parameters 'svgFilePath' and 'pngFilePath' are required."); } FileInputStream inputStream = null; FileOutputStream outputStream = null; try { // Create a transcoder instance PNGTranscoder transcoder = new PNGTranscoder(); // Set the input File svgFile = new File(svgFilePath); inputStream = new FileInputStream(svgFile); TranscoderInput input = new TranscoderInput(inputStream); // Set the output File pngFile = new File(pngFilePath); outputStream = new FileOutputStream(pngFile); TranscoderOutput output = new TranscoderOutput(outputStream); // Perform the transcoding transcoder.transcode(input, output); } catch (Exception e) { throw new ServiceException("Error converting SVG to PNG: " + e.getMessage(), e); } finally { try { if (inputStream != null) inputStream.close(); if (outputStream != null) { outputStream.flush(); outputStream.close(); } } catch (Exception e) { // Log the error if any e.printStackTrace(); } } }

Test the Service

  1. Create a Flow Service:

    • Create a flow service that calls the convertSVGToPNG Java service.
    • Map the input parameters (svgFilePath and pngFilePath).
  2. Execute the Flow Service:

    • Run the flow service and check if the PNG file is generated correctly.

By following these steps, you should be able to convert SVG files to PNG using the Batik library in Software AG Integration Server without encountering the WriteAdapter error. If you face any issues, ensure that all dependencies are correctly placed and the server is properly restarted.