documents4j not saving file after conversion

1.3k views Asked by At

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)
2

There are 2 answers

13
andrewJames On

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.

2
Mo Alex On

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.