简介
基于 OpenOffice 服务将文件 (.doc、.docx、.xls、.ppt) 转化为 pdf、html 格式,然后直接在浏览器渲染输出。
内置功能
文件上传功能
文件转换功能
文件预览功能
远程文件预览功能
代码集成
基于 SpringBoot + OpenOffice 实现的项目案例,配置文件 application.properties 引入:
# 默认不开启 需要开启的设置成true
jodconverter.local.enabled=true
# 开启多个openOffice进程,每个端口对应一个进程
jodconverter.local.portNumbers=8100,8200
# openOffice进程重启前的最大进程数
jodconverter.local.maxTasksPerProcess=100
项目启动会自动生成两个转换进程,下面是伪代码实现:
@RestController
@RequestMapping("document")
public class ConverterController {
@Autowired
private DocumentConverter documentConverter;
/**
* 文件上传
*/
@RequestMapping("/upload")
public Result upload(MultipartFile file) {
try {
if (file != null) {
File parentFile = createParentFile();
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf("."));
String uuid = IdUtil.simpleUUID();
fileName = uuid + suffix;
File docFile = new File(parentFile, fileName);
FileUtil.writeFromStream(file.getInputStream(), docFile);
/**
* 年月日目录
*/
String fileDay = DateUtil.thisYear() + "/" + (DateUtil.thisMonth() + 1) + "/"
+ DateUtil.thisDayOfMonth();
String imagePath = SystemConstant.FILE + "/document/" + fileDay + "/" + fileName;
/**
* 实时转换
*/
logger.info("开始转换pdf......");
File toFile = new File(parentFile, uuid + ".pdf");
documentConverter.convert(docFile).to(toFile).execute();
logger.info("开始转换html......");
toFile = new File(parentFile, uuid + ".html");
converter( docFile,toFile);
return Result.ok(imagePath);
} else {
return Result.error();
}
} catch (Exception e) {
logger.error("转换异常{}",e);
return Result.error();
}
}
}
软件截图
小结
网上很多文章,Java 集成 OpenOffice 已经老得不能再老了,在 Linux 下还有各种启动问题,本案例只需要在系统中安装好相关服务,程序启动会自动生成 OpenOffice 进程。
源码
源码地址:https://gitee.com/52itstyle/SPTools
演示地址:https://tools.cloudbed.vip
账号密码:admin admin2020
(完)
MarkerHub文章索引:(点击阅读原文直达)
https://github.com/MarkerHub/JavaIndex
【推荐阅读】
查询亿级数据毫秒级返回!Elasticsearch 是如何做到的?
想避免重复请求/并发请求?这样处理才足够优雅
真香!推荐一个超级简单 Java 图形验证码模块
每一个都能笑抽的 39 个奇葩代码注释
好文章!点个在看!