当前位置: 首页 > 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.zskr.cn/news/53272.html

相关文章:

  • 2025 最新打印机经销商推荐排行榜:长三角标杆企业 + 国内新锐品牌,全包服务与高效响应双重保障彩色打印机/打印机销售/打印机出租/打印机租赁公司推荐
  • 函数速查表
  • 2025年安徽合肥异味治理服务口碑推荐排行榜
  • 正规的甲醛检测平台推荐几家
  • sub-1G收发芯片DP4330A低成本解决方案OOK /(G)FSK 等多种调制方式远距离 - 动能世纪
  • 2025年羊毛地毯品牌哪家好?权威排行Top10推荐
  • 模型训练场景5090和4090的算力比较
  • 2025年羊毛地毯品牌口碑推荐榜单
  • 活动预告|IvorySQL 诚邀您参加2025开放原子开发者大会
  • 2025年评价高的羊毛地毯制造企业排行
  • 2025年隔离器厂家实力榜:细胞治疗隔离器、无菌粉体原料药隔离器、负压隔离器、多类型隔离器五家企业凭技术与口碑出圈
  • 2025年国内产品认证机构权威评测:昆明英格尔管理咨询有限公司蝉联榜首
  • [题解]P2340 [USACO03FALL] Cow Exhibition G
  • 基于模型预测控制的主蒸汽温度单步预测MATLAB实现
  • 2025年自动化绕线机订制厂家权威推荐:电机自动绕线机/小型自动绕线机/全自动电机绕线机源头厂家精选
  • Springboo下的MQTT多broker实现
  • 2025 年 11 月流速仪厂家推荐排行榜,LS300-A 流速仪,旋杯式/旋桨式流速仪,手持式电波雷达流速仪,专业测量与高效性能口碑之选
  • CF1830D Mex Tree
  • 如何在Totally Stub区域达成负载均衡
  • linux apache域名绑定域名
  • swagger 自动化文档
  • 2025年PPH真空机组定制厂家权威推荐:PPH环保型水喷射真空机组/PP水喷射真空机组/聚丙烯水喷射真空机组源头厂家精选
  • 基于DSP28027的流水灯实验
  • pycharm中如何切换多个python解释器使用:调整环节变量 - yj
  • 2025国内靠谱留学机构真实测评:5大机构核心优势全解析,精准适配不同申请需求
  • 完整教程:一文读懂 YOLOv4
  • 2025年气流烘干机优质厂家权威推荐榜单:沸腾烘干机/流化床烘干机/真空烘干机源头厂家精选
  • 10.17 T2
  • AI故事生成平台 - 呓语
  • 理解ndarray的几个重要属性