0

I have converted a .docx file to .pdf using documents4j however the pdf is not showing even the logs shows successful conversion.

26-Jun-2020 21:45:38.163 INFO [http-nio-80-exec-423] com.documents4j.conversion.msoffice.MicrosoftWordBridge.startUp From-Microsoft-Word-Converter was started successfully
26-Jun-2020 21:45:38.163 INFO [http-nio-80-exec-423] com.documents4j.job.LocalConverter.<init> The documents4j local converter has started successfully
26-Jun-2020 21:45:38.187 INFO [pool-50-thread-1] com.documents4j.conversion.msoffice.AbstractMicrosoftOfficeBridge.doStartConversion Requested conversion from C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\stock\batimp\RECTO_BAT20200045.docx (application/vnd.openxmlformats-officedocument.wordprocessingml.document) to C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\stock\batimp\RECTO_BAT20200045.pdf (application/pdf)
26-Jun-2020 21:45:38.440 INFO [http-nio-80-exec-423] com.documents4j.conversion.msoffice.MicrosoftWordBridge.shutDown From-Microsoft-Word-Converter was shut down successfully
26-Jun-2020 21:45:38.440 INFO [http-nio-80-exec-423] com.documents4j.job.LocalConverter.shutDown The documents4j local converter has shut down successfully

Also debugging showed the following:

LocalConversion{pending=false, cancelled=false, done=true, priority=Priority{value=500, creationTime=1593207938174}, file-system-target=C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\stock\batimp\RECTO_BAT20200045.pdf} 

Why is the pdf not being shown in the directory?

program snippet

File wordFile = new File(FILE_NAMEX), target = new File(outputFile1);
IConverter converter = LocalConverter.builder().baseFolder(new File(path+"batimp"))
.workerPool(20, 25, 2, TimeUnit.SECONDS)
.processTimeout(5, TimeUnit.SECONDS).build();
Future<Boolean> conversion = converter.convert(wordFile).as(DocumentType.DOCX).to(target).as(DocumentType.PDF)
.schedule();
converter.shutDown();

Adding conversion.get() throws the following error

> 29-Jun-2020 08:52:21.475 INFO [http-nio-80-exec-1]
> com.documents4j.conversion.msoffice.MicrosoftWordBridge.startUp
> From-Microsoft-Word-Converter was started successfully 29-Jun-2020
> 08:52:21.475 INFO [http-nio-80-exec-1]
> com.documents4j.job.LocalConverter.<init> The documents4j local
> converter has started successfully 29-Jun-2020 08:52:21.511 INFO
> [pool-1-thread-1]
> com.documents4j.conversion.msoffice.AbstractMicrosoftOfficeBridge.doStartConversion
> Requested conversion from C:\Program Files\Apache Software
> Foundation\Tomcat
> 9.0\webapps\stock\batimp\FICHE_TECHNIQUE_BAT20200045.docx (application/vnd.openxmlformats-officedocument.wordprocessingml.document)
> to C:\Program Files\Apache Software Foundation\Tomcat
> 9.0\webapps\stock\batimp\FICHE_TECHNIQUE_BAT20200045.pdf (application/pdf) 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)
>         at downloadbatimp.doGet(downloadbatimp.java:145)
>         at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
>         at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
>         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
>         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
>         at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
>         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
>         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
>         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
>         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
>         at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
>         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
>         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
>         at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
>         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
>         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
>         at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
>         at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
>         at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
>         at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
>         at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
>         at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
>         at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
>         at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
>         at java.base/java.lang.Thread.run(Thread.java:832) Caused by: com.documents4j.throwables.ConverterAccessException: The converter
> seems to be shut down
>         at com.documents4j.util.Reaction$ConverterAccessExceptionBuilder.make(Reaction.java:117)
>         at com.documents4j.util.Reaction$ExceptionalReaction.apply(Reaction.java:75)
>         at com.documents4j.conversion.ExternalConverterScriptResult.resolve(ExternalConverterScriptResult.java:70)
>         at com.documents4j.conversion.ProcessFutureWrapper.evaluateExitValue(ProcessFutureWrapper.java:48)
>         at com.documents4j.conversion.ProcessFutureWrapper.get(ProcessFutureWrapper.java:36)
>         at com.documents4j.conversion.ProcessFutureWrapper.get(ProcessFutureWrapper.java:11)
>         at com.documents4j.job.AbstractFutureWrappingPriorityFuture.run(AbstractFutureWrappingPriorityFuture.java:78)
>         at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
>         at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
Mo Alex
  • 14
  • 5

2 Answers2

0

The File objects for your Word document and PDF document need to explicitly include the paths to their locations.

The below sample assumes that the same location is used for both documents - and this is the same as the baseFolder location (where the converter places its temporary files):

final String dir = "C:/tmp/conversions/";

File wordFile = new File(dir + "sample.docx");
File pdfFile = new File(dir + "sample.pdf");
        
IConverter converter = LocalConverter.builder()
        .baseFolder(new File(dir))
        .workerPool(20, 25, 2, TimeUnit.SECONDS)
        .processTimeout(5, TimeUnit.SECONDS)
        .build();
        
Future<Boolean> conversion = converter
        .convert(wordFile).as(DocumentType.DOCX)
        .to(pdfFile).as(DocumentType.PDF)
        .schedule();
        
converter.shutDown();

So, in the above example, I start with my Word document already in the conversions directory.

Just for the record, I use the following dependencies:

<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-api</artifactId>
    <version>1.1.3</version>
</dependency>
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-local</artifactId>
    <version>1.1.3</version>
</dependency>
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-transformer-msoffice-word</artifactId>
    <version>1.1.3</version>
</dependency>

UPDATE:

For Tomcat, I installed it not as a Windows service, but as a standalone using the "64 bit Windows zip", from this page.

I created a very basic servlet, which does nothing except run the conversion code - the same code as I show in this answer.

I tried it with 2 different directory locations: One external to CATALINA_BASE and one the same as CATALINA_BASE.

Everything worked as expected, with no issues, or need to manage any permissions, etc. My user ID has Windows admin privileges on the PC.

andrewJames
  • 19,570
  • 8
  • 19
  • 51
  • My program runs successfully while executing it on cmd but fails with tomcat 9. If i add conversion.get() the message i get converter is shutdown. Why am i not having the issue while running the same code on cmd? – Mo Alex Jun 27 '20 at 23:17
  • Please add all the relevant new information to the question. For example, the Tomcat config and any Catalina error messages. Also, is Tomcat running locally? Thank you @moalex – andrewJames Jun 27 '20 at 23:24
  • @MoAlex - In case it helps: I ran the above code inside an instance of Tomcat 9 without any issues. This was on my local PC (where MS-Word is installed). It would not work, of course, if Tomcat was running on another server, without access to MS-Word. – andrewJames Jun 28 '20 at 16:39
  • Tomcat and ms word runs on same server. Still cannot figure out why should it work without any issue using cmd but fails on tomcat – Mo Alex Jun 28 '20 at 21:03
  • Yes - that is odd. Without more information (e.g. config and logs), I don't think it will be easy to help you, unfortunately. – andrewJames Jun 28 '20 at 21:17
  • I have updated the question to include the stack trace – Mo Alex Jun 29 '20 at 08:54
  • I believe it is an access issue. are there any policies or settings that should be enabled to let tomcat run MS Word? – Mo Alex Jun 29 '20 at 15:41
  • No explicit policies or settings were needed in my set-up, no. I am using Windows 10, with the latest version of Word installed locally. Are you writing the PDF file to an accessible directory (and is that directory inside or outside of the Tomcat installation (CATALINA_HOME) directory)? I do have full Windows admin access on my PC, by the way. – andrewJames Jun 29 '20 at 15:48
  • Yes sure. Full admin access and the folder within catalina home – Mo Alex Jun 29 '20 at 16:29
  • Also i am not ablee to run processbuilder or runtime exec. My setup is tomcat 9 with jdk 14 and windows 10 – Mo Alex Jun 29 '20 at 16:36
  • I am still unable to recreate your issue. I think it makes sense for me to delete my answer, since it is not relevant to your issue. – andrewJames Jul 01 '20 at 19:13
  • I have reproduced my issue on tomcat 7 and 9. Thus i still cannot tell what the issue is. Can you advise how didnyou start tomcat? As windows service? – Mo Alex Jul 01 '20 at 19:59
  • I added some notes to my answer, here. – andrewJames Jul 01 '20 at 20:31
0

tomcat 9 by default, as a windows service, runs under "Local Service". changing the log on to local system account, the mentioned error vanished.

however, now i am stuck with another error:

    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)
        at downloadbatimp.doGet(downloadbatimp.java:165)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: com.documents4j.throwables.ConversionInputException: The input file seems to be corrupt
        at com.documents4j.util.Reaction$ConversionInputExceptionBuilder.make(Reaction.java:159)
        at com.documents4j.util.Reaction$ExceptionalReaction.apply(Reaction.java:75)
        at com.documents4j.conversion.ExternalConverterScriptResult.resolve(ExternalConverterScriptResult.java:70)
        at com.documents4j.conversion.ProcessFutureWrapper.evaluateExitValue(ProcessFutureWrapper.java:48)
        at com.documents4j.conversion.ProcessFutureWrapper.get(ProcessFutureWrapper.java:36)
        at com.documents4j.conversion.ProcessFutureWrapper.get(ProcessFutureWrapper.java:11)
        at com.documents4j.job.AbstractFutureWrappingPriorityFuture.run(AbstractFutureWrappingPriorityFuture.java:78)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)

Again, i believe this has to do with access rights that i am not able to figure out, does anyone know why this message shows? note that even with a simple .docx file containing a single character, the same stack trace shows.

Mo Alex
  • 14
  • 5
  • You should probably post this as a question, not as an answer - either as an edit to your existing question, or as a new question. But first, take a look [here](https://stackoverflow.com/questions/36918931/conversioninputexception-on-a-complex-web-application) - and note the comment: _giving access right as the domain administrator not the machine administrator solved the problem_. Maybe that will help. – andrewJames Jul 01 '20 at 19:19
  • Hi andrew, i have posted it as an answer because it solved a partial issue. I tried the access right that you mentioned with no luck. – Mo Alex Jul 01 '20 at 19:58