引入 jodconverter 相关jar,配合libreOffice、openOffice两款软件,只需进行简单编码可以实现各种文档转换。
目前已在两个项目中应用:
<!-- 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; } }
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