Making PDF conversions is very practical. There is a lot of conversion tools that have existing, including some mature business software. As a Java program development, we will use a open source tool called FOP. Before starting the conversion, we have to know the FOP and make a simple understanding of it. FOP (Formatting Objects Processor) is the first XSL: FO-based print format processor and the first format processor that is unrelated to the output. It is a Java program that can read in the object tree and generate rendered pages to output to the specified stream. The currently supported output format has PDF, PCL, PS, SVG, XML (represented by tree structure), printer, AWT, MIF, and TXT. The main output refers to PDF. James Tauber - the original author of FOP. He developed the original version of the tool, and opened the code very generously, and then handed it to Apache XML Project. (He also gave the tool a great name; in addition to the name of the header, Webster's definition of Webster is "people who have existing appearance".) Now James is the chief XML designer of Bowstreet. The Apache XML project is developed in an open and cooperative way, providing commercial quality-based XML standard solutions. From a standard implementation perspective, it can give standard agencies (such as IETF and W
3C
) Provide feedback. FOP home http://xml.apache.org/fop/
FOP usage
FOP has three ways of use, which are command line, program embedded, XT embedded. Here will mainly describe how to embed the FOP function in the program. Converting XML files to PDF. It is actually divided into 2 steps. Step 1 is to convert XML to XSL-FO by XSLT, step 2 is to convert XSL-FO to PDF. Here is only described on how conversion programming in step 2.
After downloading the FOP package, you need to add the following JAR file in the ClassPath:
{FOP installation directory} /build/fop.jar
{FOP installation directory} /lib/batik.jar
{FOP installation directory} / lib / xalan-
2.0.0
.jar
{FOP installation directory} / lib / xerces-
1.2.3
.jar
{FOP installation directory} /lib/avalon-framework-4.0.jar
{FOP installation directory} /lib/logkit-1.0b4.jar
{FOP installation directory} /lib/jimi-1.0.jar
I use Eclipse, just join them in the engineering property -> Build path, you can use this great tool.
Below is specific procedures:
This is an XML file Test.xml:
Term here
term>
Definition here
definition>
Daa>
introduction>
featureName>
Content here.
content>
reguirement>
Content2 here.
content>
reguirement>
generaldescription>
Estring here
estring>
Resourceid Here
resourceid>
Rqmt here.
rqmt>
strRRESOURCE>
strRRRESOURCES>
Featuress>
This is its XSL file Test.xsl:
fo: Simple-Page-Master>
fo: layout-master-set>
fo: block>
fo: block>
fo: flow>
fo: Page-Sequence>
fo: root>
xsl: template>
5mm "margin-left =" 7mm "> fo: block> 5mm "margin-left =" 7mm "> fo: block> 5mm "margin-left =" 7mm "> fo: block> 5mm "margin-left =" 7mm "> fo: block> fo: block> fo: Table-Cell> fo: block> fo: Table-Cell> fo: Table-Row> fo: block> fo: Table-Cell> fo: block> fo: Table-Cell> fo: Table-Row> xsl: for-energy> fo: Table-Body> fo: Table> fo: block> xsl: template> 2.1.1 Feature Summary fo: block> 5mm "margin-left =" 9mm "> fo: block> 2.1.2 Feature Breakdown fo: block> 5mm "margin-left =" 9mm "> fo: block> 5mm "margin-left =" 7mm "> xsl: for-energy> fo: block> 5mm "margin-left =" 7mm "> fo: block> xsl: template> 10PT "font-weight =" bold "space-after =" 5mm "margin-left =" 5mm "> fo: block> fo: Table-Cell> fo: block> fo: Table-Cell> fo: block> fo: Table-Cell> fo: Table-Row> fo: block> fo: Table-Cell> fo: block> fo: Table-Cell> fo: block> fo: Table-Cell> fo: Table-Row> xsl: for-energy> fo: Table-Body> fo: Table> fo: block> xsl: template> xsl: stylesheet> Note that the above file should not be added: XML Version = "1.0" encoding = "UTF-8"?> This sentence. Converted Java code ExampleXML2PDF.JAVA: Package SRC; Import java.io.file; Import java.io.ioException; Import java.io.outputstream; Import javax.xml.transform.result; Import javax.xml.transform.source; Import javax.xml.transform.transformer; import javax.xml.transform.transformerexception; Import javax.xml.transform.transformerfactory; Import javax.xml.transform.sax.saxresult; Import javax.xml.transform.Stream.streamsource; Import org.apache.avalon.framework.exceptionUtil; Import org.apache.avaalon.framework.logger.consolelogger; Import org.apache.fop.apps.driver; Import org.apache.fop.Messaging.MessageHandler; Public class exampleXML2PDF { Public Void ConvertXML2PDF (File XML, File XSLT, File PDF) THROWS TRANSFORMEREXCEPTION, IOEXCEPTION { Driver driver = new driver (); ConsoLogger logger = new consolelogger (consoleLogger.Level_INFO); Driver.setLogger (Org.apache.Avalon.framework.logger.logger) Logger; MessageHandler.SetScreenLogger (Org.Apache.avalon.framework.logger.logger) 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 ("."); File outdir = new file (basedir, "out"); Outdir.mkdirs (); File XMLFile = New file ("f: //tomcat5/WebApps//myxml//xmldata//test.xml"); File xsltfile = new file ("f: //tomcat5/WebApps//myxml//xmldata/test.xsl"); File pdffile = new file ("f: //tomcat5/WebApps//myxml//xmldata//test.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); } } } Compiled execution.