OpenOffice


OpenOffice 和 LiberOffice 均可, 操作过程一致; LiberOffice兼容性更强.

1. 下载OpenOffice

OpenOffice官网地址
下载地址
选择对应的版本下载即可;

2. 启动OpenOffice服务

进入OpenOffice目录,运行soffice

# windows
soffice.exe -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
在Windows下可以直接修改快捷方式启动命令, 修改后直接双击启动即可;
  1. 在OpenOffice快捷方式上右键 -> 属性 -> 快捷方式 -> 目标:

    # 输入下面命令, OPENOFFICE_PATH 请替换为OpenOffice安装目录, 
    # 端口号8100可以自行更改
    # 如果需要外部访问, 请将host修改为本机IP
    "OPENOFFICE_PATH\program\soffice.exe" -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

3. 检测服务状态

  1. 打开 运行(CMD) 窗口;
  2. 键入

    # 8100 需要替换为上面启动服务时指定的端口
    # 如果执行完成后有返回结果, 即可证明服务已经启动
    netstat -ano | findstr 8100
    # 结果示例:
    # TCP    127.0.0.1:8100         0.0.0.0:0              LISTENING       39668
  3. 如果服务可以正常启动, 即可关闭服务进程, 此操作仅用于测试安装结果, 项目中会自动启动/销毁进程, 链接服务.

Java


  1. 创建SpringBoot项目
  2. 添加Maven依赖

    <!-- 版本号, 可以更改为当前最新版本 -->
    <jodconverter.version>4.4.2</jodconverter.version>
    
    
    <!-- 依赖项 -->
    <!--jodconverter 核心包 -->
    <dependency>
     <groupId>org.jodconverter</groupId>
     <artifactId>jodconverter-core</artifactId>
     <version>${jodconverter.version}</version>
    </dependency>
    
    <!--jodconverter 本地支持包 -->
    <dependency>
     <groupId>org.jodconverter</groupId>
     <artifactId>jodconverter-local</artifactId>
     <version>${jodconverter.version}</version>
    </dependency>
    
    <!--springboot支持包,里面包括了自动配置类 -->
    <dependency>
     <groupId>org.jodconverter</groupId>
     <artifactId>jodconverter-spring-boot-starter</artifactId>
     <version>${jodconverter.version}</version>
    </dependency>
    
  3. 编写配置文件, 请根据实际情况修改配置内容

    # application.yaml
    jodconverter:
      local:
     # 启用JODConverter。
     enabled: true
     # 表示office主目录。如果未设置,将自动检测office安装目录,需要安装最新版本的LibreOffice。
     office-home: "D:\\Tool\\OpenOffice\\OpenOffice 4"
     # 启动office进程时将在--accept参数中使用的主机名。 如果需要外部访问, 请设置为本机IP地址。
     host-name: "127.0.0.1"
     # 每个JODConverter处理线程使用的端口列表,以逗号分隔。office实例的数量等于端口的数量,因为每个端口号将启动一个office进程。
     port-numbers:
       - 8101
       - 8102
       - 8103
       - 8104
       - 8105
       - 8106
       - 8107
       - 8108
       - 8109
     # 工作(临时文件)目录。如果未设置,则默认为java指定的系统临时目录。
     # working-dir: "D:\\Temp\\office-temp\\working"
     # 启动已处理的office时,要复制到已创建的office配置文件目录的模板配置文件目录。
     # template-profile-dir: "D:\\Temp\\office-temp\\template"
     # 进程超时(毫秒)。尝试执行office process调用(启动/终止)时使用。
     process-timeout: 60000
     # 进程重试间隔(毫秒)。用于在office进程调用尝试之间等待(启动/终止)。
     process-retry-interval: 250
     # 指定尝试在执行任何其他操作之前启动office进程后的延迟。
     after-start-process-delay: 0
     # 允许处理任务的最长时间。如果任务的处理时间长于此超时,则将中止此任务并处理下一个任务。
     task-execution-timeout: 120000
     # office进程可以执行的最大任务数。
     max-tasks-per-process: 200
     # 转换队列中任务的最长等待时间。如果等待时间长于此超时,则将从队列中删除该任务。
     task-queue-timeout: 120000
  4. 启动项目, 测试(显示以下日志(未报错), 即代表启动成功)

      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::                (v2.7.0)
    
    2022-05-26 11:07:12.680  INFO 43092 --- [           main] c.e.openoffice.OpenofficeApplication     : Starting OpenofficeApplication using Java 1.8.0_311 on DESKTOP-GCG58PO with PID 43092 (D:\project-test\openoffice\target\classes started by Lenovo in D:\project-test\openoffice)
    2022-05-26 11:07:12.683  INFO 43092 --- [           main] c.e.openoffice.OpenofficeApplication     : No active profile set, falling back to 1 default profile: "default"
    2022-05-26 11:07:13.209  INFO 43092 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
    2022-05-26 11:07:13.214  INFO 43092 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2022-05-26 11:07:13.214  INFO 43092 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.63]
    2022-05-26 11:07:13.301  INFO 43092 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2022-05-26 11:07:13.301  INFO 43092 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 592 ms
    2022-05-26 11:07:13.741  INFO 43092 --- [er-offprocmng-7] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: OpenOffice - Version: ??? - useLongOptionNameGnuStyle: false
    2022-05-26 11:07:13.741  INFO 43092 --- [er-offprocmng-5] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: OpenOffice - Version: ??? - useLongOptionNameGnuStyle: false
    2022-05-26 11:07:13.741  INFO 43092 --- [er-offprocmng-3] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: OpenOffice - Version: ??? - useLongOptionNameGnuStyle: false
    2022-05-26 11:07:13.741  INFO 43092 --- [er-offprocmng-2] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: OpenOffice - Version: ??? - useLongOptionNameGnuStyle: false
    2022-05-26 11:07:13.741  INFO 43092 --- [er-offprocmng-4] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: OpenOffice - Version: ??? - useLongOptionNameGnuStyle: false
    2022-05-26 11:07:13.741  INFO 43092 --- [er-offprocmng-0] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: OpenOffice - Version: ??? - useLongOptionNameGnuStyle: false
    2022-05-26 11:07:13.741  INFO 43092 --- [er-offprocmng-8] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: OpenOffice - Version: ??? - useLongOptionNameGnuStyle: false
    2022-05-26 11:07:13.741  INFO 43092 --- [er-offprocmng-1] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: OpenOffice - Version: ??? - useLongOptionNameGnuStyle: false
    2022-05-26 11:07:13.741  INFO 43092 --- [er-offprocmng-6] o.j.local.office.OfficeDescriptor        : soffice info (from exec path): Product: OpenOffice - Version: ??? - useLongOptionNameGnuStyle: false
    2022-05-26 11:07:13.796  INFO 43092 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2022-05-26 11:07:13.806  INFO 43092 --- [           main] c.e.openoffice.OpenofficeApplication     : Started OpenofficeApplication in 1.413 seconds (JVM running for 1.858)
    2022-05-26 11:07:14.488  INFO 43092 --- [er-offprocmng-4] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=8105,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Lenovo\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-8105_tcpNoDelay-1'
    2022-05-26 11:07:14.502  INFO 43092 --- [er-offprocmng-6] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=8107,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Lenovo\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-8107_tcpNoDelay-1'
    2022-05-26 11:07:14.502  INFO 43092 --- [er-offprocmng-0] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=8101,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Lenovo\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-8101_tcpNoDelay-1'
    2022-05-26 11:07:14.515  INFO 43092 --- [er-offprocmng-2] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=8103,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Lenovo\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-8103_tcpNoDelay-1'
    2022-05-26 11:07:14.523  INFO 43092 --- [er-offprocmng-8] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=8109,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Lenovo\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-8109_tcpNoDelay-1'
    2022-05-26 11:07:14.537  INFO 43092 --- [er-offprocmng-5] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=8106,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Lenovo\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-8106_tcpNoDelay-1'
    2022-05-26 11:07:14.538  INFO 43092 --- [er-offprocmng-1] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=8102,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Lenovo\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-8102_tcpNoDelay-1'
    2022-05-26 11:07:14.547  INFO 43092 --- [er-offprocmng-7] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=8108,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Lenovo\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-8108_tcpNoDelay-1'
    2022-05-26 11:07:14.559  INFO 43092 --- [er-offprocmng-3] o.j.l.office.LocalOfficeProcessManager   : Starting process with --accept 'socket,host=127.0.0.1,port=8104,tcpNoDelay=1;urp;StarOffice.ServiceManager' and profileDir 'C:\Users\Lenovo\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-8104_tcpNoDelay-1'
    2022-05-26 11:07:16.657  INFO 43092 --- [er-offprocmng-1] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=8102,tcpNoDelay=1'
    2022-05-26 11:07:16.658  INFO 43092 --- [er-offprocmng-1] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 41508
    2022-05-26 11:07:16.748  INFO 43092 --- [er-offprocmng-7] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=8108,tcpNoDelay=1'
    2022-05-26 11:07:16.748  INFO 43092 --- [er-offprocmng-7] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 43804
    2022-05-26 11:07:16.831  INFO 43092 --- [er-offprocmng-6] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=8107,tcpNoDelay=1'
    2022-05-26 11:07:16.831  INFO 43092 --- [er-offprocmng-6] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 41192
    2022-05-26 11:07:16.831  INFO 43092 --- [er-offprocmng-3] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=8104,tcpNoDelay=1'
    2022-05-26 11:07:16.831  INFO 43092 --- [er-offprocmng-3] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 43528
    2022-05-26 11:07:16.833  INFO 43092 --- [er-offprocmng-4] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=8105,tcpNoDelay=1'
    2022-05-26 11:07:16.833  INFO 43092 --- [er-offprocmng-4] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 43500
    2022-05-26 11:07:17.027  INFO 43092 --- [er-offprocmng-0] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=8101,tcpNoDelay=1'
    2022-05-26 11:07:17.028  INFO 43092 --- [er-offprocmng-0] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 43376
    2022-05-26 11:07:17.029  INFO 43092 --- [er-offprocmng-2] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=8103,tcpNoDelay=1'
    2022-05-26 11:07:17.030  INFO 43092 --- [er-offprocmng-2] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 43296
    2022-05-26 11:07:17.037  INFO 43092 --- [er-offprocmng-5] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=8106,tcpNoDelay=1'
    2022-05-26 11:07:17.037  INFO 43092 --- [er-offprocmng-5] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 40968
    2022-05-26 11:07:17.054  INFO 43092 --- [er-offprocmng-8] o.j.local.office.OfficeConnection        : Connected: 'socket,host=127.0.0.1,port=8109,tcpNoDelay=1'
    2022-05-26 11:07:17.054  INFO 43092 --- [er-offprocmng-8] o.j.l.office.LocalOfficeProcessManager   : Started process; pid: 38764
  5. 可以使用cmd命令查看端口是否监听

    netstat | findstr 810*
    
      TCP    127.0.0.1:8101         activate:54084         ESTABLISHED
      TCP    127.0.0.1:8102         activate:54088         ESTABLISHED
      TCP    127.0.0.1:8103         activate:54085         ESTABLISHED
      TCP    127.0.0.1:8104         activate:54090         ESTABLISHED
      TCP    127.0.0.1:8105         activate:54092         ESTABLISHED
      TCP    127.0.0.1:8106         activate:54086         ESTABLISHED
      TCP    127.0.0.1:8107         activate:54091         ESTABLISHED
      TCP    127.0.0.1:8108         activate:54089         ESTABLISHED
      TCP    127.0.0.1:8109         activate:54087         ESTABLISHED
    

测试


  1. 测试转PDF
    1.1 编写测试类

package com.example.openoffice;

import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeException;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.File;

@SpringBootTest
class OpenofficeApplicationTests {

    @Resource
    private DocumentConverter converter;

    @Test
    void contextLoads() throws OfficeException {
        // 将word文档转换为pdf文档
        // 源文件
        File sourceFile = new File("D:\\Temp\\office-temp\\convert\\source\\测试.pptx");
        // 目标文件
        File targetFile = new File("D:\\Temp\\office-temp\\convert\\target\\测试.pdf");
        // 启动时间
        long start = System.currentTimeMillis();
        // 开始转换
        converter.convert(sourceFile).to(targetFile).execute();
        // 打印转换消耗时间
        System.out.println("转换耗时:" + (System.currentTimeMillis() - start));
    }

}


发表评论