Issues while converting DOCX file into PDF using Documents4j

4,306 views
Skip to first unread message

sabhya00...@gmail.com

unread,
Sep 7, 2015, 7:39:54 AM9/7/15
to documents4j
Hi,

The following is the simple code snippet which I am trying to use to convert a DOCX file into a PDF on my local system:

import java.io.File;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

public class DocumentToPDF {
    public static void main(String[] orange)
    {
        File wordFile = new File("C://Users//sabk//Desktop//thumbTest//office//word//one.docx")
        , target = new File("C://Users//sabk//Desktop//thumbTest//office//word//one.pdf");
        IConverter converter = LocalConverter.make();
            converter.convert(wordFile).as(DocumentType.MS_WORD)
                     .to(target).as(DocumentType.PDF)
                     .prioritizeWith(1000) // optional
                     .schedule();
        //converter.shutDown();
    }
}


which is as is picked up from http://documents4j.com/#/.
The problem is: I was getting a lot of java.lang.NoClassDefFoundError exceptions earlier, so I included all the *.jar files which I could find, from https://bintray.com/raphw/maven/documents4j#files.

Now, I am getting the following exception:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/zeroturnaround/exec/stream/slf4j/Slf4jOutputStream
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    at com.documents4j.conversion.ExternalConverterDiscovery.make(ExternalConverterDiscovery.java:29)
    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:78)
    at com.documents4j.job.LocalConverter.<init>(LocalConverter.java:51)
    at com.documents4j.job.LocalConverter$Builder.build(LocalConverter.java:166)
    at com.documents4j.job.LocalConverter.make(LocalConverter.java:71)
    at com.thumb.DocumentToPDFUsingPOI.main(DocumentToPDFUsingPOI.java:15)
Caused by: java.lang.ClassNotFoundException: org.zeroturnaround.exec.stream.slf4j.Slf4jOutputStream
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 13 more
Exception in thread "Shutdown hook: com.documents4j.job.LocalConverter" java.lang.NullPointerException
    at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:99)
    at com.documents4j.adapter.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:123)

Now, I am aware that all the *.jar files are not needed for this local implementation to work. Here are my questions:
1. Which *.jar files should be included in the classpath, along with any dependencies which they might have?
2. How to get rid of this exception?

Thanks very much in advance,
Sabhya Kaushal,
sabhya00...@gmail.com

Rafael Winterhalter

unread,
Sep 7, 2015, 8:19:54 AM9/7/15
to documents4j
It seems like you are missing the zt-exec dependency. You can use Maven to determine the dependeny tree of a module. Or you can look at the pom.xml file directly.

There are a lot of resources on how to read a POM file and on Maven online, if you do not know what this is all about.

sabhya00...@gmail.com

unread,
Sep 8, 2015, 1:50:42 AM9/8/15
to documents4j

Hello Again, Rafael,

Thanks for the quick response. I went through the pom.xml documents4j-local (as mentioned here: https://github.com/documents4j/documents4j/blob/master/documents4j-local/pom.xml), and included all the required dependencies and added them to the classpath as mentioned there. Kindly find the screenshot attached for your perusal:



But now, the following is the exception which I am getting for the same code which I posted above:

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:78)
    at com.documents4j.job.LocalConverter.<init>(LocalConverter.java:51)
    at com.documents4j.job.LocalConverter$Builder.build(LocalConverter.java:166)
    at com.documents4j.job.LocalConverter.make(LocalConverter.java:71)
    at com.thumb.DocumentToPDFUsingPOI.main(DocumentToPDFUsingPOI.java:15)
[Shutdown hook: com.documents4j.job.LocalConverter] INFO com.documents4j.adapter.ConverterAdapter - Tried to deregister shut down hook in shut down period
java.lang.IllegalStateException: Shutdown in progress
    at java.lang.ApplicationShutdownHooks.remove(Unknown Source)
    at java.lang.Runtime.removeShutdownHook(Unknown Source)
    at com.documents4j.adapter.ConverterAdapter.deregisterShutdownHook(ConverterAdapter.java:110)
    at com.documents4j.adapter.ConverterAdapter.shutDown(ConverterAdapter.java:96)
    at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:102)
    at com.documents4j.adapter.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:123)

Exception in thread "Shutdown hook: com.documents4j.job.LocalConverter" java.lang.NullPointerException
    at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:99)
    at com.documents4j.adapter.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:123)

Any idea as to why this is happening? Thanks very much once again in advance. :)

Regards,
Sabhya Kaushal
Auto Generated Inline Image 1

Rafael Winterhalter

unread,
Sep 8, 2015, 4:54:22 AM9/8/15
to documents4j
You did not register a converter for the actual conversion job. For your use-case, this is going to be the MS Word-based converter. When this converter is found on the class path, it is discovered automatically: The module is called: documents4j-transformer-msoffice-word

The machine you are running this from requires an installation of MS Word on it.
Best regards, Rafael

sabhya00...@gmail.com

unread,
Sep 8, 2015, 6:01:22 AM9/8/15
to documents4j
Hello Again, Rafael,

Now, even after including the following jars:

documents4j-transformer-msoffice-word-0.2.1.jar
documents4j-transformer-msoffice-base-0.2.1.jar


I am getting the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/documents4j/conversion/AbstractExternalConverter
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)

    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)

    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.documents4j.conversion.ExternalConverterDiscovery.tryFindClass(ExternalConverterDiscovery.java:95)
    at com.documents4j.conversion.ExternalConverterDiscovery.discover(ExternalConverterDiscovery.java:58)

    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:78)
    at com.documents4j.job.LocalConverter.<init>(LocalConverter.java:51)
    at com.documents4j.job.LocalConverter$Builder.build(LocalConverter.java:166)
    at com.documents4j.job.LocalConverter.make(LocalConverter.java:71)
    at com.thumb.DocumentToPDFUsingPOI.main(DocumentToPDFUsingPOI.java:15)
Caused by: java.lang.ClassNotFoundException: com.documents4j.conversion.AbstractExternalConverter

    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 35 more

[Shutdown hook: com.documents4j.job.LocalConverter] INFO com.documents4j.adapter.ConverterAdapter - Tried to deregister shut down hook in shut down period
java.lang.IllegalStateException: Shutdown in progress
    at java.lang.ApplicationShutdownHooks.remove(Unknown Source)
    at java.lang.Runtime.removeShutdownHook(Unknown Source)
    at com.documents4j.adapter.ConverterAdapter.deregisterShutdownHook(ConverterAdapter.java:110)
    at com.documents4j.adapter.ConverterAdapter.shutDown(ConverterAdapter.java:96)
    at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:102)
    at com.documents4j.adapter.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:123)
Exception in thread "Shutdown hook: com.documents4j.job.LocalConverter" java.lang.NullPointerException
    at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:99)
    at com.documents4j.adapter.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:123)

Any ideas as to why this might be happening?

Regards,
Sabhya Kaushal

Rafael Winterhalter

unread,
Sep 8, 2015, 6:04:24 AM9/8/15
to documents4j
Check out the POM files of those modules for their dependencies. You are missing the transformer base module.

sabhya00...@gmail.com

unread,
Sep 9, 2015, 2:45:47 AM9/9/15
to documents4j
Good Morning Rafael,

I went through the *.pom files of all the *.jars and found out the I indeed was missing some of them. I have included them in the classpath and the following is the screenshot of the same:



But even after including all of these *.jar files, I am getting the following exception:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/sabk/workspace/DocumentToPDF/lib/slf4j-simple-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/sabk/workspace/DocumentToPDF/lib/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
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:78)
    at com.documents4j.job.LocalConverter.<init>(LocalConverter.java:51)
    at com.documents4j.job.LocalConverter$Builder.build(LocalConverter.java:166)
    at com.documents4j.job.LocalConverter.make(LocalConverter.java:71)
    at com.convert.DocumentToPDF.main(DocumentToPDF.java:15)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.documents4j.conversion.ExternalConverterDiscovery.make(ExternalConverterDiscovery.java:30)
    ... 8 more
Caused by: java.lang.NoSuchFieldError: NULL_OUTPUT_STREAM
    at org.zeroturnaround.exec.ProcessExecutor.redirectOutput(ProcessExecutor.java:313)
    at org.zeroturnaround.exec.ProcessExecutor.<init>(ProcessExecutor.java:121)
    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)
    ... 13 more

[Shutdown hook: com.documents4j.job.LocalConverter] INFO com.documents4j.adapter.ConverterAdapter - Tried to deregister shut down hook in shut down period
java.lang.IllegalStateException: Shutdown in progress
    at java.lang.ApplicationShutdownHooks.remove(Unknown Source)
    at java.lang.Runtime.removeShutdownHook(Unknown Source)
    at com.documents4j.adapter.ConverterAdapter.deregisterShutdownHook(ConverterAdapter.java:110)
    at com.documents4j.adapter.ConverterAdapter.shutDown(ConverterAdapter.java:96)
    at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:102)
    at com.documents4j.adapter.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:123)
Exception in thread "Shutdown hook: com.documents4j.job.LocalConverter" java.lang.NullPointerException
    at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:99)
    at com.documents4j.adapter.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:123)

Any ideas as to what might be the issue now?

Regards,
Sabhya Kaushal
Auto Generated Inline Image 1

Rafael Winterhalter

unread,
Sep 9, 2015, 7:20:52 PM9/9/15
to sabhya00...@gmail.com, documents4j

As the stack trace suggests, you have to slf4j APIs on the class path.
For example, remove slf4j-simple to remove one.
It appears that you struggle with your general dependency management. I really suggest that you use a tool like Maven for avoiding such conflicts.
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/56928ca5-e86f-4786-9c96-af5513ddf0c1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sabhya00...@gmail.com

unread,
Sep 10, 2015, 1:44:41 AM9/10/15
to documents4j, sabhya00...@gmail.com
Thanks for the help. Much appreciated. :)

Regards,
Sabhya Kaushal

Rafael Winterhalter

unread,
Sep 10, 2015, 1:53:16 AM9/10/15
to sabhya00...@gmail.com, documents4j

You are welcome. Did this resolve your problem?

--
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.

saurab...@gmail.com

unread,
Jul 13, 2017, 3:39:27 AM7/13/17
to documents4j
Message has been deleted

saurab...@gmail.com

unread,
Jul 13, 2017, 3:48:23 AM7/13/17
to documents4j
07-13 13:05:59.344 7917-7917/com.aspose.words.examples.asposewordsexamples E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                             Process: com.aspose.words.examples.asposewordsexamples, PID: 7917
                                                                                             java.lang.IllegalStateException: Illegal auto discovery class implementation found
                                                                                                 at com.google.common.base.Preconditions.checkState(Preconditions.java:456)
                                                                                                 at com.aspose.words.examples.asposewordsexamples.conversion.ExternalConverterDiscovery.tryFindClass(ExternalConverterDiscovery.java:98)
                                                                                                 at com.aspose.words.examples.asposewordsexamples.conversion.ExternalConverterDiscovery.discover(ExternalConverterDiscovery.java:60)
                                                                                                 at com.aspose.words.examples.asposewordsexamples.conversion.ExternalConverterDiscovery.loadConfiguration(ExternalConverterDiscovery.java:87)
                                                                                                 at com.aspose.words.examples.asposewordsexamples.conversion.DefaultConversionManager.<init>(DefaultConversionManager.java:25)
                                                                                                 at com.aspose.words.examples.asposewordsexamples.job.LocalConverter.makeConversionManager(LocalConverter.java:99)
                                                                                                 at com.aspose.words.examples.asposewordsexamples.job.LocalConverter.<init>(LocalConverter.java:71)
                                                                                                 at com.aspose.words.examples.asposewordsexamples.job.LocalConverter$Builder.build(LocalConverter.java:193)
                                                                                                 at com.aspose.words.examples.asposewordsexamples.MainActivity$3.onClick(MainActivity.java:109)
                                                                                                 at android.view.View.performClick(View.java:5268)
                                                                                                 at android.view.View$PerformClick.run(View.java:21550)
                                                                                                 at android.os.Handler.handleCallback(Handler.java:822)
                                                                                                 at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                                                 at android.os.Looper.loop(Looper.java:207)
                                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5811)
                                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
                                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
plz give me solution of this error

Rafael Winterhalter

unread,
Jul 13, 2017, 3:54:03 AM7/13/17
to saurab...@gmail.com, documents4j
Are you trying to run the converter on Android? That will of course not work.

--
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+unsubscribe@googlegroups.com.

To post to this group, send email to docum...@googlegroups.com.

saurab...@gmail.com

unread,
Jul 13, 2017, 4:59:52 AM7/13/17
to documents4j, saurab...@gmail.com
yes i try for android,now what i do??
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.

Rafael Winterhalter

unread,
Jul 13, 2017, 4:36:13 PM7/13/17
to Saurabh Shah, documents4j
It will not work on Android. It requires Windows and an installation of MS Word.

To unsubscribe from this group and stop receiving emails from it, send an email to documents4j+unsubscribe@googlegroups.com.

To post to this group, send email to docum...@googlegroups.com.

saurab...@gmail.com

unread,
Jul 17, 2017, 5:00:05 AM7/17/17
to documents4j, saurab...@gmail.com
okay...tnq for help

prinkal1...@gmail.com

unread,
Jul 2, 2019, 11:08:26 AM7/2/19
to documents4j
Hi,

While convert from RTF to PDF i got below error,

Stack Trace
java.util.concurrent.ExecutionException: Could not complete conversion
at com.documents4j.job.FailedConversionFuture.get(FailedConversionFuture.java:35)
at com.documents4j.job.FailedConversionFuture.get(FailedConversionFuture.java:10)
at com.documents4j.job.AbstractFutureWrappingPriorityFuture.get(AbstractFutureWrappingPriorityFuture.java:205)
at com.documents4j.job.AbstractFutureWrappingPriorityFuture.get(AbstractFutureWrappingPriorityFuture.java:10)

So can you please help me out on it?

I also faced other error but i read your comment and based on i remove all error but after that i got above error,

Thanks,
Prinkal Mavani

Rafael Winterhalter

unread,
Jul 3, 2019, 5:36:04 PM7/3/19
to prinkal1...@gmail.com, documents4j
What does the server application say about this, are there any causes on the error?

--
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.

Sakhawat Naqvi

unread,
Aug 21, 2020, 7:09:06 AM8/21/20
to documents4j
Hi,
I am having an issue while converting the Docx file to PDF. It is running smoothly on local machines and generating pdfs in the required folder but on production server there is no exception as well and all the instructions are processed and it says the pdf has been generated in the xyz folder but when we open the folder there is no PDF file present. Please help.
Reply all
Reply to author
Forward
0 new messages