devxlogo

Process an XSL-FO Document

Process an XSL-FO Document

Sometimes you need to process an XSL-FO document that is the result of an XSLT transformation. Actually, the XSL-FO document doesn’t exist, it is just a stream of SAX events. In a Java application, this can be done with the FOP processor classes:

//the XML documentC:Data_LocalxmldocsAirWings_xpath_xslt.xml//the XSL stylesheetC:Data_LocalxmldocsAirWings_xslt_fop.xsl//the output - a PDF fileC:Data_LocalxmldocsAirWings_xslt_1.pdf//FOPimport org.apache.fop.apps.Driver; import org.apache.fop.messaging.MessageHandler;//Avalonimport org.apache.avalon.framework.logger.Logger;import org.apache.avalon.framework.logger.ConsoleLogger;//Javaimport java.io.*;import javax.xml.transform.*;import javax.xml.transform.stream.*;import javax.xml.transform.sax.SAXResult;public class XSL_FO_and_XML_XSLT_1{   public static void main(String[] args)      {      OutputStream OS_pdf=null;      Transformer tXML=null;               //create a org.apache.fop.apps.Driver object      Driver fop=new Driver();              //setting  a logger      Logger logger=new ConsoleLogger(ConsoleLogger.LEVEL_INFO);      fop.setLogger(logger);      MessageHandler.setOutputMethod(MessageHandler.SCREEN);      MessageHandler.setScreenLogger(logger);             //specify the PDF format      fop.setRenderer(Driver.RENDER_PDF);                                         //the PDF file      try{         File F_pdf=new File                ("C://Data_Local//xml//docs//AirWings_xslt_1.pdf");         OS_pdf=new FileOutputStream(F_pdf);            fop.setOutputStream(OS_pdf);         }catch(java.io.FileNotFoundException e)            {MessageHandler.error("[***   "+e.getMessage()+"                                    ***]");}                                                 //create a TransformerFactory object      TransformerFactory tfXML=TransformerFactory.newInstance();               //setting the XSL stylesheet, AirWings_xslt_fop.xsl      Source sXSL=new StreamSource(new File              ("C://Data_Local//xml//docs//AirWings_xslt_fop.xsl"));                      //getting a Transformer object      try{         tXML=tfXML.newTransformer(sXSL);         }catch             (javax.xml.transform.TransformerConfigurationException e)            {MessageHandler.error("[*** XSLT Error ***]");}               //setting the XML, AirWings_xpath_xslt.xml      Source sXML=new StreamSource(new File             ("C://Data_Local//xml//docs//AirWings_xpath_xslt.xml"));               //define the Result      Result rXML=new SAXResult(fop.getContentHandler());      //pass sXML and rXML to the transformer      try{         tXML.transform(sXML,rXML);         }catch(javax.xml.transform.TransformerException e)            {MessageHandler.error("[*** XSLT error ***]");}      }}         

Notice that there isn’t an .fo/.fop file.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist