xdocreport 学习笔记

本贴最后更新于 2021 天前,其中的信息可能已经渤澥桑田

XDocReport

XDocReport 是一个基于 Java 的 docx 文档创建工具,可以通过 Freemarker 或 Velocity 语法设计 docx 文档并将表达式的值进行替换生成新的 docx 文档,并转换为 PDF 或 XHTML。gtihub 地址

简单实现 docx 文档的生成

1.创建 docx 文件并作为模板使用

ctrl + f9 插入域

imagepng

编辑域

imagepng
imagepng
imagepng

保存并退出

imagepng

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

存在的问题

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

imagepng

  • docx 转 pdf 格式无法完美保留,可以考虑通过 jacob 调用 word 组件实现 pdf 的转换

  • B3log

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

    1083 引用 • 3461 回帖 • 262 关注
  • Java

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

    3169 引用 • 8208 回帖
  • 笔记

    好记性不如烂笔头。

    306 引用 • 782 回帖
  • 技术

    到底什么才是技术呢?

    88 引用 • 179 回帖 • 4 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...