增加word转pdf的功能

This commit is contained in:
chenxudong 2025-01-17 09:40:49 +08:00
parent 3a090c224f
commit 799fb336c1
2 changed files with 52 additions and 0 deletions

View File

@ -49,6 +49,22 @@
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
</project>

View File

@ -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());
}
}
}