javapdf-generationdocuments4jdocx-to-pdf-conversion

documents4j IllegalStateExeption: while converting a docx file to pdf in Java


I am trying to convert a docx file to pdf in Java, but I am getting this error:

Exception in thread "main" java.lang.IllegalStateException: class com.documents4j.conversion.msoffice.MicrosoftWordBridge could not be created by a (File, long, TimeUnit) constructor
 at com.documents4j.conversion.ExternalConverterDiscovery.make(ExternalConverterDiscovery.java:33)
 at com.documents4j.conversion.ExternalConverterDiscovery.makeAll(ExternalConverterDiscovery.java:43)
 at com.documents4j.conversion.ExternalConverterDiscovery.loadConfiguration(ExternalConverterDiscovery.java:86)
 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 docxCreator.pdfConvert(docxCreator.java:243)
 at docxCreator.contractCreator(docxCreator.java:57)
 at APIConnector.main(APIConnector.java:59)
Caused by: java.lang.reflect.InvocationTargetException
 at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
 at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
 at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
 at com.documents4j.conversion.ExternalConverterDiscovery.make(ExternalConverterDiscovery.java:31)
 ... 9 more
Caused by: java.lang.NoSuchMethodError: 'void com.google.common.base.Preconditions.checkArgument(boolean, java.lang.String, java.lang.Object, java.lang.Object)'
 at com.google.common.io.Resources.getResource(Resources.java:207)
 at com.documents4j.conversion.msoffice.MicrosoftWordScript.materializeIn(MicrosoftWordScript.java:53)
 at com.documents4j.conversion.msoffice.AbstractMicrosoftOfficeBridge.<init>(AbstractMicrosoftOfficeBridge.java:30)
 at com.documents4j.conversion.msoffice.MicrosoftWordBridge.<init>(MicrosoftWordBridge.java:40)
 ... 15 more
Exception in thread "Shutdown hook: com.documents4j.job.LocalConverter" java.lang.NullPointerException: Cannot invoke "java.util.concurrent.ExecutorService.shutdown()" because "this.executorService" is null
 at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:100)
 at com.documents4j.job.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:134)
23:04:00.887 [Shutdown hook: com.documents4j.job.LocalConverter] INFO com.documents4j.job.ConverterAdapter - Tried to deregister shut down hook in shut down periodjava.lang.IllegalStateException: Shutdown in progress
 at java.base/java.lang.ApplicationShutdownHooks.remove(ApplicationShutdownHooks.java:82)
 at java.base/java.lang.Runtime.removeShutdownHook(Runtime.java:245)
 at com.documents4j.job.ConverterAdapter.deregisterShutdownHook(ConverterAdapter.java:121)
 at com.documents4j.job.ConverterAdapter.cleanUp(ConverterAdapter.java:107)
 at com.documents4j.job.ConverterAdapter.shutDown(ConverterAdapter.java:98)
 at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:109)
 at com.documents4j.job.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:134)Process finished with exit code 1

This is my Code:

public static void pdfConvert(String docpath, String pdfpath, String resourcesDir) throws IOException {

    FileInputStream inputStream = null;
    try (OutputStream outputStream = new FileOutputStream(new File(pdfpath))) {
        IConverter converter = LocalConverter.builder().build();
        converter
                .convert(new File(docpath)).as(DocumentType.DOCX)
                .to(outputStream).as(DocumentType.PDF)
                .prioritizeWith(1000).schedule();
        inputStream = new FileInputStream(pdfpath);

    } catch (Exception e) {
        throw e;
    }
}

Ms Word is installed on my system.


Solution

  • According to the stack trace the issue is

    Caused by: java.lang.NoSuchMethodError: 'void com.google.common.base.Preconditions.checkArgument(boolean, java.lang.String, java.lang.Object, java.lang.Object)'

    com.google.common.base.Preconditions is a Guava class. Thus, it looks like you have an issue with your Guava dependency. Please make sure that you have only one Guava instance on your class path and that that version is compatible with documents4j.