POI 生成word 转 pdf

简介: 根据业务需要 需要出一份 PDF 文件 作为 公告的附件使用 PDF文件中 需要有 各种数据作为展示 是动态生成的

图示

work文档 大概是这种样子的  然后用${}标注的 都是需要填充的  包括下面的表格数据

image.png




提前说明


我看网上有很多方法  当然都很不错  不过有种 word 正常pdf是商业的 当然也可以破解,但是如果公司使用的话 总归是不保险的  所以就没有选用


其实生成word 的方法 相对简单   网上也有很多的方法  以及还有对应的框架


但是我都没有选用 而且是用 最普通的方法吧  就当学习了 吧  


hutool 主要准对的是 excel 对简单的 word 转成 pdf  其实没啥 可以不用 我引入这个是 解决生成excel的问题的  



重要

window环境没有问题  

linux 环境 要特别注意  没有中文字体  需要root账户的权限添加中文字体  记住是系统的字体 而不是JDK下的 front 文件    当然网上都有相关的教程  



开始正文


pom文件

先下载对应的POM文件 这里面 我走了很多坑 比如 hutool 的版本问题  等  

<!-- poi架包  说明 3.x 可以正常支持xdocreport 高版本会存在问题    --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.3</version></dependency><!--word转pdf方法--><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId><version>2.0.1</version></dependency><!-- 4.x 配合 poi 3.17 以上  不然excel 会出问题--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>4.6.12</version></dependency><!-- poi架包 -->



业务代码

因为可能存在公司代码 所以做了处理   核心内容不变  

importfr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
importfr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
importlombok.extern.slf4j.Slf4j;
importorg.apache.poi.xwpf.usermodel.*;
importorg.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
importorg.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
importorg.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
importorg.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Component;
importorg.springframework.util.StopWatch;   
publicStringgenerateWordToPdf(List<OrgExMngYlwlCollectDay>list,Stringtitle){
StopWatchstopWatch=newStopWatch();
StringylwlTitleNo=orgExMngYlwlCache.getYlwlTitleNo();
//参数Map<String, String>map=newHashMap<String, String>();
map.put("${day}", String.valueOf(LocalDate.now().getYear()));
map.put("${hao}",ylwlTitleNo);
map.put("${row}",LocalDate.now().format(DateTimeFormatter.ofPattern("MMdd")));
map.put("${datetime}",LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
ByteArrayOutputStreamoutPDF=null;
InputStreamstreamSafe=null;
// 读取word模板try {
//模板log.info("加载模板数据");
stopWatch.start("load word task");
//模板的位置 就是图示中展示的 work文档   ResourceUtil 这个是一个工具类 获取文件资源 你可以用hutool工具类的ResourceUtil streamSafe=ResourceUtil.getStream("template/ylwlTemplate.docx");
stopWatch.stop();
stopWatch.start("word task");
log.info("开始word模板填充");
XWPFDocumentdoc=newXWPFDocument(streamSafe);
//处理段落List<XWPFParagraph>paragraphList=doc.getParagraphs();
processParagraph(paragraphList,map);
//第一个表格   计算出那一个表格 进入表格的数据填充 主要字体XWPFTablexwpfTable=doc.getTables().get(0);
XWPFTableRowsourceRow=xwpfTable.getRow(1);
introwIndex=2;
for (OrgExMngYlwlCollectDaybean : list) {
//新增行  填充数据copyRow(xwpfTable,sourceRow,rowIndex);
XWPFTableRowrow=xwpfTable.getRow(rowIndex);
XWPFRunxwpfRun=row.getCell(0).addParagraph().createRun();
xwpfRun.setText(bean.getCheckData());
xwpfRun.setFontFamily("微软雅黑");
XWPFRunxwpfRun1=row.getCell(1).addParagraph().createRun();
xwpfRun1.setText(bean.getInterceptOrgCode());
xwpfRun1.setFontFamily("微软雅黑");
XWPFRunxwpfRun2=row.getCell(2).addParagraph().createRun();
xwpfRun2.setText(bean.getInterceptOrgName());
xwpfRun2.setFontFamily("微软雅黑");
XWPFRunxwpfRun3=row.getCell(3).addParagraph().createRun();
xwpfRun3.setText(String.valueOf(bean.getInterceptFailedSum()));
xwpfRun3.setFontFamily("微软雅黑");
rowIndex++;
            }
//对表格中的参数 进行匹配XWPFTableCellcell=xwpfTable.getRow(0).getCell(0);
processParagraph(cell.getParagraphs(),map);
//尾部的参数XWPFTablelastTable=doc.getTables().get(1);
XWPFTableCelllastCell=lastTable.getRow(0).getCell(1);
processParagraph(lastCell.getParagraphs(),map);
log.info("word模板填充完毕...");
stopWatch.stop();
//填充结束//这里是个大坑 如果直接进行 word转pdf的操作 则会发现表格中新增行的数据缺少  因此只能先把上面生成wrod文件 下载后 再进行pdf转化 //因数据填充问题 先下载  再转pdf  FileOutputStreamfileOutputStream=newFileOutputStream("ylwlGenereWord.docx");
doc.write(fileOutputStream);
doc.close();
FileInputStreaminputStream=newFileInputStream("ylwlGenereWord.docx");
XWPFDocumentxwpfDocument=newXWPFDocument(inputStream);
//生成PDFlog.info("准备生成pdf");
stopWatch.start("pdf task");
PdfOptionsoptions=PdfOptions.create();
outPDF=newByteArrayOutputStream();
PdfConverter.getInstance().convert(xwpfDocument, outPDF, options);
stopWatch.stop();
fileOutputStream.close();
inputStream.close();
xwpfDocument.close();
log.info("生成pdf成功....");
stopWatch.start("upload file task");
//      FileResult fileResult = xxxx.uploadFile(new ByteArrayInputStream(outPDF.toByteArray()), title);stopWatch.stop();
log.info("上传至文件服务器 文件名为{}, 响应信息为{}",title,fileResult);
log.info(stopWatch.prettyPrint());
if (fileResult.success()) {
returnfileResult.getFilePath();
            } else {
log.error("上传文件服务器失败 文件名为{},  响应信息为{}",title,fileResult);
thrownewFastDFSException();
            }
        } catch (IOExceptione) {
e.printStackTrace();
thrownewFileException();
        }finally {
IoUtil.close(streamSafe);
IoUtil.close(outPDF);
        }
    }
//这里就算把原来的 ${} 替换成 具体的内容publicstaticvoidprocessParagraph(List<XWPFParagraph>paragraphList,Map<String, String>param){
if(paragraphList!=null&&paragraphList.size() >0){
for(XWPFParagraphparagraph:paragraphList){
List<XWPFRun>runs=paragraph.getRuns();
for (XWPFRunrun : runs) {
Stringtext=run.getText(0);
if(text!=null){
booleanisSetText=false;
for (Map.Entry<String, String>entry : param.entrySet()) {
Stringkey=entry.getKey();
if(text.contains(key)){
isSetText=true;
Objectvalue=entry.getValue();
if (value!=null) {//文本替换text=text.replace(key, value.toString());
                                }
else{
text=text.replace(key, "");
                                }
                            }
                        }
if(isSetText){
run.setText(text,0);
                        }
                    }
                }
            }
        }
    }
目录
相关文章
|
4月前
|
小程序
公众号如何添加附传Word、Excel、Pdf、PPT文档
公众号里添加一些文档给公众号粉丝下载,比如课件PPT、申请表Word文档、岗位需求Excel表、大赛入围/获奖名单等。公众号本身是不支持直接上传文件的,但我们可以通过附件小程序“间接”上传文件。
738 0
|
6月前
|
存储 人工智能 文字识别
三款安卓手机word编辑器下载,Microsoft Word,wps office,Word手机版,手机word编辑查看阅读器,PDF转换器apk下载
WPS Office是一款功能强大的办公软件,支持文档编辑、表格处理和演示文稿制作,兼容多种格式并提供丰富的云服务。它具备低内存占用、快速运行的特点,支持跨设备同步与多人协作,内置海量模板及AI辅助功能,如智能写作和PPT自动生成。此外,还可扫描文件、编辑PDF并转换为其他格式,极大提升办公效率,适合手机用户便捷操作。
695 1
|
5月前
|
监控 Linux 数据安全/隐私保护
Python实现Word转PDF全攻略:从入门到实战
在数字化办公中,Python实现Word转PDF自动化,可大幅提升处理效率,解决格式兼容问题。本文详解五种主流方案,包括跨平台的docx2pdf、Windows原生的pywin32、服务器部署首选的LibreOffice命令行、企业级的Aspose.Words,以及轻量级的python-docx+pdfkit组合。每种方案均提供核心代码与适用场景,并涵盖中文字体处理、表格优化、批量进度监控等实用技巧,助力高效办公自动化。
1438 0
|
8月前
|
人工智能 算法 安全
使用CodeBuddy实现批量转换PPT、Excel、Word为PDF文件工具
通过 CodeBuddy 实现本地批量转换工具,让复杂的文档处理需求转化为 “需求描述→代码生成→一键运行” 的极简流程,真正实现 “技术为效率服务” 的目标。感兴趣的快来体验下把
465 10
DeepSeek + Xmind,1分钟自动把pdf/word文档转成思维导图
DeepSeek与Xmind结合,1分钟将PDF、Word文档转换为思维导图。只需四步:上传文档至DeepSeek,请求转换为Markdown格式,保存文件并修改后缀为.md,最后导入Xmind生成思维导图。轻松驾驭复杂文档,适用于学习笔记、工作报告、项目规划等场景,大幅提升效率!
|
人工智能 文字识别 数据挖掘
MarkItDown:微软开源的多格式转Markdown工具,支持将PDF、Word、图像和音频等文件转换为Markdown格式
MarkItDown 是微软开源的多功能文档转换工具,支持将 PDF、PPT、Word、Excel、图像、音频等多种格式的文件转换为 Markdown 格式,具备 OCR 文字识别、语音转文字和元数据提取等功能。
3095 9
MarkItDown:微软开源的多格式转Markdown工具,支持将PDF、Word、图像和音频等文件转换为Markdown格式
|
Java Apache Maven
将word文档转换成pdf文件方法
在Java中,将Word文档转换为PDF文件可采用多种方法:1) 使用Apache POI和iText库,适合处理基本转换需求;2) Aspose.Words for Java,提供更高级的功能和性能;3) 利用LibreOffice命令行工具,适用于需要开源解决方案的场景。每种方法都有其适用范围,可根据具体需求选择。
1786 2
|
Java Apache Maven
Java将word文档转换成pdf文件的方法?
【10月更文挑战第13天】Java将word文档转换成pdf文件的方法?
4775 1
|
Python
Python——将PPT和Word转为PDF文件
Python——将PPT和Word转为PDF文件
380 1
|
Python
还不会免费将PDF转为Word?你可以试试这3种工具!
还不会免费将PDF转为Word?你可以试试这3种工具!
634 0

热门文章

最新文章