当前位置: 首页 > news >正文

java使用word模板填充内容,再生成pdf

1.word模板填充内容

使用EasyPoi写入Word文档。

import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class WriteWordtemplate {public static void main(String[] args) throws Exception {Map<String, Object> params = createDate();String sourceFile = "d:/temp/模版word3.docx";String targetFile = "d:/temp/输出结果3.docx";FileOutputStream fos = null;try {//获取模板文档File templateFile = new File(sourceFile);System.out.println(templateFile.getName());// 写入wordXWPFDocument doc = WordExportUtil.exportWord07(templateFile.getPath(), params);fos = FileUtils.openOutputStream(new File(targetFile));doc.write(fos);} catch (Exception e) {System.out.println(e);} finally {IOUtils.closeQuietly(fos);}}private static Map<String, Object> createDate() {//填充数据List<WordExportBatch> resultList = new ArrayList<>();WordExportBatch wordExport = new WordExportBatch();WordExportBatch wordExport1 = new WordExportBatch();wordExport.setCreateDate("2022/9/30");wordExport1.setCreateDate("2022/9/28");wordExport.setNumber("11");wordExport.setExMoneny("11");wordExport.setExFunc("支付宝");wordExport1.setNumber("15");wordExport.setAmount("1234.5");wordExport1.setAmount("2345.77");wordExport.setEndDate("2022/12/31");wordExport1.setEndDate("2022/11/30");wordExport.setType("支付宝");wordExport1.setType("微信");wordExport1.setExMoneny("22");wordExport1.setExFunc("微信");resultList.add(wordExport);resultList.add(wordExport1);//准备数据Map<String, Object> params = new HashMap<>();params.put("number", "112");params.put("amount", "1234.5");params.put("endDate", "2022/11/30");params.put("resultList", resultList);return params;}}

pom依赖可以参考https://blog.csdn.net/u011067966/article/details/134293480#comments_36351020

把word转换为pdf

word生成pdf的方法比较多,调研了常用的方式

  1. 一种方式是部署一台windowserver,对外提供接口来进行生成,能最大程度还原格式。
  2. 第二种方式就是利用jodconverter,会有少许的失真。

现在介绍的是基于jodconverter把word转换为PDF。首先引入jar包

        <dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-local</artifactId><version>4.2.2</version></dependency><dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-spring-boot-starter</artifactId><version>4.2.2</version></dependency>

在配置文件中加入如下配置

jodconverter:local:enabled: true             # 启用本地模式office-home: /opt/libreoffice7.5    # LibreOffice 的安装路径max-tasks-per-process: 10 # 每个 Office 进程处理的最大任务数port-numbers: 8202        # LibreOffice 后台服务使用的端口号,可修改

在处理类中注入,之所以使用@Lazy是因为加载的使用会报错,让它延迟加载。

 	@Lazy@Autowiredprivate DocumentConverter converter;public void wordConvertToPdf(File wordFile, File pdfFile) {try {logger.info("word转换pdf开始");converter.convert(wordFile).to(pdfFile).execute();logger.info("word转换pdf结束");} catch (Exception e) {logger.error("word转pdf异常", e);}}

后续使用

之前写过word模板生成段落和生成表格的段落。
结合起来就能生成相对完整的word了。后续如果需要盖章什么的直接拿生成的pdf文件就可以了。

http://www.lryc.cn/news/545692.html

相关文章:

  • 回归实战详细代码+解析:预测新冠感染人数
  • AI人工智能机器学习之聚类分析
  • (下:补充——五个模型的理论基础)深度学习——图像分类篇章
  • 使用Python自动生成图文并茂的网页分析报告
  • uniapp-原生android插件开发摘要
  • GIT工具学习【1】:基本操作
  • 《国密算法开发实战:从合规落地到性能优化》
  • 【语法】C++中string类中的两个问题及解答
  • LeetCode-154. 寻找旋转排序数组中的最小值 II
  • 2.数据结构:1.Tire 字符串统计
  • C语言复习4:有关数组的基础常见算法
  • Ubuntu从零创建Hadoop集群
  • GPIO概念
  • Node.js, Bun, Deno 比较概述
  • C# 类库打包dll文件
  • Linux中的UDP编程接口基本使用
  • RAG项目实战:金融问答系统
  • 大白话React第十一章React 相关的高级特性以及在实际项目中的应用优化
  • 虚拟机Linux操作(持续更新ing)
  • 【开源-线程池(Thread Pool)项目对比】
  • JMeter 实战项目脚本录制最佳实践(含 BadBoy 录制方式)
  • Jackson注解实战:@JsonInclude的妙用
  • CAN总线通信协议学习1——物理层
  • Vim 常用快捷键大全:跳转、编辑、查找替换全解析
  • 【Python 数据结构 2.时间复杂度和空间复杂度】
  • 【Qt QML】QML鼠标事件(MouseArea)
  • LeetCode 202. 快乐数 java题解
  • 《认知·策略·跃迁:新能源汽车工程师的深度学习系统构建指南》
  • PHP环境安装达梦数据库驱动实操
  • Electron + Vite + React + TypeScript 跨平台开发实践指南