• java使用openOffice将word转换为pdf


    每日分享:java使用openOffice将word转换为pdf
    https://note.youdao.com/s/7TBuadfH

    一、安装部署openOffice
    1. 下载openoffice的安装包(注意选择合适的安装包):
    http://www.openoffice.org/download/archive.html
     
    1. 安装
    • 使用tar -xzvf xxxx.tar.gz解压缩后,会得到对应的解压文件 /zh-CN
    目录。
    • 进入/zh-CN//RPMS/目录
    • 执行 rpm –ivh *rpm(安装所有rpm文件)
    1. 启动OpenOffice服务
    • 进入opt目录:cd /opt
    • 进入openoffice.org3目录: cd openoffice.org3
    • 进入cd program目录
    • 执行 soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard &
    • 完成本地启动,如果需要远程访问,需要将host地址改成机器对应IP地址;
    1. java调用openOffice服务将word转换为PDF
    import java.io.*;
    
    import com.alibaba.druid.util.StringUtils;
    import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
    import com.artofsolving.jodconverter.DocumentConverter;
    import com.artofsolving.jodconverter.DocumentFormat;
    import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
    import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
    import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    
    
    @Component
    @Service
    @Slf4j
    public class Word2PdfOpenOffice {
    
        @Value("${openOffice.host}")
        private String openOfficeHost;
        @Value("${openOffice.port}")
        private Integer openOfficePost;
    
        public int office2PDF(String sourceFile, String destFile) {
            if(StringUtils.isEmpty(sourceFile) || StringUtils.isEmpty(destFile) ){
                log.debug("文件路径为空");
                return -1;
            }
            log.debug("sourceFile===========" + sourceFile);
            log.debug("destFile===========" + destFile);
    
            try {
                File inputFile = new File(sourceFile);
                if (!inputFile.exists()) {
                    log.debug("没有文档===========");
                    return -1;// 找不到源文件, 则返回-1
                }
                // 如果目标路径不存在, 则新建该路径
                File outputFile = new File(destFile);
                if (!outputFile.getParentFile().exists()) {
                    outputFile.getParentFile().mkdirs();
                }
                // 文件输入输出流
                FileInputStream in = null;
                FileOutputStream out = null;
                in = new FileInputStream(inputFile);
                out = new FileOutputStream(outputFile);
    
                if(in == null){
                    log.debug("文件输入流为空===========");
                    return -1;
                }
                if(out == null){
                    log.debug("文件输出流为空===========");
                    return -1;
                }
                // 创建连接
                OpenOfficeConnection connection = new SocketOpenOfficeConnection(openOfficeHost, openOfficePost);
    
                //OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
                connection.connect();
    
                // 创建转换器
                DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                // 文件格式
                DefaultDocumentFormatRegistry factory = new DefaultDocumentFormatRegistry();
    
    
                // 待转换文件名称及扩展名称
                String officeName = inputFile.getName();
                String officeExt = officeName.substring(officeName.lastIndexOf(".") + 1);
                // 待转换文件 转换文件格式对象
                DocumentFormat officeFormat = factory.getFormatByFileExtension(officeExt);
                DocumentFormat pdfFormat = factory.getFormatByFileExtension("pdf");
                log.debug("开始将word转为PDF===========");
    
                // 记录转换时间 咨询转换
                long startTime = System.currentTimeMillis();
                converter.convert(in, officeFormat, out, pdfFormat);
    //            converter.convert(in, out);
                long endTime = System.currentTimeMillis();
                log.debug("转换时间:" + (endTime - startTime) + "ms");
    
                //断开连接
                connection.disconnect();
                return 0;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                log.debug("FileNotFoundException===========" + e.getMessage() );
                return -1;
            } catch (IOException e) {
                e.printStackTrace();
                log.debug("IOException===========" + e.getMessage());
    
                return -1;
            } catch (Exception e){
                e.printStackTrace();
                log.debug("Exception===========" + e.getMessage());
                return -1;
            }
    //        return 1;
        }
    
    
        public static void main(String[] args){
    
        }
    
    }
  • 相关阅读:
    druid 连接池的配置参数
    docker启动tomcat容器访问端口显示404
    idea 查看类继承关系的快捷键
    EmbeddedServletContainerCustomizer 被代替
    Springboot中WebMvcConfigurer接口详解
    Thymeleaf 参考手册
    CSS之px、em、rem、pt的用法和区别
    CSS之text-align
    相对路径和绝对路径(实例)
    CSS的引入方式及link和@import的区别
  • 原文地址:https://www.cnblogs.com/Anonyme/p/16250943.html
Copyright © 2020-2023  润新知