package com.cisetech.put.utils.fop;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
/**
* FopReport
* @author bin.yin 2012/12/23
*/
public class FopReport {
private static Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_DEBUG);
private static final FopFactory fopFactory = FopFactory.newInstance();
/**
* 根据xsl模板及xml数据文件生成pdf
* @param xsltFile xsl模板
* @param xmlFile xml数据文件
* @return ReportData
* @throws Exception
* @author bin.yin 2012/12/25
*/
public static ReportData createReport(String xsltFile, String xmlFile) throws Exception {
ReportData reportData = new ReportData();
reportData.setContentType("application/pdf");
fopFactory.setUserConfig("conf/fop.xml");
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(new File(xsltFile)));
Source src = new StreamSource(new File(xmlFile));
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
reportData.setData(out.toByteArray());
} catch(Exception e) {
throw e;
} finally {
out.close();
}
return reportData;
}
/**
* 根据xsl模板及xml字节数组生成pdf
* @param xsltFile xsl模板
* @param bXmlData xml字节数组 eg. StringBuffer buf = new StringBuffer(); buf.getBytes("UTF-8");
* @return ReportData
* @throws Exception
* @author bin.yin 2012/12/25
*/
public static ReportData createReport(String xsltFile, byte[] bXmlData) throws Exception {
ReportData reportData = new ReportData();
try {
File xmlFile = File.createTempFile("FOP", ".tmp");
FileOutputStream fos = new FileOutputStream(xmlFile);
fos.write(bXmlData);
fos.close();
reportData = createReport(xsltFile, xmlFile.getAbsolutePath());
xmlFile.delete();
} catch (Exception e) {
throw e;
}
return reportData;
}
public static void main(String[] args) {
long t0 = System.currentTimeMillis();
try {
long t = System.currentTimeMillis();
ReportData data = FopReport.createReport("report\\sample\\Sample.xsl", "report\\sample\\Sample.xml");
long t1 = System.currentTimeMillis();
log.debug("time:" + (t1 - t));
FileOutputStream fos = new FileOutputStream("G:/sample.pdf");
fos.write(data.getData());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
log.debug("use time:" + (System.currentTimeMillis() - t0));
}
}
package com.cisetech.put.utils.fop;
import java.io.Serializable;
/**
* <p>Title: </p>
* <p>Description: </p>
* @author bin.yin 2012/12/22
* @version 1.0
*/
public class ReportData implements Serializable {
private static final long serialVersionUID = -2722248902864797698L;
private byte[] mbData = null;
private String msContentType = null