From 799fb336c17aadfad70026dd82d1b91e578822c8 Mon Sep 17 00:00:00 2001 From: chenxudong Date: Fri, 17 Jan 2025 09:40:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0word=E8=BD=ACpdf=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electromagnetic-common/pom.xml | 16 +++++++++ .../software/common/util/OfficeFileUtil.java | 36 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/OfficeFileUtil.java 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()); + } + } + +}