文件格式转换神器 unoconv !!!

7 篇文章 0 订阅

一、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

  1. 卸载已有的:
    yum remove libreoffice*
    
  2. 下载:https://www.libreoffice.org/download/download/
    我选择的是:libreoffice7.6.6.3
  3. 解压:
    tar -zxvf LibreOffice_7.6.6_Linux_x86-64_rpm.tar.gz
    
  4. 安装:
    cd LibreOffice_7.6.6.3_Linux_x86-64_rpm/RPMS && yum localinstall *.rpm
    
  5. 验证:
    ll /usr/bin | grep libre
    
    libreoffice7.6 --version
    

四、安装unoconv

  1. 下载:https://github.com/unoconv/unoconv/archive/refs/tags/0.8.2.tar.gz
  2. 解压
    tar -zxvf /data0/unoconv-0.8.2.tar.gz
    
  3. 创建软连接(使用绝对路径)
    ln -s /data0/unoconv-0.8.2/unoconv /usr/bin/unoconv
    
  4. 验证
    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命令超时,已强制终止子进程~");
    }
}

六、问题记录

  1. pptx 转 pdf失败
    报错信息:
    ...
    /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
    
    报错原因:unoconv 0.6版本遗留问题
    解决办法:升级版本
    参考链接:https://forum.cloudron.io/topic/10047/pptx-to-pdf-fails?lang=zh-CN
    在这里插入图片描述
  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值