摘要:由于公司要求限制,該項(xiàng)目在下開發(fā),非,因此需要傳統(tǒng)方式引入相關(guān)依賴包。此外,的啟動(dòng)與結(jié)束需要自己控制。項(xiàng)目啟動(dòng)時(shí)調(diào)用結(jié)束時(shí)關(guān)閉轉(zhuǎn)換服務(wù)啟動(dòng)成功找不到源文件則返回假如目標(biāo)路徑不存在則新建該路徑轉(zhuǎn)換服務(wù)完成。
簡介
引入jodconverter相關(guān)jar,配合libreOffice、openOffice兩款軟件,只需進(jìn)行簡單編碼可以實(shí)現(xiàn)各種文檔轉(zhuǎn)換。
應(yīng)用目前已在兩個(gè)項(xiàng)目中應(yīng)用:
F項(xiàng)目需要滾動(dòng)播放視頻、文檔(Excel/Word...)功能,使用jodconverter將文檔轉(zhuǎn)成pdf,結(jié)合pdfjs實(shí)現(xiàn)。由于個(gè)人獨(dú)立開發(fā),只要結(jié)果,故選型自由。后端Spring boot,前端阿里飛冰??梢灾苯邮褂胘odconverter的starter:
org.jodconverter jodconverter-core 4.2.0 org.jodconverter jodconverter-local 4.2.0 org.jodconverter jodconverter-spring-boot-starter 4.2.0
使用也非常方便,注入可用:
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項(xiàng)目應(yīng)審查要求,原本開發(fā)的導(dǎo)出Excel功能需調(diào)整為導(dǎo)出pdf,因此在原來基礎(chǔ)功能上用jodconvert額外增加一次轉(zhuǎn)換。由于公司要求限制,該項(xiàng)目在jdk1.7下開發(fā),非maven,因此需要傳統(tǒng)jar方式引入相關(guān)依賴jar包。值得注意的是,jodconvert在4.1.0不支持jdk7,而4.1.0版本僅會吃libreOffice 5(libreOffice升級到6后目錄變更,作者在4.2.0版本中調(diào)整,但該版本不支持jdk7)。此外,convert的啟動(dòng)與結(jié)束需要自己控制。
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(); // 項(xiàng)目啟動(dòng)時(shí)調(diào)用 } @Override public void stop(RuntimeEvent arg0) { DocumentConverterUtil.stop(); // 結(jié)束時(shí)關(guān)閉 } }
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轉(zhuǎn)換服務(wù)啟動(dòng)成功!"); } 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()) { // 假如目標(biāo)路徑不存在, 則新建該路徑 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轉(zhuǎn)換服務(wù)完成。"); } } }關(guān)于我
rebey.cn
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/77898.html
摘要:用將文檔轉(zhuǎn)換本例使用。在和環(huán)境下測試通過。轉(zhuǎn)換命令源文件放在或者封裝了一組轉(zhuǎn)換命令,通過調(diào)用相關(guān)服務(wù)。安裝檢查已有字體庫復(fù)制字體新建文件夾把系統(tǒng)的字體復(fù)制進(jìn)去。 用LibreOffice將Office文檔轉(zhuǎn)換PDF 本例使用 LibreOffice-6.0.4、jodconverter-4.2.0、spring-boot-1.5.9.RELEASE。 在CentOS7 + ope...
摘要:在下一切運(yùn)行正常,但是到下文件的文件名和生成路徑就會發(fā)生變化,這里的不會被當(dāng)作路徑分隔符了,而是當(dāng)作文件名的一部分,其實(shí)修改起來也很簡單創(chuàng)建另一個(gè)線程啟動(dòng)服務(wù),老是會出現(xiàn)無法連接服務(wù)的異常。 php預(yù)覽word文檔的實(shí)現(xiàn) 以及實(shí)現(xiàn)過程中遇到的各種坑 在做軟件工程的課程設(shè)計(jì)的時(shí)候,我們小組選擇做一個(gè)資料分享網(wǎng)站,網(wǎng)站最重要的功能當(dāng)然就是上傳文件和下載文件。但是這中間就需要一個(gè)比較重要的過...
摘要:在下一切運(yùn)行正常,但是到下文件的文件名和生成路徑就會發(fā)生變化,這里的不會被當(dāng)作路徑分隔符了,而是當(dāng)作文件名的一部分,其實(shí)修改起來也很簡單創(chuàng)建另一個(gè)線程啟動(dòng)服務(wù),老是會出現(xiàn)無法連接服務(wù)的異常。 php預(yù)覽word文檔的實(shí)現(xiàn) 以及實(shí)現(xiàn)過程中遇到的各種坑 在做軟件工程的課程設(shè)計(jì)的時(shí)候,我們小組選擇做一個(gè)資料分享網(wǎng)站,網(wǎng)站最重要的功能當(dāng)然就是上傳文件和下載文件。但是這中間就需要一個(gè)比較重要的過...
閱讀 3471·2021-10-27 14:20
閱讀 2639·2021-10-08 10:05
閱讀 1691·2021-09-09 09:33
閱讀 2955·2019-08-30 13:16
閱讀 1502·2019-08-29 18:34
閱讀 1241·2019-08-29 10:58
閱讀 1290·2019-08-28 18:22
閱讀 1282·2019-08-26 13:33