office文件转PDF

10 篇文章 0 订阅

office文件转PDF

所需依赖 : jodconverter-2.2.2.jar
注:转换文件需要部署openoffice环境,可参考博主蓝色格子的文章

	/**
     * office文件转PDF
     * @param file
     * @return
     */
    public static File Office2Pdf(File file,String pdfSavePath, String openOfficeIp){
        String rule = "txt|doc|docx|xlsx|xls|pdf|ppt|pptx";
        String fileType = file.getName().substring(file.getName().indexOf(".") + 1);
        if (!fileType.toLowerCase().matches(rule)){
            logger.info("不支持的文件格式{}",fileType);
            return file;
        }
        //判断源文件是否存在
        if (!file.exists()){
            throw new RuntimeException("源文件不存在!");
        }
        //输出文件
        File pdfFile = new File( pdfSavePath + File.separator + file.getName().substring(0,file.getName().lastIndexOf("."))+".pdf");
        // 转换后的pdf文件保存位置
        logger.info("转换后的pdf文件保存位置:{}",pdfFile.getAbsolutePath());
        OpenOfficeConnection connection = null;
        try {
            logger.debug("PDF开始转换");
            // PDF格式特殊处理
            if ("pdf".equals(fileType)){
                FileUtils.copyFile(file,pdfFile);
            }else{
                //创建连接
                connection = new SocketOpenOfficeConnection(openOfficeIp,8100);
                //远程连接OpenOffice服务
                connection.connect();
                //创建文件转换器
                DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
                //开始转换
                converter.convert(file, pdfFile);
            }
            Thread.sleep(1000);
            if (pdfFile.exists()){
                logger.info("文件{}转换成功",file.getAbsolutePath());
            }else {
                logger.info("文件{}转换失败",file.getAbsolutePath());
            }
            if (connection != null && connection.isConnected()){
                connection.disconnect();
            }
        } catch (Exception e) {
            if (connection != null && connection.isConnected()){
                connection.disconnect();
            }
            e.printStackTrace();
            logger.error("office转PDF异常:{}",e.getMessage());
            return file;
        }
        return pdfFile;
    }

Main 测试

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 org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;

/**
 * @Describe: office文件转PDF工具
 * @Author: zml
 * @Date: 2020-5-18 17:58:51
 */
public class OfficeToPDFUtils {

    private static Logger logger = LoggerFactory.getLogger(OfficeToPDFUtils.class);

    public static void main(String[] args) {
        File file = new File("D:\\liang\\office转PDF\\测试文件.docx");
        String pdfSavePath = "D:\\liang\\office转PDF";
        String openOfficeIp = "111.229.31.20";
        Office2Pdf(file,pdfSavePath,openOfficeIp);
    }
}

对比示例
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值