ObfuscatedFontPart.deleteEmbeddedFontTempFiles 中的 docx4j NullPointerException

docx4j NullPointerException in ObfuscatedFontPart.deleteEmbeddedFontTempFiles

我在我的软件中使用 docx4j 将 docx 文件转换为 html。我在基于 windows 的系统中进行开发,我的产品是 AWS 中的 Ubuntu Linux 14.04。在 Windows 中一切正常,但是当涉及到 Ubuntu 时,它抛出如下异常。

2015-11-19 07:33:36 ERROR AbstractExporter:108 - Exception exporting package 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:91) at org.docx4j.Docx4J.toHTML(Docx4J.java:511) at com.testbudha.document.impl.WordDocumentExtractor.toHtml(WordDocumentExtractor.java:31) at com.testbudha.document.impl.DocXExtractor.getText(DocXExtractor.java:31) at com.testbudha.document.impl.DocumentServiceImpl.getFileText(DocumentServiceImpl.java:18) at com.testbudha.server.controller.StudentHomeController.getResumeText(StudentHomeController.java:120)

我知道这是一个与字体相关的问题,我尝试安装

ttf-mscorefonts-installer

但它并没有改变任何东西我也尝试使用这个脚本安装 vista 字体。然后我也遇到了同样的异常

Vista fonts installer

protected String toHtml(WordprocessingMLPackage wordMLPackage, String path, HttpServletRequest request)
            throws Docx4JException {
        try {

            HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
            htmlSettings.setImageDirPath(path + "/_files");
            htmlSettings.setImageTargetUri(request.getContextPath() + "/getfile/");
            htmlSettings.setWmlPackage(wordMLPackage);

            OutputStream os = new ByteArrayOutputStream();
            Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
            Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
            String outputHtml = ((ByteArrayOutputStream) os).toString().replaceAll("Â", "\n").replaceAll(" – ", "-")
                    .replaceAll("(?s)â€.\s*", "").replaceAll("\[(.*?)\]","").replaceAll("#\?", "")
                    .replaceAll("\s*(?i)HYPERLINK\s\S\s*(\"([^\"]*\")|'[^']*'|([^'\">\s]+))+\"", "()")
                    .replaceAll("(\([^)]*\))\s?(\w+)", "");
            return outputHtml;
        } finally {
            if (wordMLPackage.getMainDocumentPart().getFontTablePart() != null) {
                wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
            }
        }

抛出异常
Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/ObfuscatedFontPart.java#L263

tmpFontDir 似乎是 null,可能是因为 System.getProperty("user.home") returns null

因此将 Docx4j.properties 文件中的 属性 "docx4j.openpackaging.parts.WordprocessingML.ObfuscatedFontPart.tmpFontDir" 添加到您的类路径中。