将 Doc 导出为 PDF 会引发 Docx4JException

Exporting Doc to PDF makes a Docx4JException

我正在尝试将文档导出为 PDF,文档并不重要,因为我在处理空白文档时遇到了同样的错误。 我将 docx4j 与 gradle 一起使用,因为我没有使用 Maven 的经验,我认为这不应该成为问题。

截自 build.gradle

// docx4j
implementation("org.docx4j:docx4j-JAXB-ReferenceImpl:11.2.8")

这是我的简单 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();
    }
  }
}

所以问题是我在 运行 程序时遇到的错误。

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

我找到了适合自己的解决方案,也许以后其他人也会遇到同样的问题:D

我刚刚将 docx4j-export-fo 添加到 gradle 构建文件中,它起作用了,因为 PDF Converter 只被移到了一个子项目中。

implementation 'org.docx4j:docx4j-export-fo:11.2.8'