Java使用openOffice预览文件

1.Java代码编写和依赖

https://blog.csdn.net/asing1elife/article/details/82963363

①纠正文中错误:

 下载 JODConverter ,在其 lib 目录中找到 jodconverter-2.2.2.jar ,并引入以下 jar 包

补充:我是在src/main/resources/下建立了lib目录,并把jar包放进去,然后pom配置

<dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>jodconverter</artifactId>
            <version>2.2.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/jodconverter-2.2.2.jar</systemPath>
</dependency>

<build>
<!-- 解决了打包时缺失本地jar包问题 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <webResources>
                        <resource>
                            <directory>src/main/resources/lib</directory>
                            <targetPath>WEB-INF/lib/</targetPath>
                            <includes>
                                <include>**/*.jar</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
</build>

②有歧义的地方

File pdfFile = new File(fileName.substring(0, fileName.lastIndexOf(".")) + Constants.REPORT_FILE_PREVIEW_SUFFIX); 

OpenOfficeConnection connection = new SocketOpenOfficeConnection(Constants.OPEN_OFFICE_CONNECTION_PORT);

Constants.REPORT_FILE_PREVIEW_SUFFIX 改成 ".pdf"

Constants.OPEN_OFFICE_CONNECTION_PORT 改成 "127.0.0.1",8100

其实就是openOffice的IP和端口。

 

2.openoffice4安装和启动

http://asing1elife.com/java/openoffice/linux/2017/04/11/OpenOffice-%E5%9C%A8-Linux-%E4%B8%8B%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8/

①补充:下载openOffice时,centos的系统选择下载rpm版本的,Ubuntu系统选择下载deb的

②补充:启动openOffice时,执行

/opt/openoffice4/program/soffice "-accept=socket,host=127.0.0.1,port=8100;urp;" -headless -nofirststartwizard &

127.0.0.1只能本地连接,改成0.0.0.0则可以远程连接。

 

3.openOffice4启动报错no suitable windowing system found, exiting.

执行以下命令:

yum groupinstall "X Window System"

 

4.springboot项目集成openOffice后启动报错

org\openoffice\lib' must exist and must be a directory

解决:根据报错提示,直接在本地仓库对应目录建lib或bin目录

 

5.文件转换错误

OpenOfficeException: conversion failed: could not load input document

解决:

DocumentConverter converter = new OpenOfficeDocumentConverter(connection);

改成

DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);

6.转成PDF字体方块乱码(原因:系统缺少对应的字体)

①下载linux字体包:

链接:https://pan.baidu.com/s/1xR9ujoYFVXoiv8LNtbcu1w 
提取码:wd8h 

②解压放到linux的/usr/share/fonts/路径下

③刷新字体缓存

fc-cache   如果提示 fc-cache: command not found,则需要安装# yum install fontconfig )

④kill掉openOffice服务,然后重新启动

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值