DOCX to PDF conversion java using document4j remotely

喜欢而已 提交于 2020-01-25 09:18:24

问题


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

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.

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.

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.

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.

Can anybody help me out with this?

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>

....

Relevant code:

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

Edit: 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.


回答1:


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.




回答2:


My problem was similar to this issue and was solved by creating a custom HTTP request:

Custom HttpClient for RemoteConverter of Documents4j



来源:https://stackoverflow.com/questions/53510016/docx-to-pdf-conversion-java-using-document4j-remotely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!