本例可将xml格式数据按xsl模板转化为PDF
1.首先,程序结构如下:
2.FopReport.java
1.首先,程序结构如下:
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
- &nbs

该博客介绍了如何使用Java的FOP库结合XML和XSL模板来生成包含中文的PDF报告。通过配置FopFactory、设置输出流、构造XSLT转换器,并使用UTF-8编码的XML数据,成功创建了包含标题、公司名称、表格内容等中文信息的PDF文件。

最低0.47元/天 解锁文章
1836

被折叠的 条评论
为什么被折叠?



