SpringBoot使用libreoffice转换PDF
1、简介
有时候我们需要在程序中使用到office的转换和预览功能,本文就针对这个需求记录了较为简单的office转换和功能:jodconverter。当然也有aspose和其他开源第三方(kkfileview),jodconverter是免费的,aspose需要付费。
2、SpringBoot集成jodconverter实战
2.1、首先下载并安装好OpenOffice或LibreOffice。
根据自己操作系统下载对应的,如本人是win系统,下载如下版本,下载完成之后点击直接安装。
在安装目录下的,输入命令:soffice --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard,启动LibreOffice:如下图所示:
2.2、在pom.xml引入相关依赖
<!--转换工具-->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.4.2</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.4.2</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.4.2</version>
</dependency>
2.3、在application.yml设置相关参数
jodconverter:
local:
enabled: true
# libreOffice根目录
officeHome: E:\soft\libreoffice
# 任务执行的超时时间
taskExecutionTimeout: 86400000
# 任务队列的超时时间
taskQueueTimeout: 86400000
# 端口(线程)
portNumbers: [2001,2002,2003]
# 一个进程的超时时间
processTimeout: 86400000
2.4、代码
@RequestMapping(value = "/preview")
@ResponseBody
public void preview(MultipartFile file, HttpServletResponse response) {
if (file == null) {
return;
}
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = file.getInputStream();
outputStream = response.getOutputStream();
String fileName = file.getOriginalFilename();
if (StrUtil.endWithAnyIgnoreCase(fileName, ".doc", ".docx", ".xls", ".xlsx", ".csv", ".ppt", ".pptx")) {
//转为PDF
documentConverter.convert(inputStream).to(outputStream)
.as(documentConverter.getFormatRegistry().getFormatByExtension("pdf")).execute();
} else if (StrUtil.endWithAnyIgnoreCase(fileName, ".pdf", ".txt", ".xml", ".md", ".json", ".html", ".htm",
".gif", ".jpg", ".jpeg", ".png", ".ico", ".bmp")) {
IoUtil.copy(inputStream, outputStream);
} else {
outputStream.write("暂不支持预览此类型附件".getBytes());
}
} catch (IORuntimeException e) {
log.error("附件预览IO运行异常:{}", e.getMessage());
} catch (IOException e) {
log.error("附件预览IO异常:{}", e.getMessage());
} catch (OfficeException e) {
log.error("附件预览Office异常:{}", e.getMessage());
} finally {
IOUtils.closeQuietly(inputStream);
}
IoUtil.writeUtf8(outputStream, true);
}
2.5、前端代码
<html>
<head>
<title>附件预览和下载</title>
</head>
<body>
<h5>支持附件类型为:.doc, .docx, .xls, .xlsx, .csv, .ppt, .pptx, .pdf, .txt, .xml, .md, .json, .gif, .jpg, .jpeg, .png, .ico, .bmp</h5>
<form id="fileForm" action="" method="post" enctype="multipart/form-data">
<i style="color: red">*</i> 上传附件:<input id="file" name="file" type="file" /><br/>
</form>
<button onclick="preview()">预览</button>
</body>
</html>
<script>
// 预览方法
function preview() {
// 检查是否为空和大小限制
var file = document.getElementById('file').files[0];
if (!file) {
alert("附件不能为空");
return;
}
var fileSize = file.size;
if (fileSize > 50 * 1024 * 1024) {
alert("最大支持附件大小为 50MB");
return;
}
// 提交表单
var fileForm = document.getElementById('fileForm');
fileForm.action = "[[@{/}]]file/preview";
fileForm.submit();
}
</script>
2.6、效果演示
2.4.1、在浏览器输入http://ip地址:端口号/file/index。
2.4.2、上传附件,点击“选择文件”上传需要转换文件。
2.4.3、点击“预览”,效果如下
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律