使用Apache XML项目FOP生成PDF文件(下)
四、 内容:
2、(2)XMLàXSL-FOàPDF:
有时我们在程序中会遇到要把一个只具有数据,没有格式的XML文件转化成PDF文件,这时我们就需要一个JAXP兼容的XSLT处理器:如Apache的xalan(或者IBM的xerces,Sun的JDOM等),请将它的库文件加入CLASSPATH。通过JAXP的API我们就可以先使用XSLT把XML文件转化为XML-FO文件,再把XML-FO文件转化为PDF。处理流程如下:
XLS-FO |
|
FOP |
XML |
XSLT |
JAXP |
(a) 第一步让我们先看一下如何把XML文件转化为XLS-FO文件,处理流程如下:
XLS-FO |
XML |
XSLT |
JAXP |
//例二: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 demonstrates the conversion of an XML file to an XSL-FO file
* using JAXP (XSLT).
*/
public class 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 transformation
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("Preparing...");
//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.err.println(ExceptionUtil.printStackTrace(e));
System.exit(-1);
}
}
}
编译运行上面的程序使用XSLT文件projectteam2FO.xsl把XML文件projectteam.xml转化为XML-FO文件ResultXML2FO.fo。
(b) 进一步,我们看一下如何把XML文件转化为PDF文件。
处理流程如下:
XLS-FO |
|
FOP |
XML |
XSLT |
JAXP |
//例三: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.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.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 transformation
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("Preparing...");
//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.err.println(ExceptionUtil.printStackTrace(e));
System.exit(-1);
}
}
}
编译运行上面的程序使用XSLT文件projectteam2FO.xsl把XML文件projectteam.xml转化为PDF文件ResultXML2PDF.pdf。
(3)JAVAàXMLàXSL-FOàPDF:
这种情况有兴趣的读者可以参考http://xml.apache.org/ 关于FOP的介绍。
参考资料:http://xml.apache.org/
作者邮箱:jasea@sina.com