jodconvert的亚子

9次阅读

共计 3747 个字符,预计需要花费 10 分钟才能阅读完成。

简介

引入 jodconverter 相关 jar,配合 libreOffice、openOffice 两款软件,只需进行简单编码可以实现各种文档转换。

应用

目前已在两个项目中应用:

  • F 项目需要滚动播放视频、文档(Excel/Word…) 功能, 使用 jodconverter 将文档转成 pdf,结合 pdfjs 实现。由于个人独立开发,只要结果,故选型自由。后端 Spring boot,前端阿里飞冰。可以直接使用 jodconverter 的 starter:
<!-- jodconverter newer 4.2 -->
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-core</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-local</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-spring-boot-starter</artifactId>
    <version>4.2.0</version>
</dependency>

使用也非常方便,注入可用:

import java.io.File;
import javax.annotation.Resource;
import org.jodconverter.DocumentConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.foton.common.Constants;

@Component
public class DocumentConverterUtil {private final Logger logger = LoggerFactory.getLogger(this.getClass());
//    @Value("${office.home}")
//    String officeHome;
    @Resource
    private DocumentConverter  documentConverter;
    
    public String convert(File in, File out) {//        DocumentFormat pdf = documentConverter.getFormatRegistry().getFormatByExtension("pdf");

        try {String fileName=in.getName();
            String fileType=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length()); 

            // Excel 尺寸控制
            if(fileType.equals("xls"))
                ConverterUtil.setXlsFitToWidth(in);
            long startTime = System.currentTimeMillis();
            documentConverter.convert(in).to(out).execute();
            long conversionTime = System.currentTimeMillis() - startTime;
            logger.info(String.format("successful conversion: %s to %s in %dms",in.getName(), "pdf", conversionTime));

        } catch (Exception e) {e.printStackTrace();
            return Constants.FAIL;
        }

        return Constants.SUCCESS;
    }
}
  • H 项目应审查要求,原本开发的导出 Excel 功能需调整为导出 pdf,因此在原来基础功能上用 jodconvert 额外增加一次转换。由于公司要求限制,该项目在 jdk1.7 下开发,非 maven,因此需要传统 jar 方式引入相关依赖 jar 包。值得注意的是,jodconvert 在 4.1.0 不支持 jdk7,而 4.1.0 版本仅会吃 libreOffice 5(libreOffice 升级到 6 后目录变更,作者在 4.2.0 版本中调整,但该版本不支持 jdk7)。此外,convert 的启动与结束需要自己控制。
import com.eos.runtime.core.IRuntimeListener;
import com.eos.runtime.core.RuntimeEvent;

public class JodConverterStartupListener implements IRuntimeListener {

    @Override
    public void start(RuntimeEvent arg0) {DocumentConverterUtil.start(); // 项目启动时调用
    }

    @Override
    public void stop(RuntimeEvent arg0) {DocumentConverterUtil.stop(); // 结束时关闭
    }

}
import java.io.File;

import org.jodconverter.DocumentConverter;
import org.jodconverter.LocalConverter;
import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DocumentConverterUtil {private static final Logger logger = LoggerFactory.getLogger(DocumentConverterUtil.class);
    public static DocumentConverter  documentConverter;
    public static OfficeManager officeManager;
    
    public static void start() {officeManager = LocalOfficeManager.builder().build();
        documentConverter = LocalConverter.make(officeManager);

        try {officeManager.start();
            logger.info(">>> office 转换服务启动成功!");
        } catch (OfficeException e) {e.printStackTrace();
        }
    }
    
    public static void converter(String inputFilePath, String outputFilePath) throws Exception {File inputFile = new File(inputFilePath);
        if (inputFile.exists()) {// 找不到源文件, 则返回  
            File outputFile = new File(outputFilePath);
            if (!outputFile.getParentFile().exists()) { // 假如目标路径不存在, 则新建该路径  
                outputFile.getParentFile().mkdirs();
            }
            
            String fileType = inputFilePath.substring(inputFilePath.lastIndexOf(".")+1,inputFilePath.length()); 

            if(fileType.equals("xls"))
                ConverterUtil.setXlsFitToWidth(inputFile);
            
            documentConverter.convert(inputFile)
                    .to(outputFile)
                    .execute();}
            
    }
    
    
    public static void stop() {if (officeManager.isRunning()) {
            try {officeManager.stop();
            } catch (OfficeException e) {e.printStackTrace();
            }
            logger.info(">>> office 转换服务完成。");
        }
    }
}

关于我

rebey.cn

正文完
 0