documents4j IllegalStateExeption:在 Java 中将 docx 文件转换为 pdf 时

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

我试图在 Java 中将 docx 文件转换为 pdf,但出现此错误:

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

这是我的代码:

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。

根据堆栈跟踪,问题是

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 是番石榴 class。因此,您的 Guava 依赖项似乎有问题。请确保您的 class 路径上只有一个 Guava 实例,并且该版本与 documents4j 兼容。