Java使用OpenOffice实现文件转换为PDF文件(三)

项目背景:开发中需要实现word、ppt文件的在线预览,而浏览器无法打开此类文件,于是需要上传文件的时候转PDF文件存储。

下面是具体操作步骤:

一、OpenOffice是跨平台的免费软件套件,下载地址:http://www.openoffice.org/

二、下载 jodconverter-2.2.2.zip,MAVEN导入jodconverter-2.2.2.jar失败,需要手动建立依赖包。

地址:https://sourceforge.net/projects/jodconverter/files/JODConverter/
在这里插入图片描述
注意:如果使用2.2.1这个包无法操作docx、pptx文件

三、 安装完之后,在安装目录的program文件下cmd执行命令

C:\Program Files (x86)\OpenOffice 4\program> soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard

四、项目的pom.xml文件下maven配置

<!--openOffice-->
        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>com.artofsolving</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>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>juh</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>unoil</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!--openOffice-->

五、编写测试类。


import java.io.File;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class File2PDFUtil {

    /**
     * 使用openoffice 文件转换为pdf格式
     *
     * @param sourceFilePath 源文件
     * @param destFilePath   目标文件
     */
    public static void fileTwoPdf(String sourceFilePath, String destFilePath) {
        File inputFile = new File(sourceFilePath);
        // 找不到源文件, 则返回false
        if (!inputFile.exists()) {
            System.err.println("文件不存在!");
        }
        // 如果目标路径不存在, 则新建该路径
        File outputFile = new File(destFilePath);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }
        // 如果目标文件存在,则删除
        if (outputFile.exists()) {
            outputFile.delete();
        }
        try {
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
            connection.connect();
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
            if (inputFile.exists()) {
                // 进行PDF格式的转换
                converter.convert(inputFile, outputFile);
            }
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        fileTwoPdf("D:\\Temp\\patch\\sas.xlsx", "D:\\Temp\\patch\\22.pdf");
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lovme_du

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值