简体   繁体   English

转换后documents4j不保存文件

[英]documents4j not saving file after conversion

I have converted a.docx file to.pdf using documents4j however the pdf is not showing even the logs shows successful conversion.我已经使用documents4j将a.docx文件转换为.pdf,但是pdf没有显示,即使日志显示转换成功。

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?为什么 pdf 没有显示在目录中?

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添加conversion.get() 会引发以下错误

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

The File objects for your Word document and PDF document need to explicitly include the paths to their locations. Word 文档和 PDF 文档的File对象需要明确包含其位置的路径。

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):下面的示例假定两个文档使用相同的位置 - 这与baseFolder位置(转换器放置其临时文件的位置)相同:

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.因此,在上面的示例中,我从已在conversions目录中的 Word 文档开始。

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.对于 Tomcat,我不是将其安装为 Windows 服务,而是使用页面中的“64 位 Windows zip”作为独立服务安装。

I created a very basic servlet, which does nothing except run the conversion code - the same code as I show in this answer.我创建了一个非常基本的 servlet,它除了运行转换代码之外什么都不做——与我在这个答案中显示的代码相同。

I tried it with 2 different directory locations: One external to CATALINA_BASE and one the same as CATALINA_BASE.我尝试了 2 个不同的目录位置:一个在 CATALINA_BASE 外部,一个与 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.一切都按预期工作,没有问题,或者需要管理任何权限等。我的用户 ID 在 PC 上具有 Windows 管理员权限。

tomcat 9 by default, as a windows service, runs under "Local Service". tomcat 9 默认情况下,作为 windows 服务,在“本地服务”下运行。 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.请注意,即使使用包含单个字符的 simple.docx 文件,也会显示相同的堆栈跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM