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

存在的问题

生成完成后控制台一直有进程没有结束,原因未知

B3log 是一个开源组织,名字来源于“Bulletin Board Blog”缩写,目标是将独立博客与论坛结合,形成一种新的网络社区体验,详细请看 B3log 构思。目前 B3log 已经开源了多款产品:SymSoloVditor思源笔记

Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。