[英]Convert docx to pdf using documents4j throw exception
I am trying to write a converter for docx to pdf using the documents4j library. 我正在尝试使用documents4j库将docx转换为pdf。 Is there any missiong libraries ?
有没有宣教图书馆? could it be a limitation of the documents4j library ?
可能是document4j库的限制吗?
This is the dependencies I am using : 这是我正在使用的依赖项:
<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>
And this is the code for my converter : 这是我的转换器的代码:
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);
}
I am gettng the below exception. 我得到以下例外。
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)
Any idea about that ? 有什么想法吗?
Thanks. 谢谢。
I figured out that this dependecy is missing : 我发现缺少这种依赖:
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version>
</dependency>
and the conversion code should be written like below : 转换代码应如下所示:
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;
}
Documents4j is best free api for convert docx to pdf. Documents4j是将docx转换为pdf的最佳免费API。
<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>
Use the below code for convert docx to pdf. 使用以下代码将docx转换为pdf。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
public class Document4jApp {
public static void main(String[] args) {
File inputWord = new File("Tests.docx");
File outputFile = new File("Test_out.pdf");
try {
InputStream docxInputStream = new FileInputStream(inputWord);
OutputStream outputStream = new FileOutputStream(outputFile);
IConverter converter = LocalConverter.builder().build();
converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
outputStream.close();
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.