I am trying to export a Doc to PDF, the Document doesn't matter because i get the same error with a blank document. I'm using docx4j with gradle because i have no experience with maven and i don't think this should be a problem.
Snipped from build.gradle
// docx4j
implementation("org.docx4j:docx4j-JAXB-ReferenceImpl:11.2.8")
And this is my simple Class OpenDoc
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import org.docx4j.Docx4J;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
public class OpenDoc
{
public static void main(final String[] args)
{
final String filePath = "AnyFile.docx";
try
{
final WordprocessingMLPackage word = Docx4J.load(new File(filePath));
final MainDocumentPart doc = word.getMainDocumentPart();
final OutputStream outputSteam = new OutputStream()
{
@Override
public void write(final int arg0) throws IOException
{
// TODO kase: Auto-generated method stub
}
};
Docx4J.toPDF(word, outputSteam);
}
catch (final Docx4JException e)
{
e.printStackTrace();
}
}
}
So the problem is the error I get when i run the program.
10:45:47.557 [main] DEBUG org.docx4j.openpackaging.io3.Save - ...Done!
10:45:47.561 [main] DEBUG org.docx4j.services.client.ConverterHttp - starting, with endpointURL: http://localhost:9016/v1/00000000-0000-0000-0000-000000000000/convert
10:45:47.807 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: default
10:45:47.815 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
10:45:47.816 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://localhost:9016][total available: 0; route allocated: 0 of 2; total allocated: 0 of 20]
10:45:47.832 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://localhost:9016][total available: 0; route allocated: 1 of 2; total allocated: 1 of 20]
10:45:47.833 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Opening connection {}->http://localhost:9016
10:45:47.842 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connecting to localhost/127.0.0.1:9016
10:45:49.891 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connect to localhost/127.0.0.1:9016 timed out. Connection will be retried using another IP address
10:45:49.891 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connecting to localhost/0:0:0:0:0:0:0:1:9016
10:45:51.936 [main] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-0: Shutdown connection
10:45:51.936 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Connection discarded
10:45:51.936 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://localhost:9016][total available: 0; route allocated: 0 of 2; total allocated: 0 of 20]
10:45:51.937 [main] ERROR org.docx4j.services.client.ConverterHttp -
Looks like your endpoint URL 'http://localhost:9016/v1/00000000-0000-0000-0000-000000000000/convert' is wrong
10:45:51.937 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection manager is shutting down
10:45:51.937 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection manager shut down
org.docx4j.openpackaging.exceptions.Docx4JException: This behaviour may be Windows client OS specific; please look in the server logs or try a Linux client
at org.docx4j.Docx4J.toPDF(Docx4J.java:764)
at docx4j.OpenDoc.main(OpenDoc.java:34)
Caused by: org.docx4j.services.client.ConversionException: This behaviour may be Windows client OS specific; please look in the server logs or try a Linux client
at org.docx4j.services.client.ConverterHttp.execute(ConverterHttp.java:248)
at org.docx4j.services.client.ConverterHttp.convert(ConverterHttp.java:190)
at org.docx4j.Docx4J.toPDF(Docx4J.java:761)
... 1 more
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:9016 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
...
I found the solution for myself, maybe someone else will the same problem in the future :D
I just added docx4j-export-fo to the gradle build file, and it works because PDF Converter was only moved to a sub-project.
implementation 'org.docx4j:docx4j-export-fo:11.2.8'