fop生成PDF支持中文(xml & xsl)

本例可将xml格式数据按xsl模板转化为PDF
1.首先,程序结构如下:
[img]http://dl.iteye.com/upload/attachment/0078/4825/7dcf4457-0376-3614-914e-f66ee944c76a.png[/img]
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("<ItemListReport>");
buf.append(" <ReportHeader>");
buf.append(" <Title>附加条款</Title>");
buf.append(" <PartyA>上海信息技术有限公司B</PartyA>");
buf.append(" <PartyB>上海信息技术有限公司B</PartyB>");
buf.append(" </ReportHeader>");
buf.append(" <ReportBody>");
buf.append(" <Table>");
buf.append(" <TableRow>");
buf.append(" <ItemName>附加条款1</ItemName>");
buf.append(" <ItemTime>2012-12-23 09:03</ItemTime>");
buf.append(" </TableRow>");
buf.append(" <TableRow>");
buf.append(" <ItemName>上海信息技术有限公司附加条款1</ItemName>");
buf.append(" <ItemTime>2012-12-23 09:03</ItemTime>");
buf.append(" </TableRow>");
buf.append(" </Table>");
buf.append(" </ReportBody>");
buf.append(" <ReportFooter>");
buf.append(" <PrintDate>2012-12-12</PrintDate>");
buf.append(" <ReportNo>010123456789</ReportNo>");
buf.append(" </ReportFooter>");
buf.append("</ItemListReport>");

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
<font metrics-url="conf/fonts/SimSun.xml" kerning="yes" embed-url="conf/fonts/SimSun.ttc">
<font-triplet name="SimSun" style="normal" weight="normal" />
<font-triplet name="SimSun" style="normal" weight="bold" />
<font-triplet name="SimSun" style="italic" weight="normal" />
<font-triplet name="SimSun" style="italic" weight="bold" />
</font>

6.效果图如下:
[img]http://dl.iteye.com/upload/attachment/0078/4829/738822cd-0075-30a3-8de0-d55336488bd5.png[/img]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值