ITeye博客

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

samwong · 2014-04-16 22:39 · 行业应用 · Markdown

http://oudoud.iteye.com/blog/1755054

本例可将xml格式数据按xsl模板转化为PDF 
1.首先,程序结构如下: 
 
2.FopReport.java 

Java代码  收藏代码

  1. // Step 1: Construct a FopFactory
  2. private static final FopFactory fopFactory = FopFactory.newInstance();
  3. /**
  4. * 根据xsl模板及xml数据文件生成pdf
  5. * @param xsltFile xsl模板
  6. * @param xmlFile xml数据文件
  7. * @return ReportData
  8. * @throws Exception
  9. * @author bin.yin 2012/12/25
  10. */
  11. public static ReportData createReport(String xsltFile, String xmlFile) throws Exception {
  12. ReportData reportData = new ReportData();
  13. reportData.setContentType("application/pdf");
  14. fopFactory.setUserConfig("conf/fop.xml");
  15. // Step 2: Set up output stream.
  16. ByteArrayOutputStream out = new ByteArrayOutputStream();
  17. try {
  18. // Step 3: Construct fop with desired output format
  19. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
  20. // Step 4: Setup XSLT using identity transformer
  21. TransformerFactory factory = TransformerFactory.newInstance();
  22. Transformer transformer = factory.newTransformer(new StreamSource(new File(xsltFile)));
  23. // Step 5: Setup input and output for XSLT transformation
  24. Source src = new StreamSource(new File(xmlFile));
  25. // Source src = new StreamSource(new StringReader(myString));
  26. // Step 6: Resulting SAX events (the generated FO) must be piped through to FOP
  27. Result res = new SAXResult(fop.getDefaultHandler());
  28. // Step 7: Start XSLT transformation and FOP processing
  29. transformer.transform(src, res);
  30. reportData.setData(out.toByteArray());
  31. } catch(Exception e) {
  32. throw e;
  33. } finally {
  34. out.close();
  35. }
  36. return reportData;
  37. }
  38. /**
  39. * 根据xsl模板及xml字节数组生成pdf
  40. * @param xsltFile xsl模板
  41. * @param bXmlData xml字节数组 eg. StringBuffer buf = new StringBuffer(); buf.getBytes("UTF-8");
  42. * @return ReportData
  43. * @throws Exception
  44. * @author bin.yin 2012/12/25
  45. */
  46. public static ReportData createReport(String xsltFile, byte[] bXmlData) throws Exception {
  47. ReportData reportData = new ReportData();
  48. try {
  49. // convert xml bytes to a temp file
  50. File xmlFile = File.createTempFile("FOP", ".tmp");
  51. FileOutputStream fos = new FileOutputStream(xmlFile);
  52. fos.write(bXmlData);
  53. fos.close();
  54. reportData = createReport(xsltFile, xmlFile.getAbsolutePath());
  55. // delete temp file
  56. xmlFile.delete();
  57. } catch (Exception e) {
  58. throw e;
  59. }
  60. return reportData;
  61. }

3.拼接xml文件 

Java代码  收藏代码

  1. StringBuffer buf = new StringBuffer();
  2. buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  3. buf.append("<ItemListReport>");
  4. buf.append("    <ReportHeader>");
  5. buf.append("        <Title>附加条款</Title>");
  6. buf.append("        <PartyA>上海信息技术有限公司B</PartyA>");
  7. buf.append("        <PartyB>上海信息技术有限公司B</PartyB>");
  8. buf.append("    </ReportHeader>");
  9. buf.append("    <ReportBody>");
  10. buf.append("        <Table>");
  11. buf.append("            <TableRow>");
  12. buf.append("                <ItemName>附加条款1</ItemName>");
  13. buf.append("                <ItemTime>2012-12-23 09:03</ItemTime>");
  14. buf.append("            </TableRow>");
  15. buf.append("            <TableRow>");
  16. buf.append("                <ItemName>上海信息技术有限公司附加条款1</ItemName>");
  17. buf.append("                <ItemTime>2012-12-23 09:03</ItemTime>");
  18. buf.append("            </TableRow>");
  19. buf.append("        </Table>");
  20. buf.append("    </ReportBody>");
  21. buf.append("    <ReportFooter>");
  22. buf.append("        <PrintDate>2012-12-12</PrintDate>");
  23. buf.append("        <ReportNo>010123456789</ReportNo>");
  24. buf.append("    </ReportFooter>");
  25. buf.append("</ItemListReport>");

4.生成PDF文档 

Java代码  收藏代码

  1. ReportData data = FopReport.createReport("report\\sample\\Sample.xsl", buf.toString().getBytes("UTF-8"));
  2. FileOutputStream fos = new FileOutputStream("D:/sample.pdf");

5.附字体配置fop.xml 

Java代码  收藏代码

  1. <font metrics-url="conf/fonts/SimSun.xml" kerning="yes" embed-url="conf/fonts/SimSun.ttc">
  2. <font-triplet name="SimSun" style="normal" weight="normal" />
  3. <font-triplet name="SimSun" style="normal" weight="bold" />
  4. <font-triplet name="SimSun" style="italic" weight="normal" />
  5. <font-triplet name="SimSun" style="italic" weight="bold" />
  6. </font>

6.效果图如下: 

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。