centos

centos liboffice文件转换技巧

小樊
46
2025-08-21 00:29:53
栏目: 智能运维

一、基础安装

  1. 安装LibreOffice

    sudo yum update && sudo yum install libreoffice
    

    (若需特定版本,可手动下载RPM包安装)

  2. 安装中文支持(避免乱码)

    • 下载中文字体包(如思源黑体),解压后复制到/usr/share/fonts/
    • 执行fc-cache -fv刷新字体缓存。

二、命令行转换技巧

  1. 单文件转换

    soffice --headless --convert-to pdf:writer_pdf_Export input.docx --outdir /output/path
    
    • --headless:后台运行,无界面弹出。
    • :writer_pdf_Export:指定导出过滤器(不同格式需调整,如calc_pdf_Export用于Excel转PDF)。
  2. 批量转换

    find /input/dir -name "*.docx" | while read file; do
        soffice --headless --convert-to pdf "$file" --outdir /output/dir
    done
    

    或使用unoconv(需先安装):

    unoconv -f pdf /input/dir/*.docx
    
  3. 自定义输出路径与权限

    • 通过--outdir指定输出目录,确保目录存在且当前用户有写入权限。
    • 转换后文件权限可通过chmod调整。

三、常见问题解决

  1. 服务未启动
    若使用unoconv或远程转换,需启动LibreOffice服务:

    sudo systemctl start libreoffice.service
    sudo systemctl enable libreoffice.service
    
  2. 中文乱码

    • 确认已安装中文字体并刷新缓存(fc-cache -fv)。
    • 检查输出文件编码是否为UTF-8(可通过file命令查看)。
  3. 大文件转换失败

    • 分批次转换或增加系统内存。
    • 使用--norestore参数避免加载上次会话(节省资源)。

四、高级用法

参考来源


0
看了该问题的人还看了