apache FOP研究
 
 
         fop是apache.org上的一个开源项目,主要是通过xsl-fo将xml数据转换成pdf、ps等文件格式,当然最主要的还是转换成pdf.基于元数据的xml+xsl-fo很容易将文档格式化得很漂亮.
          首先让我们下载相关文件以及文档:
           在这里:http://xmlgraphics.apache.org/fop/ ,点击左边的download下载你需要的资源包.呵呵.里面有文档、demo、依赖库、甚至ant的编译配置文件一应俱全,想研究源码的下源码包自己编译,在下比较懒,下载的是二进制包,下载完成后解压,将build目录下的fop.jar以及lib下的依赖库文件copy到你新建的工程、然后构建路径......不必多说了吧.重点是如何将xml转成pdf,打开example目录,大概看一边应该就了解了.具体如下:
           package embedding;
//Java
import java.io.File;
import java.io.OutputStream;
//JAXP
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
//FOP
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
/**
 * This class demonstrates the conversion of an XML file to PDF using
 * JAXP (XSLT) and FOP (XSL-FO).
 */
public class ExampleXML2PDF {
    /**
     * Main method.
     * @param args command-line arguments
     */
    public static void main(String[] args) {
        try {
            System.out.println("FOP ExampleXML2PDF\n");
            System.out.println("Preparing...");
            // Setup directories
            File baseDir = new File("E:/workdir/FopExample");
            File outDir = new File(baseDir, "out");
            outDir.mkdirs();
            // Setup input and output files           
            File xmlfile = new File(baseDir, "xml/xml/10061006.xml");
            File xsltfile = new File(baseDir, "xml/xslt/simple.xsl");
            File pdffile = new File(outDir, "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...");
           
            // configure fopFactory as desired
            FopFactory fopFactory = FopFactory.newInstance();
            fopFactory.setUserConfig("E:/workdir/FopExample/src/fopconfig.xml");
            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            // configure foUserAgent as desired           
            // Setup output
            OutputStream out = new java.io.FileOutputStream(pdffile);
            out = new java.io.BufferedOutputStream(out);
           
            try {
                // Construct fop with desired output format
                Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
   
                // Setup XSLT
                TransformerFactory factory = TransformerFactory.newInstance();
                Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
               
                // Set the value of a in the stylesheet
//                transformer.setParameter("versionParam", "2.0");
           
                // Setup input for XSLT transformation
                Source src = new StreamSource(xmlfile);
           
                // Resulting SAX events (the generated FO) must be piped through to FOP
                Result res = new SAXResult(fop.getDefaultHandler());
   
                // Start XSLT transformation and FOP processing
                transformer.transform(src, res);
            } finally {
                out.close();
            }
           
            System.out.println("Success!");
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }
}
 
 
 
当然你需要自己的xml和xsl模板、有关xml和xsl方面的知识笔者将会陆续介绍,敬请关注,附件中是我测试的demo,请指教。
 
郁闷,太大了,传不上,敬请原谅.不过这东西挺好的感觉.