SpringBoot整合openoffice进行各种格式转换为PDF,文件预览(详细)

SpringBoot使用openoffice进行各种格式转换为PDF(详细)解决连接超时和拒绝远程连接问题

记录一下openoffice转PDF使用方法

OpenOffice安装及启动服务:

1.下载安装包到Linux opt文件夹
2.解压:tar -zxvf Apache_OpenOffice_4.1.6_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
注意:开启服务:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

最好把127.0.0.1替换成服务器ip 可以在服务器桥接一下

pom.xml

我用的是jodconverter 2.2.1 但是2.2.2支持的转换格式更多

jodconverter 2.2.2 下载地址:

    <dependency>
        <groupId>com.artofsolving</groupId>
        <artifactId>jodconverter</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.openoffice</groupId>
        <artifactId>jurt</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.openoffice</groupId>
        <artifactId>ridl</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.openoffice</groupId>
        <artifactId>juh</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.openoffice</groupId>
        <artifactId>unoil</artifactId>
        <version>3.0.1</version>
    </dependency>

效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

工具类

package com.ruifu.conversions.utils;

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.StreamOpenOfficeDocumentConverter;
import com.ruifu.conversions.config.OpenOfficeConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.net.ConnectException;

/**
 * 持txt docx pptx xlsx doc xls ppt 转pdf
 */
@Component
@Slf4j
public class Office4Pdf {
    @Autowired
    OpenOfficeConfig openOfficeConfig;
    public File Office2Pdf(File file){

        //判断源文件是否存在
        if (!file.exists()){
            throw new RuntimeException("源文件不存在!");
        }

        //输出文件
        File outputFile = new File(file.getName().substring(0,file.getName().lastIndexOf("."))+".pdf");

        //创建连接
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(openOfficeConfig.getUrl(),openOfficeConfig.getPort());
        try {

            //远程连接OpenOffice服务
            connection.connect();

            //创建文件转换器
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);

            //开始转换
            converter.convert(file, outputFile);
           if (outputFile.exists()){
               log.info(file.getName()+"文件转换成功");
           }else {
               log.info(file.getName()+"文件转换失败");
           }
        } catch (ConnectException e) {
            e.printStackTrace();
            log.info("OpenOffice服务启动失败");
        }
        return outputFile;
    }
}

##  实现类

package com.ruifu.conversions.service.impl;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.ruifu.common.config.FileType;
import com.ruifu.common.eneity.ConversionTaskQueue;
import com.ruifu.common.po.DmFile;
import com.ruifu.common.utils.FileUtils;
import com.ruifu.conversions.mapper.DmFileMapper;
import com.ruifu.conversions.service.FileConversionService;
import com.ruifu.conversions.utils.Dwg4Pdf;
import com.ruifu.conversions.utils.FastDFSClient;
import com.ruifu.conversions.utils.Office4Pdf;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


/**
 * @date 2019/9/17
 */
@Service
@Slf4j
public class FileConversionServiceImpl implements FileConversionService {
    @Autowired
    private FastDFSClient fastDFSClient;

    @Autowired(required = false)
    private DmFileMapper dmFileMapper;

    @Autowired
    private FastFileStorageClient storageClient;
    @Autowired
    Office4Pdf office4Pdf;

    /**
     * 文件转换
     *
     * @param conversionTaskQueue
     * @throws Exception
     */
    @Override
    public void conversionFileUpload(ConversionTaskQueue conversionTaskQueue) throws IOException {
        //判断文件对象是否为空
        if (conversionTaskQueue.getDmFile() == null) {
            throw new RuntimeException("文件对象为空");
        }
        byte[] bytes = new byte[0];
        try {
            //根据文件路径得到文件数组
            bytes = fastDFSClient.copyFile(conversionTaskQueue.getDmFile().getFiStorPath());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //Byte数组转File
        File sourceFile = FileUtils.ByteToFile(bytes, conversionTaskQueue.getDmFile().getFiFileName());

        //获取文件后缀
        String extName = FilenameUtils.getExtension(sourceFile.getName());
        log.info("文件后缀"+extName);

        //判断源文件后缀是否为dwg
        if (FileType.DWG.equalsIgnoreCase(extName)){
            //dwg文件转换为pdf文件
            File dwgFile = Dwg4Pdf.Dwg2Pdf(sourceFile);
            //上传文件
            StorePath storePath = getFilePath(sourceFile, dwgFile);
            InitializeFile(conversionTaskQueue, dwgFile, storePath);
        }else {
            //判断文件类型是否转换为pdf
            if (FileType.PDF.equalsIgnoreCase(conversionTaskQueue.getType())) {
                //office文件转换为pdf文件
                File pdfFile = office4Pdf.Office2Pdf(sourceFile);
                //上传文件
                StorePath storePath = getFilePath(sourceFile, pdfFile);
                InitializeFile(conversionTaskQueue, pdfFile, storePath);
            }
        }

    }
}





评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值