首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用documents4j抛出异常将docx转换为pdf

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

Stack Overflow用户
提问于 2018-01-19 18:27:25
回答 2查看 4.3K关注 0票数 2

我正在尝试使用documents4j库编写一个从docx到pdf的转换器。有密松图书馆吗?这会不会是documents4j库的一个限制?

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

代码语言:javascript
复制
<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>

这是我的转换器的代码:

代码语言:javascript
复制
    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);


    }

我得到了下面的异常。

代码语言:javascript
复制
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)

对此有什么想法吗?

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2018-01-19 22:32:20

我发现这个依赖关系缺失了:

代码语言:javascript
复制
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-transformer-msoffice-word</artifactId>
    <version>1.0.3</version>
</dependency>

转换代码的写法如下:

代码语言:javascript
复制
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;
}
票数 2
EN

Stack Overflow用户

发布于 2019-02-11 20:30:59

Documents4j是将docx转换为pdf的最好的免费api。

代码语言:javascript
复制
<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>

使用下面的代码将docx转换为pdf。

代码语言:javascript
复制
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();
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48338925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档