Use the Apache XML project FOP to generate a PDF file (below)

xiaoxiao2021-03-06  51

Use the Apache XML project FOP to generate a PDF file (below)

Fourth, content:

2, (2) xmlàxsl-foàpdf:

Sometimes we will encounter an XML file that you want to have only data, no format, and we need a JAXP-compatible XSLT processor: such as Apache's Xalan (or IBM Xerces, Sun) JDOM, etc.), add its library file to classpath. With JAXP's API, we can use XSLT to convert the XML file to the XML-FO file, and then convert the XML-FO file to PDF. The processing flow is as follows:

XLS-FO

PDF

FOP

Xml

Xslt

JAXP

(A) The first step let's take a look at how to convert the XML file into the XLS-FO file, the process process is as follows:

XLS-FO

Xml

Xslt

JAXP

// Example 2: ExampleXML2FO.JAVA

// java

Import java.io.file;

Import java.io.ioException;

Import java.io.outputstream;

// jaxp

Import javax.xml.transform.transformer;

Import javax.xml.transform.transformerfactory;

Import javax.xml.transform.transformerexception;

Import javax.xml.transform.source;

Import javax.xml.transform.result;

Import javax.xml.transform.Stream.streamResult;

Import javax.xml.transform.Stream.streamsource;

// Avalon

Import org.apache.avalon.framework.exceptionUtil;

/ **

* This class demonstrate the conversion of an xml file to an xsl-fo file

* Using JAXP (XSLT).

* /

Public class example examplexml2fo {

Public Void ConvertXML2FO (File XML, File XSLT, File Fo)

THROWS IOEXCEPTION, TRANSFORMEREXCEPTION {

// setup OUTPUT

OutputStream out = new java.io.fileoutputstream (fo);

Try {

// setup XSLT

TransformerFactory Factory = TransformerFactory.newinstance ();

Transformer Transformer = Factory.NewTransformer (New Streamsource (XSLT));

// setup input for xslt transport

Source src = new streamsource (XML);

// RESULTING SAX Events (The Generated Fo) Must Be Piped THROUGH To FOP

Result res = new streamResult (out);

// Start Xslt Transformation and Fop Processing

Transformer.Transform (SRC, RES);

} finally {out.close ();

}

}

Public static void main (String [] args) {

Try {

System.out.println ("FOP EXAMPLEXML2FO / N");

System.out.println ("preplay ...");

// setup Directories

FILE Basedir = New File (".");

// setup Input and Output Files

File XMLFile = New File (Basedir, "ProjectTeam.xml);

File Xsltfile = New File (Basedir, "Projectteam2fo.xsl");

File Fofile = New File (Basedir, "Resultxml2fo.fo");

System.out.println ("INPUT: XML (" XMLFile ")")

System.out.println ("Stylesheet:" xsltfile);

System.out.println ("OUTPUT: XSL-FO (" FOFILE ")")

System.out.println ();

System.out.println ("Transforming ...");

ExampleXML2FO App = New ExampleXML2FO ();

App.convertXML2FO (XMLFile, Xsltfile, FOFILE);

System.out.println ("Success!");

} catch (exception e) {

System.rr.println (ExceptionUtil.PrintStackTrace (e));

System.exit (-1);

}

}

}

Compilation The above program uses the XSLT file ProjectTeam2fo.xsl to convert the XML file ProjectTeam.xml into XML-FO files Resultxml2fo.fo.

(B) Further, let's take a look at how to convert the XML file into a PDF file.

The processing flow is as follows:

XLS-FO

PDF

FOP

Xml

Xslt

JAXP

// Example 3: ExampleXML2PDF.JAVA

// java

Import java.io.file;

Import java.io.ioException;

Import java.io.outputstream;

// jaxp

Import javax.xml.transform.transformer;

Import javax.xml.transform.transformerfactory;

Import javax.xml.transform.transformerexception;

Import javax.xml.transform.source;

Import javax.xml.transform.result;

Import javax.xml.transform.Stream.streamsource;

Import javax.xml.transform.sax.saxresult;

// Avalon

Import org.apache.avalon.framework.exceptionUtil;

Import org.apache.avaalon.framework.logger.consolelogger;

Import org.apache.avaalon.framework.logger.logger; // FOP

Import org.apache.fop.apps.driver;

Import org.apache.fop.apps.fopexception;

Import org.apache.fop.Messaging.MessageHandler;

/ **

* This class demonstrates the conversion of an xml file to pdf using

* JAXP (XSLT) and FOP (XSL: FO).

* /

Public class exampleXML2PDF {

Public Void ConvertXML2PDF (File XML, File XSLT, File PDF)

THROWS IOEXCEPTION, FOPEXCEPTION, TRANSFORMEREXCEPTION {

// Construct Driver

Driver driver = new driver ();

// setup logger

Logger logger = new consolelogger (consoleLogger.Level_INFO);

Driver.setLogger (Logger);

MessageHandler.SetScreenLogger (Logger);

// setup renderer (Output Format)

Driver.setrenderer (driver.render_pdf);

// setup OUTPUT

OutputStream out = new java.io.fileoutputstream (PDF);

Try {

Driver.setOutputStream (OUT);

// setup XSLT

TransformerFactory Factory = TransformerFactory.newinstance ();

Transformer Transformer = Factory.NewTransformer (New Streamsource (XSLT));

// setup input for xslt transport

Source src = new streamsource (XML);

// RESULTING SAX Events (The Generated Fo) Must Be Piped THROUGH To FOP

Result res = new saxResult (driver.getcontenthandler ());

// Start Xslt Transformation and Fop Processing

Transformer.Transform (SRC, RES);

} finally {

Out.close ();

}

}

Public static void main (String [] args) {

Try {

System.out.println ("FOP EXAMPLEXML2PDF / N");

System.out.println ("preplay ...");

// setup Directories

FILE Basedir = New File (".");

// setup Input and Output Files

File XMLFile = New File (Basedir, "ProjectTeam.xml);

File Xsltfile = New File (Basedir, "Projectteam2fo.xsl");

File pdffile = new file (basedir, "resultxml2pdf.pdf"); system.out.println ("Input: XML (" XMLFile ")")

System.out.println ("Stylesheet:" xsltfile);

System.out.println ("OUTPUT: PDF (" PDFFILE ")")

SYSTEM.OUT.PRINTLN ();

System.out.println ("Transforming ...");

ExampleXML2PDF App = New ExampleXML2PDF ();

App.convertXML2PDF (XMLFile, Xsltfile, PDFFile);

System.out.println ("Success!");

} catch (exception e) {

System.rr.println (ExceptionUtil.PrintStackTrace (e));

System.exit (-1);

}

}

}

Compile the above program to use the XSLT file ProjectTeam2fo.xsl to convert the XML file ProjectTeam.xml to PDF file Resultxml2pdf.pdf.

(3) javaàxmlàxsl-foàpdf:

This situation is interested in readers can refer to http://xml.apache.org/ about FOP.

Reference: http://xml.apache.org/

Author E-mail: jasea@sina.com

转载请注明原文地址:https://www.9cbs.com/read-116829.html

New Post(0)