1. LibreOffice

LibreOffice 是一款免费的开源办公软件,类似于 Microsoft Office 和 Google Docs。它包括多个应用程序,例如:

Writer:用于处理文档和文字处理。
Calc:用于处理电子表格和数据分析。
Impress:用于创建演示文稿和幻灯片演示。
Draw:用于创建图形和矢量图形。
Base:用于处理数据库。
Math:用于创建和编辑数学公式。
LibreOffice 支持多种文件格式,包括 Microsoft Office 和 OpenDocument 格式,同时还支持 PDF 导出和打印等功能。它可用于 Windows、MacOS 和 Linux 等操作系统,并且可以在多种语言中使用。

由于 LibreOffice 是免费和开源的,因此它成为了许多人和组织的首选办公软件之一。它不仅提供了基本的办公功能,而且在数据安全和隐私方面也有优势。

2. Linux安装LibreOffice

2.1. 查看Linux系统的类别

  1. CentOS
cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
  1. Ubuntu
cat /etc/issue
Ubuntu 20.04.5 LTS \n \l

2.2. 安装

  1. CentOS下安装
  • 打开终端并以 root 用户身份登录。
  • 运行以下命令更新软件包:
yum update
  • 安装 LibreOffice 的依赖项:
yum install -y libreoffice*
  1. Ubuntu下安装
  • 打开终端并以 root 用户身份登录。
  • 运行以下命令更新软件包:
sudo apt-get update
  • 安装 LibreOffice 的依赖项:
sudo apt-get install libreoffice

2.3. 查看libreoffice的版本信息

libreoffice --version

这将输出 LibreOffice 的版本信息,例如:

LibreOffice 6.4.7.2 40(Build:2)

3. 使用libreoffice命令行word转pdf

3.1. 命令行

soffice --headless --convert-to pdf --outdir %s %s

3.2. Java调用命令行

    String strParsInfo = String.format("soffice --headless --convert-to pdf --outdir %s %s", strPdfFolderPath, strDocxFilePath);
    runProcess(strParsInfo);

public static String runProcess(String strParsInfo) {
        logger.info(strParsInfo);
        String strLog = strParsInfo+"\r\n";
        try {
            Process process = null;
            process = Runtime.getRuntime().exec(strParsInfo);
            // 获取进程的输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            // 逐行读取输出流并打印
            while ((line = reader.readLine()) != null) {
                strLog += line + "\n";
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage());
        }
        return strLog;
    }

4. 注意事项

4.1. 当程序在Docker中时

需要在Docker中安装LibreOffice

4.2. Word转pdf过程中乱码

  1. 安装 ttf-mscorefonts-installer
sudo apt-get install ttf-mscorefonts-installer
  1. 安装fonts-wqy-zenhei
sudo apt-get install fonts-wqy-zenhei

以上两种方式只用安装其中一种,有些服务器1好用,有些服务器2好用

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐