diff --git a/electromagnetic-common/pom.xml b/electromagnetic-common/pom.xml index 68972c7..f992669 100644 --- a/electromagnetic-common/pom.xml +++ b/electromagnetic-common/pom.xml @@ -49,6 +49,22 @@ logback-core 1.2.3 + + fr.opensagres.xdocreport + fr.opensagres.poi.xwpf.converter.pdf-gae + 2.0.1 + + + + com.documents4j + documents4j-local + 1.0.3 + + + com.documents4j + documents4j-transformer-msoffice-word + 1.0.3 + \ No newline at end of file diff --git a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/OfficeFileUtil.java b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/OfficeFileUtil.java new file mode 100644 index 0000000..ca9380f --- /dev/null +++ b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/OfficeFileUtil.java @@ -0,0 +1,36 @@ +package com.electromagnetic.industry.software.common.util; + +import cn.hutool.core.lang.Assert; +import com.documents4j.api.DocumentType; +import com.documents4j.api.IConverter; +import com.documents4j.job.LocalConverter; +import lombok.extern.slf4j.Slf4j; + +import java.io.*; +import java.nio.file.Files; + +@Slf4j +public class OfficeFileUtil { + + public static void doc2pdf(String wordPath, String pdfPath) { + File inputWord = new File(wordPath); + File outputFile = new File(pdfPath); + try { + InputStream docxInputStream = Files.newInputStream(inputWord.toPath()); + OutputStream outputStream = Files.newOutputStream(outputFile.toPath()); + IConverter converter = LocalConverter.builder().build(); + boolean execute = converter.convert(docxInputStream) + .as(DocumentType.DOCX) + .to(outputStream) + .as(DocumentType.PDF).schedule().get(); + Assert.isTrue(execute, "转换失败"); + outputStream.close(); + docxInputStream.close(); + log.info("转换完毕 targetPath = {}", outputFile.getAbsolutePath()); + converter.shutDown(); + } catch (Exception e) { + log.error("[documents4J] word转pdf失败:{}", e.toString()); + } + } + +}