jodconvert的亞子

簡介

引入jodconverter相關jar,配合libreOffice、openOffice兩款軟件,只需進行簡單編碼能夠實現各類文檔轉換。前端

應用

目前已在兩個項目中應用:java

  • 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>

使用也很是方便,注入可用:git

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.cngithub

相關文章
相關標籤/搜索