当前位置: 首页 > 知识库问答 >
问题:

如何修复:带有DOCX4J错误的DOCX到HTML(java.lang.NullPointerException)

南门新知
2023-03-14

我正在尝试使用DOCX4j、Java11、Spring Boot2.3.5、Ubuntu18.04.5将DOCX转换为HTML,我正在Tomcat9上运行我的.war文件。

当我运行代码时,我得到这个错误。我怎么才能修好这个?

我的代码

 WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(source));

 HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
 htmlSettings.setWmlPackage(wordMLPackage);
 htmlSettings.setImageDirPath("temp_images");
 htmlSettings.setImageTargetUri("temp_images");
 htmlSettings.setImageIncludeUUID(false);

 boolean nestLists = false;
 if (nestLists) {
     SdtWriter.registerTagHandler("HTML_ELEMENT", new SdtToListSdtTagHandler());
 } else {
     // convert numberings to plain text
     htmlSettings.getFeatures().remove(ConversionFeatures.PP_HTML_COLLECT_LISTS);
 } 

 OutputStream os = new java.io.FileOutputStream(dest);
 Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
   <dependency>
        <groupId>org.docx4j</groupId>
        <artifactId>docx4j</artifactId>
        <version>6.1.2</version>
    </dependency>

    <dependency>
        <groupId>org.docx4j</groupId>
        <artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
        <version>11.2.5</version>
    </dependency> 

    <dependency>
        <groupId>org.docx4j</groupId>
        <artifactId>docx4j-MOXy-JAXBContext</artifactId>
        <version>6.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.moxy</artifactId>
        <version>2.7.7</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.7.7</version>
    </dependency>
    <dependency>
        <groupId>fr.opensagres.xdocreport</groupId>
        <artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
        <version>2.0.2</version>
    </dependency>
    <dependency>
        <groupId>fr.opensagres.xdocreport</groupId>
        <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
        <version>2.0.2</version>
    </dependency>
    <dependency>
        <groupId>fr.opensagres.xdocreport</groupId>
        <artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
        <version>2.0.2</version>
    </dependency>
org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
        at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:106)
        at org.docx4j.Docx4J.toHTML(Docx4J.java:830)
        at com.home.converter.Xhtmlconv.startConveter(Xhtmlconv.java:141)
        at com.home.controller.myproject.shephard.GetController.getStartGenerateXhtml(GetController.java:1377)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:126)
        at org.springframework.boot.web.servlet.support.ErrorPageFilter.access$000(ErrorPageFilter.java:64)
        at org.springframework.boot.web.servlet.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:101)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
        at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:119)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException
        at org.docx4j.openpackaging.parts.WordprocessingML.ObfuscatedFontPart.deleteEmbeddedFontTempFiles(ObfuscatedFontPart.java:263)
        at org.docx4j.openpackaging.parts.WordprocessingML.FontTablePart.deleteEmbeddedFontTempFiles(FontTablePart.java:161)
        at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:90)
        ... 61 more

编辑:我试着在Tomcat9、Windows10上运行同样的战争,一切都正常。有趣...但是如何在Linux-Tomcat上解决这个问题呢?(检查了tomcat/linux,这些都是正常的,没有损坏)

共有1个答案

岳凯康
2023-03-14

看起来您的tmpFontDir为空。

https://github.com/plutext/docx4j/blob/master/docx4j-core/src/main/java/org/docx4j/openpackaging/parts/abstractfontpart.java#L66是设置它的工具。

考虑设置属性docx4j.openpackaging.parts.wordprocessingml.obfuscatedfontpart.tmpfontdir

 类似资料:
  • 我正在使用Docx4j Version3将docx转换为html。 文档包含由制表符、空格和换行符组成的空白。生成的HTML要么具有无法识别的字符,要么根本不保留空白。 谢谢你。

  • 我试图生成一个. docx从超文本标记语言字符串与docx4j在Java,其中包含Base64图像里面。目前,我能够生成Word并下载它(在一个Vaadin项目内),但图像没有正确插入。但是,如果我把超文本标记语言代码放入index.html页面,它们确实显示正确! 我使用的代码是这样的: 有没有什么特别的工作应该做,而我没有做? 编辑: 我现在可以插入图片了!用以下代码替换行下方和行上方的代码:

  • 我对docx4j样本有一些问题。我需要转换一个文件从docx在html格式和回来。我正在尝试编译ConvertInXHTMLDocument。java示例。它创建的Html文件很好,但当试图将其转换回docx时,抛出一个缺少关闭标记(META、img等)的异常。有人遇到过这个问题吗?

  • (*来自http://www.docx4java.org/forums/xhtml-import-f28/html-docx-html-inserts-a-lot-of-space-t1966.html#p6791?sid=78b64a02482926c4dbdbbafbf50d0a914将在应答时更新) 我已经创建了一个html测试文档,其内容如下: 然后,我的代码从这个html创建一个docx

  • 我需要生成一个DOCX从网页(与图像)。DOCX4J是否可以将HTML内容导出为DOCX格式? 还有其他库可以将HTML内容导出到DOCX吗?

  • 我在将HTML转换为docx时遇到了新问题,它引发了异常: 组织。xml。萨克斯。SAXS异常;行号:4;栏目号:73;实体“nbsp”已被引用,但未被声明 正如我所理解的,这是因为docx4j认为我的文件是XML,并希望将其转换为docx但XML中只有5个预定义的实体,而nbsp等实体没有在XML中定义。如何让docx4j将超文本标记语言转换为doc,而无需在doctype中声明实体nbsp?