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

[解决方案] Word转PDF

背景:

之前做过一些pdf导出, 客户提了一个特别急的需求, 要求根据一个模版跟一个csv的数据源, 批量生成PDF, 因为之前用过FOP, 知道调整样式需要特别长的时间, 这个需求又特别急, 所以寻找了一个其他的方案。

优点:

生成快捷,代码简单, 样式依赖模版,所见即所得

缺点:

模版难以调整

思路:

既然已经放弃FOP,那么就直接从模版生成新的word文档, 并且将word文档直接导出

第一版思路:

<dependency><groupId>org.docx4j</groupId><artifactId>docx4j-core</artifactId><version>8.3.9</version></dependency> <!-- :contentReference[oaicite:0]{index=0} --><!-- 内置 MOXy JAXB 实现 --><dependency><groupId>org.docx4j</groupId><artifactId>docx4j-JAXB-MOXy</artifactId><version>8.3.9</version></dependency> <!-- :contentReference[oaicite:1]{index=1} --><!--    &lt;!&ndash; FO 导出,用于生成 XSL-FO &ndash;&gt;--><dependency><groupId>org.docx4j</groupId><artifactId>docx4j-export-fo</artifactId><version>8.3.9</version></dependency> <!-- :contentReference[oaicite:2]{index=2} -->public static void main(String[] args) throws Exception {// 1. 加载模板InputStream tpl = Word2PDF.class.getResourceAsStream("/template.docx");if (tpl == null) {throw new RuntimeException("未找到模板 template.docx");}//这部分非必须, 是为了多次导出,不重复读模版byte[] template = tpl.readAllBytes();// 2. 准备多条替换数据List<Map<String,String>> dataList = new ArrayList<>();Map<String,String> maps = new HashMap<>();maps.put("firstName","Alice");maps.put("context","测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测");maps.put("lastName","Wang");maps.put("date","2025-05-20");dataList.add(maps);maps = new HashMap<>();maps.put("firstName","Bob");maps.put("lastName","Li");maps.put("date","2025-05-21");dataList.add(maps);maps = new HashMap<>();maps.put("firstName","Carol");maps.put("lastName","Zhang");maps.put("date","2025-05-22");dataList.add(maps);// 3. 循环生成for (Map<String,String> row : dataList) {// 3.1 重新加载模板WordprocessingMLPackage pkg;try (InputStream tplStream = new ByteArrayInputStream(template)) {pkg = WordprocessingMLPackage.load(tplStream);}// 3.2 执行替换 (${key})MainDocumentPart mdp = pkg.getMainDocumentPart();mdp.variableReplace(row);// 替换 ${firstName}、${lastName}、${date} :contentReference[oaicite:2]{index=2}// 3.3 保存为 DOCXString name = row.get("firstName");String docxPath = "/Users/Documents/" + name + ".docx";pkg.save(new File(docxPath));try(OutputStream os = new FileOutputStream("/Users/Documents/" + name + ".pdf"))  {Docx4J.toPDF(pkg, os);}}}

这种方式全部依赖docx4j的jar包,进行导出。 

缺点, 当模版有复杂模型,比如侧边栏时这种方式是无法导出的, 在网上找到的解决方案也是无效的。可能是因为JDK版本的升级。

版本2:

上面代码的逻辑一样,额外使用了documents4j的jar

<dependency><groupId>org.docx4j</groupId><artifactId>docx4j-core</artifactId><version>8.3.9</version></dependency> <!-- :contentReference[oaicite:0]{index=0} --><!-- 内置 MOXy JAXB 实现 --><dependency><groupId>org.docx4j</groupId><artifactId>docx4j-JAXB-MOXy</artifactId><version>8.3.9</version></dependency> <!-- :contentReference[oaicite:1]{index=1} --><!--    &lt;!&ndash; FO 导出,用于生成 XSL-FO &ndash;&gt;--><dependency><groupId>org.docx4j</groupId><artifactId>docx4j-export-fo</artifactId><version>8.3.9</version></dependency> <!-- :contentReference[oaicite:2]{index=2} -->//转化为PDF的代码使用//声明转换器,可重用
IConverter converter = LocalConverter.builder().baseFolder(new File(targetPath)).workerPool(5,15,30, TimeUnit.SECONDS).processTimeout (60, TimeUnit.SECONDS).build();
//声明 转换, 最后一步有schedule excute 两种写法, excute是直接生成,结果是boolean,是单条生成的,这种是为了批量运行
Future<Boolean> future = converter.convert(word).as(DocumentType.MS_WORD).to(new File(wordName + ".pdf")).as(DocumentType.PDF).schedule();//对应 schedule的运行
future.get();

这种方式可以达成所见即所得。

PS:

之前提出了模版难以修改,是因为模版中要使用${替换名称}的方式, 但是word有时会自动截断一个字符串, 导致实际上变成了${替 换名称  }的样式, 需要多改几次试下,连续输入试一下。

有一种比较简单的方式,就是将word文件的后缀名改成zip ,然后拿出document.xml 可以在这个里面直接改,名称改回后记得打开看是否报错, 如果报错,另存一下,就可以去掉报错。

http://www.xdnf.cn/news/7654.html

相关文章:

  • Android SharedPreferences:从零到一的全面解析与实战指南
  • win10使用nginx做简单负载均衡测试
  • 省赛备考~全国青少年信息素养大赛-图形化编程复赛/省赛-模拟题-判断质数合数
  • JavaScript 数组方法详解:全面指南
  • 如何优化前端应用中的JavaScript执行效率?
  • 【LinkedList demo 内部类讲说】
  • BI是什么意思?一文讲清BI的概念与应用!
  • LeetCode-前缀和-和为K的子数组
  • 网络学习中通信方面的相关知识、及再次解读B=2W
  • 如果电路教材这么讲--积分运算电路中反馈电容并联电阻的作用
  • 制造业或跨境电商相关行业三种模式:OEM、ODM、OBM
  • 十大排序算法--快速排序
  • VitePress 中以中文字符结尾的字体加粗 Markdown 格式无法解析
  • 颠覆传统:PROFINET转EthernetIP在油墨生产线的成功应用
  • 小土堆pytorch--神经网路-卷积层池化层
  • 时尚外观+专业性能丨特伦斯V30Pro重新定义便携电子钢琴
  • 深入剖析Zynq AMP模式下CPU1中断响应机制:从原理到创新实践
  • 【八股战神篇】Java虚拟机(JVM)高频面试题
  • Spring Validation校验
  • 吃透 Golang 基础:数据结构之数组
  • 高级SQL技巧:窗口函数与复杂查询优化实战
  • RestFul操作ElasticSearch:索引与文档全攻略
  • 【基于SpringBoot的图书购买系统】深度讲解 分页查询用户信息,分析前后端交互的原理
  • [Java实战] Docker 快速启动 Sentinel 控制台(二十八)
  • 【node.js】核心进阶
  • IP风险画像技术:如何用20+维度数据构建网络安全护城河?
  • 73.矩阵置零
  • 【b站计算机拓荒者】【2025】微信小程序开发教程 - 3 项目目录结构
  • 《Flask vs Django:项目规模、灵活性与开发时间的深入比较》
  • IDEA2025版本使用Big Data Tools连接Linux上Hadoop的HDFS