文件格式转换神器 unoconv !!!
一、Unoconv简介
unoconv是一个基于LibreOffice / Openoffice开发的命令行工具,可以将不同格式的文件(如DOC、PPT、PDF等)在不同的操作系统上通过Libreoffice / Openoffice转换为PDF、DOC、PNG、PPTX等格式,并支持在命令行中指定输出文件名称、格式等功能。
官方文档:http://dag.wiee.rs/home-made/unoconv/
Github地址:https://github.com/unoconv/unoconv
二、版本选择
三、安装LibreOffice
- 卸载已有的:
yum remove libreoffice*
- 下载:https://www.libreoffice.org/download/download/
我选择的是:libreoffice7.6.6.3 - 解压:
tar -zxvf LibreOffice_7.6.6_Linux_x86-64_rpm.tar.gz
- 安装:
cd LibreOffice_7.6.6.3_Linux_x86-64_rpm/RPMS && yum localinstall *.rpm
- 验证:
ll /usr/bin | grep libre libreoffice7.6 --version
四、安装unoconv
- 下载:https://github.com/unoconv/unoconv/archive/refs/tags/0.8.2.tar.gz
- 解压
tar -zxvf /data0/unoconv-0.8.2.tar.gz
- 创建软连接(使用绝对路径)
ln -s /data0/unoconv-0.8.2/unoconv /usr/bin/unoconv
- 验证
ll /usr/bin | grep unoconv unoconv --version
五、Java示例
/**
* 利用unoconv进行文件格式转换
*
* @param sourceFilePath 源文件全路径
* @param destFilePath 目标文件全路径
* @param format 转换类型
*/
private static void convert(String sourceFilePath, String destFilePath, String format)
throws IOException, InterruptedException {
log.info("convert sourceFilePath: {}", sourceFilePath);
log.info("convert destFilePath: {}", destFilePath);
log.info("convert format: {}", format);
if (!FileUtil.exist(sourceFilePath)) {
throw new ServiceException("Source file not exist");
}
if (!FileUtil.exist(destFilePath) && !FileUtil.newFile(destFilePath).createNewFile()) {
throw new ServiceException("Dest file create error");
}
// -f [输出格式]
// -o [输出文件名] [输入文件名]
ProcessBuilder builder = new ProcessBuilder("unoconv",
"-f", format, "-o", destFilePath, sourceFilePath);
log.info("unoconv command: {}", String.join(" ", builder.command()));
Process process = builder.start();
boolean complated = process.waitFor(30, TimeUnit.SECONDS);
if (!complated) {
// 如果超时,强制终止子进程
process.destroyForcibly();
throw new ServiceException("unoconv命令超时,已强制终止子进程~");
}
}
六、问题记录
- pptx 转 pdf失败
报错信息:
报错原因:unoconv 0.6版本遗留问题... /usr/lib/libreoffice/program/libuno_cppu.so.3(+0x99c4)[0x7f201960c9c4] /usr/lib/libreoffice/program/libuno_sal.so.3(+0x44b68)[0x7f201ade6b68] /lib/x86_64-linux-gnu/libc.so.6(+0x94b43)[0x7f201ac02b43] /lib/x86_64-linux-gnu/libc.so.6(clone+0x44)[0x7f201ac93bb4] unoconv: RuntimeException during import phase: Office probably died. Binary URP bridge disposed during call ./binaryurp/source/bridge.cxx:613 Traceback (most recent call last): File "/usr/bin/unoconv", line 1285, in <module> die(exitcode) File "/usr/bin/unoconv", line 1138, in die if convertor.desktop.getCurrentFrame(): uno.com.sun.star.lang.DisposedException: Binary URP bridge already disposed
解决办法:升级版本
参考链接:https://forum.cloudron.io/topic/10047/pptx-to-pdf-fails?lang=zh-CN