远程使用document4j将DOCX转换为PDF [英] DOCX to PDF conversion java using document4j remotely

查看:895
本文介绍了远程使用document4j将DOCX转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用Java将高级Word文档(.DOCX)转换为PDF.

I am currently trying to convert an advanced word document (.DOCX) to PDF using java.

为了生成Word文档,我使用的是apache poi@3.15.他们内置了PDF转换器,尽管它破坏了文档格式.

For the generation of word documents I am using apache poi@3.15. They have a PDF converter build in, although it kind of ruins the document format.

最终,我最终使用了基于云的API(收费),感觉真是让人不知所措.但是今天,我发现了document4j项目,这让我非常兴奋.

I eventually ended up using a cloud based API (paid), which felt like a bummer. Today though, I found the document4j project and it got me very excited.

我已购买了运行Windows的VPS,其唯一目的是将.docx文件转换为.pdf文件,并且可以访问. document4j/local-demo也可以正常工作,并且能够很好地将我的.docx文件转换为.pdf.

I have purchased a VPS which runs windows for the sole purpose of converting .docx files to .pdf files and it is reachable. The document4j/local-demo is working fine as well and is capable of converting my .docx files to .pdf very nicely.

不过,除了Javadocs之外,在线文档似乎非常有限,但Javadocs却比我的编辑器中的intellisense帮不上忙.

There seems to be very limited documentation online though, except for the Javadocs, which don't help me out anymore than the intellisense in my editor does.

有人可以帮我吗?

2018-11-28 00:37:21.577  INFO 701 --- [o-8081-exec-477] com.documents4j.job.RemoteConverter      : The documents4j remote converter has started successfully (URI: http://xxx.xxx.xxx.xxx:1337)
Exception in thread "pool-3-thread-1" java.lang.NoSuchMethodError: org.glassfish.jersey.internal.util.PropertiesHelper.getValue(Ljava/util/Map;Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Object;
        at org.glassfish.jersey.apache.connector.ApacheConnector.getSslContext(ApacheConnector.java:324)
        at org.glassfish.jersey.apache.connector.ApacheConnector.<init>(ApacheConnector.java:240)
        at org.glassfish.jersey.apache.connector.ApacheConnectorProvider.getConnector(ApacheConnectorProvider.java:110)
        at org.glassfish.jersey.client.ClientConfig$State.initRuntime(ClientConfig.java:425)
        at org.glassfish.jersey.client.ClientConfig$State.access$000(ClientConfig.java:90)
        at org.glassfish.jersey.client.ClientConfig$State$3.get(ClientConfig.java:122)
        at org.glassfish.jersey.client.ClientConfig$State$3.get(ClientConfig.java:119)
        at org.glassfish.jersey.internal.util.collection.Values$LazyValueImpl.get(Values.java:340)
        at org.glassfish.jersey.client.ClientConfig.getRuntime(ClientConfig.java:733)
        at org.glassfish.jersey.client.ClientRequest.getConfiguration(ClientRequest.java:286)
        at org.glassfish.jersey.client.JerseyInvocation.validateHttpMethodAndEntity(JerseyInvocation.java:135)
        at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:105)
        at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:101)
        at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:92)
        at org.glassfish.jersey.client.JerseyInvocation$AsyncInvoker.method(JerseyInvocation.java:642)
        at org.glassfish.jersey.client.JerseyInvocation$AsyncInvoker.post(JerseyInvocation.java:520)
        at com.documents4j.job.RemoteFutureWrappingPriorityFuture.startConversion(RemoteFutureWrappingPriorityFuture.java:61)
        at com.documents4j.job.RemoteFutureWrappingPriorityFuture.startConversion(RemoteFutureWrappingPriorityFuture.java:15)
        at com.documents4j.job.AbstractFutureWrappingPriorityFuture.run(AbstractFutureWrappingPriorityFuture.java:70)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)

pom.xml

....

        <!-- https://mvnrepository.com/artifact/com.documents4j/documents4j-client -->
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-client</artifactId>
            <version>1.0.3</version>
        </dependency>

....

相关代码:

IConverter converter = RemoteConverter.builder()
                    .baseFolder(null)
                    .workerPool(20, 25, 2, TimeUnit.SECONDS)
                    .requestTimeout(10, TimeUnit.SECONDS)
                    .baseUri("http://xxx.xxx.xxx.xxx:1337")
                    .build();

                File pdfFile = File.createTempFile(fileName, ".pdf");
                pdfFile.deleteOnExit();

                converter.convert(data).as(DocumentType.DOCX).to(pdfFile).as(DocumentType.PDF).execute();

不幸的是,使用java -jar ./documents4j-client-standalone-1.0.3-shaded.jar http://xxx.xxx.xxx.xxx:1337可以正常工作,尽管不幸的是不在Java代码中.

using java -jar ./documents4j-client-standalone-1.0.3-shaded.jar http://xxx.xxx.xxx.xxx:1337 works, though unfortunately not in the java code itself.

推荐答案

这是一个明显的依赖版本冲突.检查您使用的是哪个版本的Glassfish以及取决于哪个版本的Glassfish文档.运行 mvndependency:tree 可以帮助您解决这个问题.

This is an obvious dependency version conflict. Check what version of Glassfish you are using and what version of Glassfish documents4j depends on. Run mvn dependency:tree to help you figure this out.

这篇关于远程使用document4j将DOCX转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆