Ibex PDF Creator

Passing java.xml Objects to Ibex

John Farrow, Visual Programming Limited

This example shows how to invoke the PDF creation method of Ibex using existing java.xml.transform.Source and java.xml.transform.Templates objects.

Example Code

The Java code below shows how to create the necessary Java objects to
(1) load XML data into a Source object,
(2) load an XSLT stylesheet into a Templates object and
(3) invoke Ibex to create the PDF file.

        
import java.io.*;
import java.util.Hashtable;

import javax.xml.transform.*;
import javax.xml.transform.stream.*;

import ibex4.*;
import ibex4.logging.*;

public class Build {

  public static void main( String[] args ) {

    if(args.length < 3 ) {
      System.out.println("java Build xml-file xsl-file pdf-file");
      return;
    }

    Hashtable xsltArgs = null;

    FODocument doc = new FODocument();

		Logger.getLogger().addHandler( new ConsoleHandler() );

    try {

        String xmlfile = args[0];
        String xslfile = args[1];
        String pdffile = args[2];

        OutputStream pdfStream = new FileOutputStream( pdffile );
                
        /*
         * Create a Source object for the XML
         */
        Source xml_source = new StreamSource( xmlfile );
                
        /*
         * Create XSL object
         */
        StreamSource xsl_source = new StreamSource( xslfile );
                   
        TransformerFactory transformer_factory = TransformerFactory.newInstance();
                   
        Templates templates = transformer_factory.newTemplates(xsl_source);
                
        /*
         * Call Ibex to create PDF file
         */
                
        doc.generate( xml_source, templates, pdfStream, true, xsltArgs );
                
        Logger.getLogger().info( "count of pages created = " + doc.getPageCount() );
    }
    catch( Exception e ) {
       
       Exception ex = e;
       
       if( doc.Settings.SuppressStackDump) {
          Logger.getLogger().info("caught runtime exception:" + e.getMessage());
       }
       else {
          Logger.getLogger().info("caught runtime exception:");
          ex.printStackTrace();
       }
       
    }

  }
}