2.FopReport.java
// Step 1: Construct a FopFactory
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");
// Step 2: Set up output stream.
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
// Step 3: Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
// Step 4: Setup XSLT using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(new File(xsltFile)));
// Step 5: Setup input and output for XSLT transformation
Source src = new StreamSource(new File(xmlFile));
// Source src = new StreamSource(new StringReader(myString));
// Step 6: Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Step 7: Start XSLT transformation and FOP processing
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 {
// convert xml bytes to a temp file
File xmlFile = File.createTempFile("FOP", ".tmp");
FileOutputStream fos = new FileOutputStream(xmlFile);
fos.write(bXmlData);
fos.close();
reportData = createReport(xsltFile, xmlFile.getAbsolutePath());
// delete temp file
xmlFile.delete();
} catch (Exception e) {
throw e;
}
return reportData;
}
3.拼接xml文件
StringBuffer buf = new StringBuffer();
buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
buf.append("");
buf.append("");
buf.append("
附加条款");buf.append("上海信息技术有限公司B");
buf.append("上海信息技术有限公司B");
buf.append("");
buf.append("");
buf.append("
buf.append("");
buf.append("附加条款1");
buf.append("2012-12-23 09:03");
buf.append("");
buf.append("");
buf.append("上海信息技术有限公司附加条款1");
buf.append("2012-12-23 09:03");
buf.append("");
buf.append("
");buf.append("");
buf.append("");
buf.append("2012-12-12");
buf.append("010123456789");
buf.append("");
buf.append("");
4.生成PDF文档
ReportData data = FopReport.createReport("report\\sample\\Sample.xsl", buf.toString().getBytes("UTF-8"));
FileOutputStream fos = new FileOutputStream("D:/sample.pdf");
5.附字体配置fop.xml
6.效果图如下:
大小: 32.4 KB
大小: 59.8 KB
下载次数: 246
下载次数: 386
分享到:
2012-12-27 16:01
浏览 9345
评论
7 楼
qrqhuang
2017-11-01
感谢博主分享, 条理清晰,很赞。
稍加修改, FOP2.1 一样可以跑起来。
6 楼
qrqhuang
2017-11-01
shihengli2010 写道
.xsl文件需要我们自己编写么 好难啊
一般来说这种标记语言, 学习起来很快的。
后续你能用到的也就那么几种排版
5 楼
shihengli2010
2016-09-05
.xsl文件需要我们自己编写么 好难啊
4 楼
Nancy0
2016-01-04
为什么解压报错呢?很是费解。。。
3 楼
qq_28504151
2015-07-18
好东西!!!
2 楼
gg_goodgame
2013-12-11
可以直接跑起来,但是下载解压时有些麻烦
1 楼
asigh
2013-10-24
东西很好,不然在fop官方看英文实在头疼。博主强大。赞赞赞