`
samwong
  • 浏览: 281857 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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.效果图如下: 
 
分享到:
评论

相关推荐

    fop生成PDF

    本例采用FOP根据xsl模板将xml格式数据转化为PDF文档,支持中文

    XML+XSL/FO生成PDF文件Demo

    XML+XSL/FO生成PDF文件Demo,主要用于Fop插件,算是个小型练习项目

    Apache Fop

    Apache Fop工具,编写xs文件l生成PDF文件,编写xsl参考xsl语言,000.bat为启动脚本,数据文本在\fop\fop-1.1\data.xml里面,demo只有一个,没上传多个例子,慢慢琢磨

    FOP高级技术文档.rar

    xsl-fo 语言 作为xml的模板 用FOP工具生成PDF,如有疑问 sinbh@163.com

    stylekit:JavaScript替代品,用于使用XML和XSL创建动态文档

    使用XML和XSL创建动态文档。 由创建和维护。 该项目当前正在。 有关如何使用样式套件的更多信息,请参见LogicPull。 配置 Stylkit带有一个配置文件config.js 。 这可用于设置Apache FOP可执行文件的位置,以及输入...

    pentext:PenText系统

    为了生成PDF文档,首先将报告,报价,发票或常规文档XML转换为XSL-FO(XSL格式对象),然后使用Apache FOP将其转换为PDF。结构这些目录的用法如下: chatops:包含可以与Hubot(chatOps)一起使用的bash和Python...

    FOPLaboratory:Apache FOP的GUI前端-开源

    另一方面,FOPLaboratory使您可以快速交换XSL工作表,以从一个相同的XML内容生成不同的PDF文件。 已发布的项目文件包含该程序的C ++ / Qt源代码。 我接受捐款。 这笔钱将用于翻修农舍和谷仓,以及建设我们的家庭...

    XSL-FO Wysiwyg MiniScribus-开源

    XSL-FO格式标记所见即所得编辑器和PDF树书签。 XML文档,最常用作PDF或RTF生成器。 它可以从Apache fop示例中读取和编辑95%的内容。 导出到fo,pdf,rtf,tif传真,页面,导入fo,html,页面,odt OpenOffice 1-2

    Visual xsltproc Debugger-开源

    Visual xsltproc是一个工具,可帮助编写xslt文件并对其进行调试以查找错误。 它编写xml,并生成xml(XML和行号的语法高亮显示)。 最后,如果结果是XSL-FO,它将在Apache FOP java上生成pdf。 建立在QT4.2之上。

    DocBook sml-开源

    DocBook sml维护多语言文档,生成全自动工件(html,pdf,xml,txt),使用DocBook XSL,Saxon,Xalan,FOP,Lynx,由Ant,Yax驱动,支持计算机辅助翻译,可独立运行或在IDE中运行像Eclipse。

    jpivot学习总结.doc

    生成的 URL 中包含这个 member 的唯一名称,这个标签必须要在一个 table 或一个 query 的标签里嵌套使用。 这个动作还依赖于该标签的 sessionParam 属性,如果该属性存在,那么参数值将在页面显示之前写到 ...

Global site tag (gtag.js) - Google Analytics