XDocReport
XDocReport 是一个基于 Java 的 docx 文档创建工具,可以通过 Freemarker 或 Velocity 语法设计 docx 文档并将表达式的值进行替换生成新的 docx 文档,并转换为 PDF 或 XHTML。gtihub 地址
简单实现 docx 文档的生成
1.创建 docx 文件并作为模板使用
ctrl + f9 插入域
编辑域
保存并退出
2.引入依赖
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.4</version> </dependency> <!-- word,excel,ppt支持 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.11</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.11</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> </dependency> <!-- word模板支持 --> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.document</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.document.docx</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.template.velocity</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.itext.extension</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId> <version>2.0.1</version> </dependency>
3.代码实现
/** * 替换模板中的表达式,并转换为pdf格式输出 * @param srcPath 模板文件 docx文件 * @param destPath 输出文件 * @param param 参数 */ public void word2PDF(String srcPath, String destPath, Map<String, String> param) { File outFile = new File(destPath); InputStream is = null; OutputStream out = null; try { is = new FileInputStream(srcPath); logger.info(is.toString()); // 通过填充Velocity模板引擎和缓存来加载Docx文件 IXDocReport report = XDocReportRegistry.getRegistry().loadReport(is, TemplateEngineKind.Velocity); // 创建上下文Java模型 IContext context = report.createContext(); // 遍历参数 for(Map.Entry<String, String> entry : param.entrySet()) { logger.info("遍历参数: " + entry.getKey() + " ---- " + entry.getValue()); context.put(entry.getKey(), entry.getValue()); } if(outFile.exists()) { outFile.delete(); } // 通过将Java模型与Docx合并生成文档 out = new FileOutputStream(outFile); report.process(context, out); // 直接输出word文档 } catch (IOException e) { e.printStackTrace(); logger.error("读取模板异常!", e); } catch (XDocReportException e) { e.printStackTrace(); logger.error("word模板文字替换失败!", e); } finally { if(is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
转换为 pdf
将代码 report.process(context, out);
修改为
Options options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.XWPF); report.convert(context, options, out);
完整代码
/** * 替换模板中的表达式,并转换为pdf格式输出 * @param srcPath 模板文件 docx文件 * @param destPath 输出文件 pdf文件 * @param param 参数 */ public void word2PDF(String srcPath, String destPath, Map<String, String> param) { File outFile = new File(destPath); InputStream is = null; OutputStream out = null; try { is = new FileInputStream(srcPath); logger.info(is.toString()); // 通过填充Velocity模板引擎和缓存来加载Docx文件 IXDocReport report = XDocReportRegistry.getRegistry().loadReport(is, TemplateEngineKind.Velocity); // 创建上下文Java模型 IContext context = report.createContext(); // 遍历参数 for(Map.Entry<String, String> entry : param.entrySet()) { logger.info("遍历参数: " + entry.getKey() + " ---- " + entry.getValue()); context.put(entry.getKey(), entry.getValue()); } if(outFile.exists()) { outFile.delete(); } // 通过将Java模型与Docx合并生成文档 out = new FileOutputStream(outFile); // report.process(context, out); // 直接输出word文档 // 转换成PDF logger.info("开始转换成PDF"); Options options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.XWPF); report.convert(context, options, out); logger.info("PDF转换完成"); } catch (IOException e) { e.printStackTrace(); logger.error("读取模板异常!", e); } catch (XDocReportException e) { e.printStackTrace(); logger.error("word模板文字替换失败!", e); } finally { if(is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
注意
-
如果出现转换后的 pdf 是空白的,需要在电脑上安装 MS Office
存在的问题
-
生成完成后控制台一直有进程没有结束,原因未知
-
docx 转 pdf 格式无法完美保留,可以考虑通过 jacob 调用 word 组件实现 pdf 的转换
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于