gpt4 book ai didi

java - 使用documents4j将docx转换为pdf抛出异常

转载 作者:行者123 更新时间:2023-11-30 02:16:11 27 4
gpt4 key购买 nike

我正在尝试使用 Documents4j 库编写一个 docx 到 pdf 的转换器。有没有任何任务库?这可能是 Documents4j 库的限制吗?

这是我正在使用的依赖项:

<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-api</artifactId>
<version>1.0.3</version>
</dependency>

<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-util-conversion</artifactId>
<version>1.0.3</version>
</dependency>

<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer</artifactId>
<version>1.0.3</version>
</dependency>

<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-util-all</artifactId>
<version>1.0.3</version>
</dependency>

<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.0.3</version>
</dependency>

这是我的转换器的代码:

    public static FileInputStream convert(InputStream docxInputStream) throws FileNotFoundException {

IConverter converter = LocalConverter.builder()
.baseFolder(new File("C:\\"))
.workerPool(20, 25, 2, TimeUnit.SECONDS)
.processTimeout(5, TimeUnit.SECONDS)
.build();
FileOutputStream fileOutputStream = new FileOutputStream(new File(TEMP_PATH));

converter.convert(docxInputStream).as(DocumentType.DOCX)
.to(fileOutputStream).as(DocumentType.PDF)
// .prioritizeWith(1000) // optional
.schedule();

return new FileInputStream(TEMP_PATH);


}

我收到以下异常。

java.lang.IllegalStateException: The application was started without any registered or class-path discovered converters.
at com.documents4j.conversion.ExternalConverterDiscovery.validate(ExternalConverterDiscovery.java:68)
at com.documents4j.conversion.ExternalConverterDiscovery.loadConfiguration(ExternalConverterDiscovery.java:85)
at com.documents4j.conversion.DefaultConversionManager.<init>(DefaultConversionManager.java:22)
at com.documents4j.job.LocalConverter.makeConversionManager(LocalConverter.java:79)
at com.documents4j.job.LocalConverter.<init>(LocalConverter.java:51)
at com.documents4j.job.LocalConverter$Builder.build(LocalConverter.java:186)
at com.bnpparibas.sit.communication.historage.utilities.converting.DocxToPDFConverter.convert(DocxToPDFConverter.java:30)

对此有什么想法吗?

谢谢。

最佳答案

我发现缺少这种依赖性:

<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version>
</dependency>

转换代码应如下所示:

public static FileInputStream convert(InputStream docxInputStream) throws FileNotFoundException {

FileInputStream inputStream = null;
try (OutputStream outputStream = new FileOutputStream(new File(TEMP_PATH))) {
IConverter converter = LocalConverter.builder().build();
converter
.convert(docxInputStream).as(DocumentType.DOCX)
.to(outputStream).as(DocumentType.PDF)
.prioritizeWith(1000).schedule();
inputStream = new FileInputStream(TEMP_PATH);

} catch (Exception e) {
LOGGER.error(e.getMessage());
}
return inputStream;
}

关于java - 使用documents4j将docx转换为pdf抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48338925/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com