javapdfdocxfile-conversiondocuments4j

How do I solve this below exception I get when converting docx to pdf using Documents4j library?


I am using the below code to convert docx to pdf.

    public static void main(String[] args) {
    File inputdocxfile = new File(System.getProperty("user.dir") + "/src/test/resources/files/output/");

    File outputpdffile = new File(System.getProperty("user.dir") + "/src/test/resources/files/output/"
            + "CustomerOutputdocx.pdf");
    IConverter converter = LocalConverter.builder().baseFolder(inputdocxfile)
            .workerPool(20, 25, 2, TimeUnit.SECONDS).processTimeout(5, TimeUnit.SECONDS).build();

    Future<Boolean> conversion = converter.convert(inputdocxfile).as(DocumentType.MS_WORD).to(outputpdffile)
            .as(DocumentType.PDF).prioritizeWith(1000).schedule();
}

and I am getting this below exception. I am using the same code as mentioned in the documents4j official website.

Exception in thread "main" 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:74)
at com.documents4j.job.LocalConverter.<init>(LocalConverter.java:47)
at com.documents4j.job.LocalConverter$Builder.build(LocalConverter.java:162)
at com.apakgroup.docgen.converters.ConvertToPdf.main(ConvertToPdf.java:19)
Exception in thread "Shutdown hook: com.documents4j.job.LocalConverter" java.lang.NullPointerException
at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:95)
at com.documents4j.job.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:125)

Solution

  • I had missed few dependencies and I also had to use a newer version of commons-io. I previously used commons-io 1.3 but later I came to know that Documents4J uses commons-io 1.4 or later and when commons-io version was changed it worked. And If anyone wants to know the dependencies I used to convert docx file to pdf in java. These are the ones.

        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-api</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-local</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer-msoffice-word</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
    

    And let me remind you. This library works only on machines which have MS Office installed in it; as the library uses the application itself to convert docx to pdf. If someone is hosting this code on a server there is also a remote converter which you can use instead of the Local converter as shown here.