fop用于XSL Formatting Object,是apache的一个开放源代码项目。
http://xml.apache.org/fop/
它的发布包里就有很多例子。
如何在程序中嵌入FOP--FOP简介
http://www-900.ibm.com/developerWorks/cn/xml/x-fop/index.shtml
一本书:Processing XML with Java,里面有一些有关fop的内容。
http://www.ibiblio.org/xml/books/xmljava/
给你一个小例子(其实是改自fop的examples):
//Java
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.i
utputStream;
import java.io.FileOutputStream;
//SAX
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
//Avalon
import org.apache.avalon.framework.ExceptionUtil;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.logger.ConsoleLogger;
//FOP
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.FOPException;
import org.apache.fop.messaging.MessageHandler;
import org.apache.fop.apps.*;
/**
* This class demonstrates the conversion of an FO file to PDF using FOP.
*/
public class FopTest{
public void convertFO2PDF(String fo, String pdf) throws IOException, FOPException {
//Construct driver
Driver driver = new Driver();
driver.setInputSource(new InputSource(fo));
driver.setOutputStream(new FileOutputStream(pdf));
//Setup input
driver.setRenderer(Driver.RENDER_PDF);
//读入配置(在Options的构造函数中完成)
Options options = new Options(new File("userconfig.xml"));
//Process FO
Driver.run();
}
public static void main(String[] args) {
try {
System.out.println("FOP ExampleFO2PDF/n");
System.out.println("Preparing...");
//Setup directories
File outDir = new File("out");
outDir.mkdirs();
String inputFileName = args[0];
String outputFileName = "out"+args[1];
System.out.println("Input: XSL-FO (" + inputFileName + ")");
System.out.println("Output: PDF (" + outputFileName + ")");
System.out.println();
System.out.println("Transforming...");
FopTest app = new FopTest();
app.convertFO2PDF(inputFileName, outputFileName);
System.out.println("Success!");
} catch (Exception e) {
System.err.println(ExceptionUtil.printStackTrace(e));
System.exit(-1);
}
}
}