Java 使用openoffice转换office文档为pdf

package com.lpo.web.controller.common;

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;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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


public class OfficeToPDF {
    static final String host = "xxx.xxx.xxx.xxx";
    static final int post = 8100;

    public static void toPdf(String filePath, String pdfPath) {
        try {
            long old = System.currentTimeMillis();
            System.out.println("open-开始:" + filePath);
            File wordFile = new File(filePath);
            File PDFFile = new File(pdfPath);
            SocketOpenOfficeConnection connection = new SocketOpenOfficeConnection(host, post);

            try {
                connection.connect();
                DocumentConverter converter = getConverter(host, connection);
                converter.convert(wordFile, PDFFile);
                long now = System.currentTimeMillis();
                System.out.println("open-完成:" + filePath);
                System.out.println("共耗时:" + (double) (now - old) / 1000.0D + "秒");
            } catch (ConnectException var14) {
                System.out.println("获取OpenOffice连接失败...");
                var14.printStackTrace();
            } finally {
                connection.disconnect();
            }
        } catch (Exception var16) {
            System.out.println("转换失败:" + filePath);
            var16.printStackTrace();
        }

    }


    /**
     * excel设置 打印缩放比例
     *
     * @param inputPath
     * @param outPath
     */
    public static void setExcelScale(String inputPath, String outPath) throws IOException {
        //读取excel文件
        XSSFWorkbook workbook;

        workbook = new XSSFWorkbook(new FileInputStream(inputPath));

        OutputStream fos = null;
        try {
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表
                XSSFSheet sheet = workbook.getSheetAt(i);
                //打印设置
                XSSFPrintSetup print = sheet.getPrintSetup();
                print.setLandscape(true); // 打印方向,true:横向,false:纵向(默认)
                print.setFitHeight((short) 0);//设置高度为自动分页
                print.setFitWidth((short) 1);//设置宽度为一页
                print.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张类型
//                print.setScale((short)55);//自定义缩放①,此处100为无缩放
                //启用“适合页面”打印选项的标志
                sheet.setFitToPage(true);
            }
            // Excel文件生成后存储的位置。
            File file = new File(outPath);
            fos = new FileOutputStream(file);
            workbook.write(fos);
        } catch (Exception e) {
            System.out.println("setExcelScale fail: 创建输出文件IO异常");
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    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 StreamOpenOfficeDocumentConverter(connection)
                : new OpenOfficeDocumentConverter(connection);
        return (DocumentConverter) converter;
    }
}

1.安装openoffice,以windows系统为例,cmd进入openoffice安装文件目录program文件夹内执行,soffice -headless -accept="socket,host=xxx.xxx.xxx.xxx,port=8100;urp;" -nofirststartwizard

2.调用该方法即可实现转换pdf,且解决原生openoffice转换后文档走样问题。

jar包链接:https://pan.baidu.com/s/17-dyFq7PM9wE9rMVHVawBw  
提取码:0oq5 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值