spring boot 各种文档类型转pdf 之openOffice使用

项目需要将各种txt docx pptx ppt doc xlsx xls 文档类型转成PDF 并在终端显示,查阅资料发现能使用openoffice实现,记录一下使用心得。

一 安装openoffice

网上很多安装教程 window版本下无脑下一步。

spring boot 导入依赖

注意 :jodconverter 2.2.1 不支持docx和xlsx等格式 而jodconverter 2.2.2版本是支持。但是jodconverter 2.2.2 在maven仓库是找到不到的 需要自己去网上下载jar包后放在项目下自己导入
spring boot 各种文档类型转pdf 之openOffice使用_第1张图片

        <!-- openoffice word转pdf-->
<!--        <dependency>-->
<!--            <groupId>com.artofsolving</groupId>-->
<!--            <artifactId>jodconverter</artifactId>-->
<!--            <version>2.2.1</version>-->
<!--        </dependency>-->

        <!-- openoffice2.2.2 版本 支持多种文本格式转PDF-->
        <dependency>
            <groupId>com.sun</groupId>  <!--自定义-->
            <artifactId>jna</artifactId>    <!--自定义-->
            <version>1.0</version> <!--自定义-->
            <scope>system</scope> <!--system,类似provided,需要显式提供依赖的jar以后,Maven就不会在Repository中查找它-->
            <systemPath>${project.basedir}/src/main/resources/lib/jodconverter-2.2.2.jar</systemPath> <!--项目根目录下的lib文件夹下-->
        </dependency>

        <dependency>
            <groupId>com.sun</groupId>  <!--自定义-->
            <artifactId>jna</artifactId>    <!--自定义-->
            <version>1.0</version> <!--自定义-->
            <scope>system</scope> <!--system,类似provided,需要显式提供依赖的jar以后,Maven就不会在Repository中查找它-->
            <systemPath>${project.basedir}/src/main/resources/lib/commons-io-1.4.jar</systemPath> <!--项目根目录下的lib文件夹下-->
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>jurt</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>ridl</artifactId>
            <version>4.1.2</version>
        </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.1</version>
        </dependency>

spring boot 下使用openOffice

package com.example.demo.until;



import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormatRegistry;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.*;
import java.net.ConnectException;
import java.net.URL;

/**
 * @Author:linjunbo
 * @Description:
 * @Date: 2020/4/28 14:21
 */
@Component
public class Office4Pdf {
    public static File Office2Pdf(File file){
    	//启动服务
        openOpenOffice();
        /*支持txt docx pptx ppt doc xlsx xls  转pdf*/
        //判断源文件是否存在
        if (!file.exists()){
            throw new RuntimeException("源文件不存在!");
        }
        // 输出文件目录
        File outputFile = new File("C:/buildingBoard/video/pic/photo/"+file.getName().substring(0,file.getName().lastIndexOf("."))+".pdf");
        // 创建openoffice服务连接
        OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
        try {
            //连接OpenOffice服务
            connection.connect();
            //创建文件转换器
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            //开始转换
            converter.convert(file, outputFile);
            if (outputFile.exists()){
                System.out.println("文件转换成功!");
                return outputFile;
            }
        } catch (ConnectException e) {
            e.printStackTrace();
            throw new RuntimeException("OpenOffice服务启动失败!");
        }finally {
        	//关闭连接
            connection.disconnect();

        }
        return outputFile;
    }


	//启动OpenOffice服务
    private static void openOpenOffice() {
        try {
        //openOffice的安装路径
        String command = "C:\\Program Files (x86)\\OpenOffice 4\\"
                + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";

            Process pro = Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) throws ConnectException, FileNotFoundException {
       	//测试文件路径
         String path = "C:/buildingBoard/video/pic/photo/1121..1.doc";

         File file = new File(path)

         System.out.println(  Office2Pdf(file));

    }
}


你可能感兴趣的:(Springboot)