SpringBoot2.x整合OpenOffice4

1.导入依赖

<!-- openOffice 和 jobconverter-->
<dependency>
   <groupId>org.jodconverter</groupId>
   <artifactId>jodconverter-core</artifactId>
   <version>4.2.2</version>
</dependency>
<dependency>
   <groupId>org.jodconverter</groupId>
   <artifactId>jodconverter-spring-boot-starter</artifactId>
   <version>4.2.2</version>
</dependency>
<dependency>
   <groupId>org.jodconverter</groupId>
   <artifactId>jodconverter-local</artifactId>
   <version>4.2.2</version>
</dependency>
<!-- openOffice end-->

2.安装OpenOffice


OpenOffice下载地址: http://www.openoffice.org/download/

(Linux)安装

1.下载安装包到Linux opt文件夹

2.解压:tar -zxvf Apache_OpenOffice_4.1.7_Linux_x86-64_install-rpm_zh-CN.tar.gz

3.进入cd zh-CN/RPMS

*4.安装:rpm -ivh *.rpm

5.进入openOffice安装目录,命令:cd /opt/openoffice4/program/

6.开启服务(永久启动):soffice --headless --accept=“socket,host=127.0.0.1,port=8100;urp;” --nofirststartwizard &

7.查看进程:netstat -lnp |grep 8100

8.查询指定端口是否已开(开放8100端口): firewall-cmd --query-port=8100/tcp

​ 提示 yes,表示开启;no表示未开启

9.添加指定需要开放的端口:firewall-cmd --add-port=8100/tcp --permanent

10.重载入添加的端口:firewall-cmd --reload

11.查询指定端口是否开启成功:firewall-cmd --query-port=8100/tcp

(Windows)安装

1.一直按下一步就ok(安装路径默认即可)

3.application.yml相关配置

#openOffice配置
jodconverter:
  local:
    enabled: true
    max-tasks-per-process: 10
    port-numbers: 9100
    #home:安装地址
#    office-home: /opt/openoffice4   
#    working-dir: /opt/openoffice4/workingdir

前面3个配置项显而易见也没什么大问题,后两个容易出现一些问题:

 

  问题1:抛出异常IllegalStateException:officeHome not set and could not be auto-detected

  第一种情况:office-home其实是可以不填的,系统会默认一些路径。如果没有安装在系统指定路径并且没有配置office-home会抛出这个异常。

  默认路径存储在jar包中org.jodconverter.office.LocalOfficeUtils文件下,可以自行查阅。如果没有安装在这下面像我一样指定。

  第二种情况:项目本地没有问题,部署在docker中突然出现这个错误。熟悉docker的朋友应该知道程序获取路径获取的其实是docker容器的路径,

  所以需要在运行docker容器的时候添加挂载目录。例:docker run ... -v opt/openoffice4:/opt/openoffice4 ... 

 

  问题2:抛出异常IllegalArgumentException: URL seems to be an unsupported

  第一种情况:查看当前文件格式是否被支持。支持类型存储在org.jodconverter.document.DefaultDocumentFormatRegistry类中,自己去查看是

  否支持自己的类型转换。

  第二种情况:其实还有一种情况也会抛出这个异常,就是程序在转换文件类型时会生成中间临时文件,如果找不到也会出现这个异常。我在配置

  文件中配置的working-dir就是指定中间临时文件的存储路径。默认不填是没有问题,系统生成然后程序自己去找。而程序部署在docker中就会出现

  系统生成在服务器目录下,程序在docker容器目录下找的尴尬问题。因为我们之前上一步启动docker挂载了opt目录,所以干脆在opt目录下新建文

  件夹配置上就ok了。如果不是docker挂载问题也可以考虑是不是其他类似情况。

4.测试类测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class OpenOfficeTest {

    @Autowired
    private DocumentConverter converter;

    @Test
    public void testPdf(){
        try {
            converter.convert(new File("C:\\Users\\Administrator\\Desktop\\swagger使用说明.docx")).
                    to(new File("C:\\Users\\Administrator\\Desktop\\test.pdf")).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testInputStreamPdf(){
        InputStream in = null;
        OutputStream out = null;
        try {
            converter.convert(in).as(DefaultDocumentFormatRegistry.DOCX).
                    to(out).as(DefaultDocumentFormatRegistry.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值