Batik Convert SVG to PDF Java Example

Convert SVG Image to PDF Document

In this tutorial, we will briefly discuss how to convert SVG Image to PDF document with a simple Java program. There are different approaches to do this. You can use iText library along with Apache Batik. The example we will provide, is purely based on Batik alone. In a later post, we will describe other approaches for this conversion. So, have a copy of the Batik library files for the code to work.

The step by step guide to convert SVG to PDF is discussed below:

Read SVG Image to Convert

The first step is to read SVG Image into TranscoderInput. This is similar to our SVG to PNG / JPG conversion tutorials. For a change,  we will use Java NIO to read input SVG Image.
        //Step -1: We read the input SVG document into Transcoder Input
        String svg_URI_input = Paths.get("chessboard.svg").toUri().toURL().toString();
        TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input);        


Define PDF Output Stream:

In this step, we define a PDF file as a OutputStream object and attach it to TranscoderOutput. The code to do this is provided below:
        //Step-2: Define OutputStream to PDF file and attach to TranscoderOutput
        OutputStream pdf_ostream = new FileOutputStream("chessboard.pdf");
        TranscoderOutput output_pdf_file = new TranscoderOutput(pdf_ostream);               


Create Transcoder object using FOP:

In this step, we will create a PDFTranscoder object, that is defined in org.apache.fop.svg.PDFTranscoder and create a Transcoder object of type org.apache.batik.transcoder.Transcoder. The PDFTranscoder will be instrumental to write a PDF output of the SVG Image input. An example is provided below:
        // Step-3: Create a PDF Transcoder and define hints
        Transcoder transcoder = new PDFTranscoder();


Convert SVG to PDF

Once we have defined a PDFTranscoder, we can convert SVG to PDF easily by the following code below:
        // Step-4: Write output to PDF format
        transcoder.transcode(input_svg_image, output_pdf_file);
        // Step 5- close / flush Output Stream
        pdf_ostream.flush();
        pdf_ostream.close();        


Complete Java Program Example

The full Java program that converts SVG image to PDF document is provided below:
import java.io.*;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.Transcoder;
import java.nio.file.Paths;
import java.nio.file.Path;
import org.apache.fop.svg.PDFTranscoder;
public class svg2pdf {
    public static void main(String[] args) throws Exception {
        //Step -1: We read the input SVG document into Transcoder Input
        String svg_URI_input = Paths.get("chessboard.svg").toUri().toURL().toString();
        TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input);        
        //Step-2: Define OutputStream to PDF file and attach to TranscoderOutput
        OutputStream pdf_ostream = new FileOutputStream("chessboard.pdf");
        TranscoderOutput output_pdf_file = new TranscoderOutput(pdf_ostream);               
        // Step-3: Create a PDF Transcoder and define hints
        Transcoder transcoder = new PDFTranscoder();
        // Step-4: Write output to PDF format
        transcoder.transcode(input_svg_image, output_pdf_file);
        // Step 5- close / flush Output Stream
        pdf_ostream.flush();
        pdf_ostream.close();        
    }
}

To run this program, you would need all the JAR files specified in SVG to JPEG conversion tutorial. In addition, you will also need the pdf-transcoder.jar file, which is supplied with the Batik distribution.

PDFTranscoder shipped with Batik has greatly simplified the need to convert SVG image to PDF file. In my case, I got an output PDF document of size around 70 Kb. The PDF producer in document properties was set as “Apache FOP Version 1.0beta 2: PDF Transcoder for Batik”

Try the example with your SVG image and let us know how you go. If you are stuck, give us a shout.

4 comments:

  1. Hello,

    You mentioned about setting document properties to “Apache FOP Version 1.0beta 2: PDF Transcoder for Batik”. Where do you define this. what are the different properties so as to create the lease size pdf.

    Also my requirement is to convert dxf file format to svg. could you please let me know how to do it..

    Thanks & Regards,
    Pranav C Lunavat

    ReplyDelete
  2. I wanted to put number of svg images into pdf . Suppose if i get 5 svg images It needs to create one pdf file with 5 pages with 5 different svg images .
    Could you guys help me in regarding this

    ReplyDelete
    Replies
    1. I'm hopelessly searching for the same answer : I'm using Apache FOP to generate PDF but what to insert dynamic SVG inside the pdf. Have you found an answer?

      Delete
  3. What about embedding custom fonts? Is there a way embed custom fonts used in the svg image?

    ReplyDelete