spring boot 集成openOffice实现Office转换成pdf前端在线预览

调研了很多java可实现的转换方式,最终选择了大A开源的项目

这里环境使用的是windows ,linux差不多只是安装过后启动命令不同

1.先去安装openOffice

Apache OpenOffice - Official Download

直接下载

然后傻瓜式安装全部选择下一步

最后启动起来,我这边是远程服务器上安装的

启动命令:

soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard
//顺便提供下linux命令
nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard &
//这里主要下远程使用0.0.0.0 本机使用可以直接127.0.0.1

编译环境:spring boot  2.1.0.RELEASE

<!--jodconverter 开始 -->
		<dependency>
			<groupId>com.artofsolving</groupId>
			<artifactId>jodconverter</artifactId>
		    <version>2.2.1</version>
		</dependency>
<!--jodconverter 结束 -->

只用引入这个即可

下面是转换工具类:


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.lang3.StringUtils;

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

/**
 * 
 * @author zf
 * @desc openoffice转换工具
 */
public class OpenOfficeUtils {

    public static final String LOCAL_HOST = "localhost";
    public static final int LOCAL_PORT = 8100;

    // Format
    public static DocumentFormatRegistry formatFactory = new DefaultDocumentFormatRegistry();

    /**
     * 
     * @param inputStream
     * @param inputFileExtension 示例:pdf
     * @param outputStream
     * @param outputFileExtension 示例:pdf
     * @param connectIp 远程调用ip
     * @param connectPort 远程调用端口
     */
    public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
            String outputFileExtension, String connectIp, int connectPort) throws ConnectException {

        if (inputStream == null || StringUtils.isEmpty(inputFileExtension) || outputStream == null
                || StringUtils.isEmpty(outputFileExtension) || StringUtils.isEmpty(connectIp)) {
            throw new IllegalArgumentException("参数异常!!");
        }

        OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
        connection.connect();
        DocumentConverter converter = getConverter(connectIp, connection);

        converter.convert(inputStream, formatFactory.getFormatByFileExtension(inputFileExtension), outputStream,
                formatFactory.getFormatByFileExtension(outputFileExtension));
        connection.disconnect();
    }

    private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) {
        DocumentConverter converter = "localhost".equals(connectIp) || "127.0.0.1".equals(connectIp)
                || "0:0:0:0:0:0:0:1".equals(connectIp) ? new OpenOfficeDocumentConverter(connection)
                        : new StreamOpenOfficeDocumentConverter(connection);
        return converter;
    }

    public static void main(String[] args) throws Exception {
        File file = new File("H:/zf/index/test/2.xls");
        FileInputStream fileInputStream = new FileInputStream(file);
        File file1 = new File("H:/zf/index/test/2.1.pdf");
        FileOutputStream fileOutputStream = new FileOutputStream(file1);
//这里端口是上面你启动服务设置的端口
        OpenOfficeUtils.convert(fileInputStream, "doc", fileOutputStream, "pdf", "192.168.30.112", 8100);
    }

}

目前转换office可以正常转换,excel因为本身长度,openOffice会以4A大小转换所以要覆写相关类调整

/**
 * 自定义转换格式
 * @author zf
 */
public class ConverterDocument extends StreamOpenOfficeDocumentConverter {
    public ConverterDocument(OpenOfficeConnection connection) {
        super(connection);
    }
 
    public final static Size A5, A4, A3;
    public final static Size B4, B5, B6;
    public final static Size paperSize;
 
    static {
        A5 = new Size(14800, 21000);
        A4 = new Size(21000, 29700);
        A3 = new Size(29700, 42000);
 
        B4 = new Size(25000, 35300);
        B5 = new Size(17600, 25000);
        B6 = new Size(12500, 17600);
        //最大限度  宽 1600000
        paperSize = new Size(29700, 27940);
    }
 
 
 
    @Override
    protected void refreshDocument(XComponent document) {
        super.refreshDocument(document);
        XPrintable xPrintable = (XPrintable) UnoRuntime.queryInterface(XPrintable.class, document);
        PropertyValue[] printerDesc = new PropertyValue[2];
        // 转换
        printerDesc[0] = new PropertyValue();
        printerDesc[0].Name = "PaperFormat";
        printerDesc[0].Value = PaperFormat.USER;
 
        // 纸张大小
        printerDesc[1] = new PropertyValue();
        printerDesc[1].Name = "PaperSize";
        printerDesc[1].Value = paperSize;
 
        try {
            xPrintable.setPrinter(printerDesc);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
}

将工具类用到

StreamOpenOfficeDocumentConverter 改成我们实现的类即可,大小可以自己调整
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值