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

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

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.效果图如下: 
 

猜你喜欢

转载自samwong.iteye.com/blog/2047597
xsl
今日推荐