Преобразование docx в pdf с использованием исключения document4j

Я пытаюсь написать конвертер для docx в pdf, используя библиотеку document4j. Есть ли библиотеки миссий? может ли это быть ограничением библиотеки 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)

Любая идея об этом?

Спасибо.


person H.abidi    schedule 19.01.2018    source источник
comment
Вы определили TEMP_PATH?   -  person Laurens    schedule 19.01.2018
comment
Да, TEMP_PATH присутствует.   -  person H.abidi    schedule 19.01.2018


Ответы (2)


Я понял, что эта зависимость отсутствует:

<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;
}
person H.abidi    schedule 19.01.2018

Documents4j — лучший бесплатный API для преобразования docx в pdf.

<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();
        }
    }
}
person Sathia    schedule 11.02.2019
comment
... и нужно установить MS Office на сервер. - person amir110; 10.01.2021
comment
Правда, используйте этот конвертер для linux и windows env ="как преобразовать текстовый документ в pdf"> stackoverflow.com/questions/3022376/ - person Sathia; 10.01.2021
comment
tnx, но он не поддерживает персидский язык и языки справа налево. :) - person amir110; 10.01.2021