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

Springboot -- 按照模板生成docx、pdf文件,docx转pdf格式

使用 poi-tl 根据模板生成 word 文件。
使用 xdocreport 将 docx 文件转换为 pdf 文件。

xdocreport 也支持根据模板导出 word ,但是 poi-tl 的功能更齐全,操作更简单,文档清晰。
poi-tl 、xdocreport 内部均依赖了 poi ,要注意两者中 poi 和 自身项目引用的 poi 的版本是否存在冲突。

文章目录

    • Pom 依赖
    • 生成 DOCX 文件
      • 创建 DOCX 模板
      • 生成 DOCX 文档
        • 创建模板填充类
        • 生成文件(多种方式)
        • 生成效果
      • DOCX 转 PDF
        • PDF 生成效果
        • 注意

Pom 依赖

使用 poi 5.2.2 ,poi-tl 1.12.1 ,xdocreport 2.0.3

<!--        poi依赖--><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.2</version></dependency>
<!--        poi-tl依赖--><dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.12.1</version></dependency>
<!--        xdocreport依赖--><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-full</artifactId><version>5.2.2</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>xdocreport</artifactId><version>2.0.3</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>org.apache.poi.xwpf.converter.pdf</artifactId><version>1.0.6</version></dependency>

生成 DOCX 文件

创建 DOCX 模板

根据官方文档按要求创建模板,并放在resources文件夹下。官方文档 http://deepoove.com/poi-tl/。
在这里插入图片描述
在这里插入图片描述

生成 DOCX 文档

创建模板填充类

其实也可以在生成文件时使用 Map 类型的方式填充文件内容,如下

XWPFTemplate.compile(inputStream).render(new HashMap<String, Object>(){{put("title", "Hi, poi-tl Word模板引擎");
}});

但是使用实体类更规范一些。

@Data
public class ChildRoundsProvalDocxEntity {private String childName;//孩子姓名private String identify;//身份证号private String gender;//性别private String childRounds;//孩次private PictureRenderData avatar;//头像地址private String entryDate;//入院时间private String gardenMonths;//孩次private String charge;//每月收费private String fatherName;//父亲姓名private String fatherIdentify;//父亲身份证号private String motherName;//母亲姓名private String motherIdentify;//母亲身份证号private String address;//住址及联系方式private String firstChildName;//第一个子女姓名private String firstChildIdentify;//第一个子女身份证号private String secondChildName;//第二个子女姓名private String secondChildIdentify;//第二个子女身份证号private String signDate;//签署日期
}

生成文件(多种方式)

1、生成一个文件输入流,方便用于再次编辑
不要在方法内部将输入流关闭

/*** 生成文件到输入流中* @param templatePath* @param data* @return*/
public InputStream createWordFile1(Object data){Resource templateFile = resourceLoader.getResource("classpath:wordtemplate/childRoundsProval.docx");XWPFTemplate template = null;InputStream resultStream = null;try {// word模板填充InputStream inputStream = templateFile.getInputStream();template = XWPFTemplate.compile(inputStream).render(data);resultStream = PoitlIOUtils.templateToInputStream(template);PoitlIOUtils.closeQuietlyMulti(template);} catch (Exception e) {log.error("导出失败,异常原因:" + e.getMessage());} finally {try {if (template != null) {template.close();}} catch (Exception e) {log.error("流关闭失败,异常原因:" + e.getMessage());}}return resultStream;
}

2、生成docx到输出流中,一般是在网络响应中直接输出,浏览器去下载

public void createWordFile2(Object data, HttpServletResponse httpServletResponse){//获取模板信息Resource templateFile = resourceLoader.getResource("classpath:wordtemplate/childRoundsProval.docx");XWPFTemplate template = null;docName = URLEncoder.encode(docName, StandardCharsets.UTF_8);try {httpServletResponse.setContentType("application/octet-stream");httpServletResponse.addHeader("Content-Disposition", "attachment;filename=" + docName + ".docx");httpServletResponse.addHeader("filename", docName);// word模板内容填充InputStream inputStream = templateFile.getInputStream();template = XWPFTemplate.compile(inputStream).render(data);OutputStream out = httpServletResponse.getOutputStream();//要记得关闭BufferedOutputStream bos = new BufferedOutputStream(out);//要记得关闭template.write(bos);bos.flush();out.flush();PoitlIOUtils.closeQuietlyMulti(template, bos, out);} catch (Exception e) {log.error("导出失败,异常原因:" + e.getMessage());throw new BaseException("Word文档生成失败");} finally {try {if (template != null) {template.close();}} catch (Exception e) {log.error("流关闭失败,异常原因:" + e.getMessage());}}
}

3、直接生成文件到指定路径

public void createWordFile3(Object data, String path){//获取模板信息Resource templateFile = resourceLoader.getResource("classpath:wordtemplate/childRoundsProval.docx");XWPFTemplate template = null;try {// word模板内容填充InputStream inputStream = templateFile.getInputStream();template = XWPFTemplate.compile(inputStream).render(data);template.writeToFile(path);//文件夹路径必须存在 可以提前创建PoitlIOUtils.closeQuietlyMulti(template);} catch (Exception e) {log.error("导出失败,异常原因:" + e.getMessage());throw new BaseException("Word文档生成失败");} finally {try {if (template != null) {template.close();}} catch (Exception e) {log.error("流关闭失败,异常原因:" + e.getMessage());}}
}

生成效果

在这里插入图片描述

DOCX 转 PDF

初始化 XWPFDocument 需要一个输入流,以下是直接使用文件输入流去初始化。

FileInputStream inputStream1 = new FileInputStream("docx文件位置.docx");
XWPFDocument xwpfDocument = new XWPFDocument(inputStream1);
PdfOptions options = PdfOptions.create();
try (OutputStream outPDF = Files.newOutputStream(Paths.get("要生成的pdf位置.pdf"))) {PdfConverter.getInstance().convert(xwpfDocument.getXWPFDocument(), outPDF, options);
} catch (IOException e) {log.error("PDF转换失败",e);
}

PDF 生成效果

在这里插入图片描述

注意

DOCX 模板中如果表格元素中放了图片(如上图中的头像),要确保生成的文件中的图片大小不超过模板中单元格大小。
即图片所在单元格不能被图片撑大,否则图片在转换成PDF时无法展示。

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

相关文章:

  • UE5.1.1 创建C++项目失败
  • windows进行端口映射
  • Python 异常处理
  • C++ 类的静态成员
  • 360T7路由器进行WiFi无线中继教程
  • 主成分分析
  • 笙默考试管理系统-MyExamTest(26)
  • 重新理解 RocketMQ Commit Log 存储协议
  • ES6基础知识十:你是怎么理解ES6中 Decorator 的?使用场景?
  • 接口/Web自动化测试如何做?框架如何搭建封装?
  • Linux怎么从网络上下载文件
  • Flutter携带JSON参数post请求
  • 【vue】vue-image-lazy图片懒加载使用与介绍【超详细+npm包源代码】
  • MFC第二十四天 使用GDI对象画笔和画刷来开发控件(分页控件选择态的算法分析、使用CToolTipCtrl开发动静态提示)
  • 【NLP-新工具】语音转文本与OpenAI的用途
  • try-catch-finally的字节码原理
  • 基于双层优化的微电网系统规划设计方法(Matlab代码实现)
  • 【Nginx13】Nginx学习:HTTP核心模块(十)Types、AIO及其它配置
  • 2023年华数杯数学建模C题思路分析
  • 安卓手机变身Linux服务器
  • 【Ansible】Ansible自动化运维工具之playbook剧本
  • Yolov8目标检测
  • Jmeter用于接口测试中,关联如何实现
  • 线程状态
  • HTML一些基础知识
  • git 命令总结
  • 【Django】如何优化数据库访问
  • 常压室温超导材料:揭开物理学的新篇章
  • 【《C# 10 和 .NET 6入门与跨平台开发(第6版)》——一本循序渐进的C#指南】
  • 2.5 BUMP图改进