【问题标题】:Convert docx to pdf using documents4j throw exception使用documents4j将docx转换为pdf抛出异常
【发布时间】:2018-06-28 13:35:25
【问题描述】:

我正在尝试使用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)

对此有什么想法吗?

谢谢。

【问题讨论】:

  • 你定义了 TEMP_PATH 吗?
  • 是的,TEMP_PATH 存在。

标签: java documents4j


【解决方案1】:

我发现缺少这种依赖:

<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;
}

【讨论】:

    【解决方案2】:

    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>
    

    使用以下代码将 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();
            }
        }
    }
    

    【讨论】:

    • ... 并且需要在服务器上安装 ms office。
    • tnx ,但它不支持波斯语和从右到左的语言。 :)
    • 注意。这在基于 UNIX 的系统中不起作用。
    • @brebDev,将此用于 linux/unix stackoverflow.com/questions/3022376/…
    • 在使用documents4j之前,我已经用docx4j做了一些测试。生成的 pdf 中包含红色标签,例如:未实现:支持 w:pict 没有 v:imagedata 或未实现:支持 w:ptab。
    猜你喜欢
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 2017-05-17
    • 2016-09-13
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多