介绍:FOP全称为Formatting Objects Processor,译为格式化对象处理器,简单点,它就是一种打印格式驱动,通过这种驱动,可以很方便的生成相应格式化的文件。FOP现在支持的格式化的文件包括PDF, PCL, PS, SVG, XML, Print, AWT, MIF and TXT.最主要也是最常使用的也是我们熟悉的PDF文件。下面就用简单的几个例子来说明FOP是如何做到的。
内容:1、 安装:从http://apache.etoak.com/xmlgraphics/fop/source/ 外下载一个文件包fop-1.1-src.tar.gz 文件包解压缩以后生成一个文件夹假设为F:/fop目录结构为 F:\fop 的目录2012-12-25 21:02 <DIR> .2012-12-25 21:02 <DIR> ..2012-10-16 23:47 1,653 build.properties2012-10-16 23:47 79,596 build.xml2012-10-16 23:47 18,545 checkstyle-5.5.xml2012-10-16 23:47 615 checkstyle-suppressions.xml2012-10-16 23:47 889 checkstyle.header2012-12-25 21:01 <DIR> conf2012-12-25 21:01 <DIR> examples2012-10-16 23:47 179,013 findbugs-exclude.xml2012-10-16 23:47 7,480 fop2012-10-16 23:47 3,216 fop.bat2012-10-16 23:47 1,252 fop.cmd2012-10-16 23:47 11,013 fop.js2012-10-16 23:47 4,184 forrest.properties2012-10-16 23:47 1,037 forrest.properties.xml2012-12-25 21:01 <DIR> hyph2012-10-16 23:47 1,429 jacoco.xml2012-10-16 23:47 15,684 KEYS2012-10-16 23:47 3,174 known-issues.xml2012-12-25 21:01 <DIR> lib2012-10-16 23:47 11,358 LICENSE2012-10-16 23:47 351 NOTICE2012-10-16 23:47 10,678 README2012-12-25 21:01 <DIR> src2012-10-16 23:47 128,549 status.xml2012-12-25 21:01 <DIR> test 19 个文件 479,716 字节 8 个目录 22,332,116,992 可用字节 其中lib目录包括几个需要的库:(a)XML的API:xml-apis.jar(b)Apache的XML的解析器:xercesImpl-2.2.1.jar(c) Apache的XSLT的处理器:xalan-2.4.1.jar(d) Apache的SVG的库文件,可以使PDF生成多彩图片:batik.jar(e) Avalon framework库文件:avalon-framework-cvs-20020806.jar将它们加入CLASSPATH;在系统的PATH变量里加入:F:/fop一个简单的测试:先生成一个fo文件:<?xml version="1.0" encoding="UTF-8"?><fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm"> <fo:region-body/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="simpleA4"> <fo:flow flow-name="xsl-region-body"> <fo:block>Hello World!</fo:block> </fo:flow> </fo:page-sequence></fo:root>打开DOS窗口:键入 fop helloworld.fo helloworld.pdf如果安装配置没有问题,就会看到在当前目录下生成一个helloworld.pdf文件。2、 下面我们来看一下在JAVA程序中如何使用FOP库资源来生成PDF文件,具体会遇到几种情况:(1)XLS-FOàPDF;(2)XMLàXSL-FOàPDF;(3)JAVAàXMLàXSL-FOàPDF(1)XLS-FOàPDF:XSL是W3C的标准XML文档,它包括两种:XSLT和XSLFO,而XSLFO即eXtensible Stylesheet Language Formatting Objects,也就是具有格式化对象的扩展样式语言文件。使用FOP的函数可以直接把一个XMLFO文件生成PDF文件。处理流程如下:XLS-FO PDF FOP例一:ExampleFO2PDF.java import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream; //SAXimport org.xml.sax.InputSource; //Avalonimport org.apache.avalon.framework.ExceptionUtil;import org.apache.avalon.framework.logger.Logger;import org.apache.avalon.framework.logger.ConsoleLogger; //FOPimport org.apache.fop.apps.Driver;import org.apache.fop.apps.FOPException;import org.apache.fop.messaging.MessageHandler; /** * This class demonstrates the conversion of an FO file to PDF using FOP. */public class ExampleFO2PDF { public void convertFO2PDF(File fo, File pdf) throws IOException, FOPException { //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 input InputStream in = new java.io.FileInputStream(fo); try { driver.setInputSource(new InputSource(in)); //Process FO driver.run(); } finally { in.close(); } } finally { out.close(); } } public static void main(String[] args) { try { System.out.println("FOP ExampleFO2PDF/n"); System.out.println("Preparing..."); //Setup directories File baseDir = new File("."); //Setup input and output files File fofile = new File(baseDir, "helloworld.fo"); File pdffile = new File(baseDir, "ResultFO2PDF.pdf"); System.out.println("Input: XSL-FO (" + fofile + ")"); System.out.println("Output: PDF (" + pdffile + ")"); System.out.println(); System.out.println("Transforming..."); ExampleFO2PDF app = new ExampleFO2PDF(); app.convertFO2PDF(fofile, pdffile); System.out.println("Success!"); } catch (Exception e) { System.err.println(ExceptionUtil.printStackTrace(e)); System.exit(-1); } }} 编译运行上面的程序把XMLFO文件helloworld.fo转化为ResultFO2PDF.pdf