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

java根据word模板生成word,在根据word文件转换成pdf文件

1.引入pom文件

  <!-- Apache POI for Word document generation --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.3</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.3</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>5.2.3</version></dependency><!-- Apache POI PDF Converter --><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId><version>2.0.6</version></dependency>

2.Word模板使用说明

在`src/main/resources/templates/`(自定义)目录下放置Word模板文件,例如`rectification_template.docx`。在模板中使用占位符格式:`${字段名}`,例如:
- `${safe}` 
- `${quality}` 当调用接口时,系统会根据模板和数据生成新的Word文档

image

 

 

3.代码

    @ApiOperation("预览整改通知PDF文档")@GetMapping("/previewPdfDocument/{id}")public void previewPdfDocument(@PathVariable Long id, HttpServletResponse response) {try {// 生成PDF文档文件File pdfFile = rectificationService.generatePdfDocumentFile(id);if (pdfFile == null) {response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "PDF文档生成失败");return;}// 设置响应头,使浏览器直接预览而不是下载response.setContentType("application/pdf");response.setContentLength((int) pdfFile.length());// 写入响应try (FileInputStream fileInputStream = new FileInputStream(pdfFile);ServletOutputStream outputStream = response.getOutputStream()) {byte[] buffer = new byte[1024];int bytesRead;while ((bytesRead = fileInputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, bytesRead);}outputStream.flush();}// 删除临时文件pdfFile.delete();} catch (Exception e) {try {response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "PDF文档生成或预览过程中发生错误: " + e.getMessage());} catch (IOException ioException) {ioException.printStackTrace();}}}

4.代码2

   @Overridepublic File generatePdfDocumentFile(Long id) {try {// 首先生成Word文档ByteArrayOutputStream wordOutputStream = generatePdf(id);if (wordOutputStream == null) {return null;}// 创建临时文件File pdfFile = File.createTempFile("rectification_" + id + "_", ".pdf");// 将Word转换为PDF并写入文件try (ByteArrayInputStream wordInputStream = new ByteArrayInputStream(wordOutputStream.toByteArray());FileOutputStream pdfOutputStream = new FileOutputStream(pdfFile)) {PdfUtil.convertWordToPdfFormatted(wordInputStream, pdfOutputStream);return pdfFile;}} catch (Exception e) {e.printStackTrace();return null;}}

5.模板数据填入

  @Overridepublic ByteArrayOutputStream generatePdf(Long id) {Intion entity = baseMapper.selectById(id);//改成自己需要的数据if (null == entity) {return null;}//模板路径ClassPathResource resource = new ClassPathResource("templates/rectification_template.docx");// 准备模板数据Map<String, Object> dataMap = new HashMap<>();dataMap.put("safe", entity.getSafe());try {// 获取模板路径String templatePath = resource.getFile().getAbsolutePath();// 生成Word文档ByteArrayOutputStream outputStream = new ByteArrayOutputStream();WordTemplateUtil.generateWordFromTemplate(templatePath, dataMap, outputStream);return outputStream;} catch (Exception e) {e.printStackTrace();return null;}}

6.word工具类

import org.apache.poi.xwpf.usermodel.*;
import java.io.*;
import java.util.Map;
import java.util.List;/*** Word模板工具类* 根据Word模板生成带数据的Word文档*/
public class WordTemplateUtil {/*** 根据模板生成Word文档* @param templatePath 模板文件路径* @param params 数据参数* @param outputStream 输出流* @throws IOException IO异常*/public static void generateWordFromTemplate(String templatePath, Map<String, Object> params, OutputStream outputStream) throws IOException {try (FileInputStream fis = new FileInputStream(templatePath);XWPFDocument document = new XWPFDocument(fis)) {// 替换段落中的占位符
            replaceInParagraphs(document, params);// 替换表格中的占位符
            replaceInTables(document, params);// 写入输出流
            document.write(outputStream);}}/*** 替换段落中的占位符* @param document Word文档* @param params 参数映射*/private static void replaceInParagraphs(XWPFDocument document, Map<String, Object> params) {for (XWPFParagraph paragraph : document.getParagraphs()) {for (XWPFRun run : paragraph.getRuns()) {String text = run.getText(0);if (text != null) {for (Map.Entry<String, Object> entry : params.entrySet()) {String placeholder = "${" + entry.getKey() + "}";Object value = entry.getValue();if (text.contains(placeholder)) {text = text.replace(placeholder, value != null ? value.toString() : "");run.setText(text, 0);}}}}}}/*** 替换表格中的占位符* @param document Word文档* @param params 参数映射*/private static void replaceInTables(XWPFDocument document, Map<String, Object> params) {for (XWPFTable table : document.getTables()) {for (XWPFTableRow row : table.getRows()) {for (XWPFTableCell cell : row.getTableCells()) {for (XWPFParagraph paragraph : cell.getParagraphs()) {for (XWPFRun run : paragraph.getRuns()) {String text = run.getText(0);if (text != null) {for (Map.Entry<String, Object> entry : params.entrySet()) {String placeholder = "${" + entry.getKey() + "}";Object value = entry.getValue();if (text.contains(placeholder)) {text = text.replace(placeholder, value != null ? value.toString() : "");run.setText(text, 0);}}}}}}}}}/*** 生成表格数据* @param document Word文档* @param bookmark 表格书签* @param headers 表头* @param dataList 数据列表*/public static void generateTableData(XWPFDocument document, String bookmark, String[] headers, List<Object[]> dataList) {// 查找书签位置// 这里简化处理,实际应用中可以通过查找书签位置来确定表格插入点// 创建表格XWPFTable table = document.createTable();// 设置表头XWPFTableRow headerRow = table.getRow(0);for (int i = 0; i < headers.length; i++) {if (i == 0) {headerRow.getCell(0).setText(headers[i]);} else {headerRow.addNewTableCell().setText(headers[i]);}}// 填充数据for (Object[] rowData : dataList) {XWPFTableRow row = table.createRow();for (int i = 0; i < rowData.length; i++) {row.getCell(i).setText(rowData[i] != null ? rowData[i].toString() : "");}}}
}

7.pdf 工具类

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;/*** PDF工具类* 提供Word转PDF等功能*/
public class PdfUtil {/*** 将Word文档转换为PDF (使用Apache POI实现)* 使用org.apache.poi.xwpf.usermodel.XWPFDocument和fr.opensagres.poi.xwpf.converter.pdf实现转换* @param wordInputStream Word文档输入流* @param pdfOutputStream PDF文档输出流* @throws Exception 转换异常*/public static void convertWordToPdfFormatted(InputStream wordInputStream, OutputStream pdfOutputStream) throws Exception {try {// 使用Apache POI加载Word文档XWPFDocument document = new XWPFDocument(wordInputStream);// 创建PDF转换选项PdfOptions options = PdfOptions.create();// 执行转换
            PdfConverter.getInstance().convert(document, pdfOutputStream, options);} catch (IOException e) {throw new Exception("转换Word为PDF失败", e);}}
}

 

http://www.jsqmd.com/news/43759/

相关文章:

  • (二)文件下载压缩打包:下载(wget)、压缩(gzip)、解压(gunzip)、打包(tar)
  • 前端打包的一些注意事项
  • 2025 最新打印机经销商推荐排行榜:长三角标杆企业 + 国内新锐品牌,全包服务与高效响应双重保障彩色打印机/打印机销售/打印机出租/打印机租赁公司推荐
  • 函数速查表
  • MATLAB实现的基于压缩感知的图像处理
  • AI变革,企业如何应用AI大模型重塑思考维度?
  • 还是得要耐心--从淘宝数据线中考虑到的
  • 比较好的空气检测服务
  • 2025年建材连锁ERP软件前十名分析:四大主流系统评测
  • 2025年安徽合肥异味治理服务口碑推荐排行榜
  • 正规的甲醛检测平台推荐几家
  • Kafka-配置SASL/SCRAM认证
  • 2025年潜水泵厂家实力榜:轴流水泵、潜水轴流泵、轴流潜水泵、卧式混流水泵、品类五家企业凭技术与口碑出圈
  • QT中groupbox填满整个页面
  • 视频编辑的新成果!港科大蚂蚁集团提出Ditto框架刷新SOTA!
  • 2025年气体减压阀厂家实力榜:大流量气体减压阀,不锈钢氮气减压阀,不锈钢泄压阀,实验室气体减压阀、多品类阀门企业凭技术与口碑出圈
  • 2025年市场朋友圈计划平台榜单top10:权威解析与推荐
  • sub-1G收发芯片DP4330A低成本解决方案OOK /(G)FSK 等多种调制方式远距离 - 动能世纪
  • 2025年惠州青少年素质教育机构权威推荐榜单:青少年专门教育/感恩教育/沉迷游戏矫正源头机构精选
  • jQuery的.each()方法中return的坑
  • 深入解析:文本描述驱动的可视化工具在IDE中的应用与实践
  • 模型训练场景5090和4090的算力比较
  • 2025年羊毛地毯品牌口碑推荐榜单
  • 01.入门篇-体验AI编程
  • cocos creator开发斗地主(包含server)
  • 2025年羊毛地毯品牌口碑推荐榜单:甄选优质手工艺术与健康生活
  • 活动预告|IvorySQL 诚邀您参加2025开放原子开发者大会
  • 2025 年 11 月护栏厂家推荐排行榜,桥梁防撞护栏,道路护栏,景观护栏,河道护栏,不锈钢复合管护栏公司推荐
  • 2025年评价高的羊毛地毯制造企业排行
  • GYM106191E-Leaf