Documents4j exception when converting docx to pdf

3,551 views
Skip to first unread message

speedp...@gmail.com

unread,
Jan 11, 2016, 11:35:58 AM1/11/16
to documents4j
Hi I am using the following code to convert DOCX to PDF.
public class ConvertToPdf {

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

        File outputpdffile = new File(System.getProperty("user.dir") + "/src/test/resources/files/output/"
                + "CustomerOutputdocx.pdf");

        IConverter converter = LocalConverter.builder().baseFolder(new File(System.getProperty("java.io.tmpdir")))
                .workerPool(20, 25, 2, TimeUnit.SECONDS).processTimeout(5, TimeUnit.SECONDS).build();

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

    }
}

And I have imported these dependencies in  my POM file.

<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-util-conversion</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-util-all</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.0</version>
</dependency>

 I end up with this exception.

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:32)
at com.documents4j.conversion.ExternalConverterDiscovery.makeAll(ExternalConverterDiscovery.java:42)
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:21)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.documents4j.conversion.ExternalConverterDiscovery.make(ExternalConverterDiscovery.java:30)
... 7 more
Caused by: java.lang.NoSuchFieldError: NULL_OUTPUT_STREAM
at org.zeroturnaround.exec.ProcessExecutor.redirectOutput(ProcessExecutor.java:449)
at org.zeroturnaround.exec.ProcessExecutor.<init>(ProcessExecutor.java:159)
at com.documents4j.conversion.AbstractExternalConverter.makePresetProcessExecutor(AbstractExternalConverter.java:43)
at com.documents4j.conversion.AbstractExternalConverter.runNoArgumentScript(AbstractExternalConverter.java:59)
at com.documents4j.conversion.msoffice.AbstractMicrosoftOfficeBridge.runNoArgumentScript(AbstractMicrosoftOfficeBridge.java:51)
at com.documents4j.conversion.msoffice.AbstractMicrosoftOfficeBridge.tryStart(AbstractMicrosoftOfficeBridge.java:34)
at com.documents4j.conversion.msoffice.MicrosoftWordBridge.startUp(MicrosoftWordBridge.java:42)
at com.documents4j.conversion.msoffice.MicrosoftWordBridge.<init>(MicrosoftWordBridge.java:37)
... 12 more
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)

If I do not use the below dependency.

<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.0</version>
</dependency> 

I end up with this exception.
 

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:21)
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)
 
How do I solve this error?

Rafael Winterhalter

unread,
Jan 11, 2016, 2:12:43 PM1/11/16
to speedp...@gmail.com, documents4j
Seems to me like a class-path problem. documents4j uses zt-exec which itself uses commons-io 1.4.

You probably have a newer version of commons IO on the class path as the NULL_OUTPUT_STREAM that zt-exec expects is missing.

Can you confirm this?
Cheers, Rafael

--
You received this message because you are subscribed to the Google Groups "documents4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to documents4j...@googlegroups.com.
To post to this group, send email to docum...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/documents4j/b7f89446-2a10-47fe-a6e4-caaee5883329%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Deepak Sunanda Prabhakar

unread,
Jan 12, 2016, 3:27:18 AM1/12/16
to documents4j, speedp...@gmail.com
It did work when I used commons-io 1.4. My application was using commons-io 1.3 as it was a version used by one of my other libraries. Thank you very much it solved all my issues.

Cheers,
Deepak

Deepak Sunanda Prabhakar

unread,
Jan 12, 2016, 7:18:56 AM1/12/16
to documents4j, speedp...@gmail.com
Hi, Does the library need MS Office word to be installed in the machine I am running the converter code?

Rafael Winterhalter

unread,
Jan 12, 2016, 7:24:50 AM1/12/16
to Deepak Sunanda Prabhakar, documents4j
Yes, this is described in detail by the documentation. The converting machine requires MS Word which is why there also is a remote server that implements the API.

--
You received this message because you are subscribed to the Google Groups "documents4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to documents4j...@googlegroups.com.
To post to this group, send email to docum...@googlegroups.com.

Deepak Sunanda Prabhakar

unread,
Jan 12, 2016, 7:39:12 AM1/12/16
to documents4j, speedp...@gmail.com
How can I access the remote api? Sorry if I am asking dumb questions I am new to Java and your library as well. I and still learning Java. I use maven build automation tool.

Rafael Winterhalter

unread,
Jan 12, 2016, 7:48:39 AM1/12/16
to Deepak Sunanda Prabhakar, documents4j
Please refer to the documentation at documents4j.com - You need to run a server on a machine that has MS Word installed. Then you can use the RemoteConverter package to connect to this server for conversion.

2016-01-12 13:39 GMT+01:00 Deepak Sunanda Prabhakar <speedp...@gmail.com>:
How can I access the remote api? Sorry if I am asking dumb questions I am new to Java and your library as well. I and still learning Java. I use maven build automation tool.

--
You received this message because you are subscribed to the Google Groups "documents4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to documents4j...@googlegroups.com.
To post to this group, send email to docum...@googlegroups.com.

pj89...@gmail.com

unread,
Jun 2, 2020, 8:51:49 AM6/2/20
to documents4j
Hi, I am also facing same problem, though i am using all correct versions like commons-io 1.4 and zt-exec updated new version.
could you please tell where am i missing?
To unsubscribe from this group and stop receiving emails from it, send an email to docum...@googlegroups.com.

Rafael Winterhalter

unread,
Jun 4, 2020, 3:52:18 PM6/4/20
to pj89...@gmail.com, documents4j
As said: you require an actual installation of MS Word.

To unsubscribe from this group and stop receiving emails from it, send an email to documents4j...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/documents4j/c6ba4a55-bf40-478a-b665-17b948bd2aa3%40googlegroups.com.

Ken Liu

unread,
Mar 20, 2023, 4:08:41 AM3/20/23
to documents4j
Hello, I also encounter the same problem. 
But I'm using newer versions of commons-io (2.7) and zt-exec (1.12). 
Is it because my computer does not have MS Word installed?
Rafael Winterhalter 在 2020年6月5日 星期五凌晨3:52:18 [UTC+8] 的信中寫道:

Rafael Winterhalter

unread,
Mar 20, 2023, 5:08:20 AM3/20/23
to Ken Liu, documents4j
Without Office, it can surely not work.

Reply all
Reply to author
Forward
0 new messages