jodconvert的亚子

栏目: Java · 发布时间: 6年前

内容简介:引入目前已在两个项目中应用:使用也非常方便,注入可用:

简介

引入 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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Sovereign Individual

The Sovereign Individual

James Dale Davidson、William Rees-Mogg / Free Press / 1999-08-26 / USD 16.00

Two renowned investment advisors and authors of the bestseller The Great Reckoning bring to light both currents of disaster and the potential for prosperity and renewal in the face of radical changes ......一起来看看 《The Sovereign Individual》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具