远程使用document4j将DOCX转换为PDF

时间:2018-11-27 23:51:20

标签: java maven pdf documents4j

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

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

最终,我最终使用了基于云的API(收费),感觉真是令人讨厌。但是今天,我发现了document4j项目,这让我非常兴奋。

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

除了Javadocs之外,在线文档似乎非常有限,除了Javadocs之外,它对我的​​帮助不及编辑器中的intellisense。

有人可以帮我吗?

Stacktrace

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可以使用events

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我的问题与此问题类似,并且通过创建自定义HTTP请求得以解决:

Custom HttpClient for RemoteConverter of Documents4j