【问题标题】:Exporting Doc to PDF makes a Docx4JException将 Doc 导出为 PDF 会导致 Docx4JException
【发布时间】:2021-07-09 07:27:51
【问题描述】:

我正在尝试将文档导出为 PDF,文档无关紧要,因为我在空白文档中遇到相同的错误。 我将 docx4j 与 gradle 一起使用,因为我没有使用 maven 的经验,我认为这应该不是问题。

从 build.gradle 截取

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

这是我的简单类 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
...

【问题讨论】:

    标签: java http docx4j


    【解决方案1】:

    我为自己找到了解决方案,也许其他人将来会遇到同样的问题:D

    我刚刚将 docx4j-export-fo 添加到 gradle 构建文件中,它可以正常工作,因为 PDF Converter 仅移动到子项目中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 2011-08-19
      相关资源
      最近更新 更多