Search code examples
javaubuntudocx4j

docx4j NullPointerException in ObfuscatedFontPart.deleteEmbeddedFontTempFiles


I am using docx4j to convert a docx file to html in my software. I am developing in a windows based system and my production is a Ubuntu Linux 14.04 in AWS. Everything works well in Windows, But when it comes to Ubuntu, it is throwing me an exception as follows.

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)

I know it's a font related issue and I tried installing

ttf-mscorefonts-installer

But it doesn't change anything I also tried installing the vista fonts by using this script. Then also I am getting the same exception

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]+))+\"", "($1)")
                    .replaceAll("(\\([^)]*\\))\\s?(\\w+)", "$2$1");
            return outputHtml;
        } finally {
            if (wordMLPackage.getMainDocumentPart().getFontTablePart() != null) {
                wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
            }
        }

it is throwing exception at

Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

Solution

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

    tmpFontDir seems to be null, probably because System.getProperty("user.home") returns null

    so set the property "docx4j.openpackaging.parts.WordprocessingML.ObfuscatedFontPart.tmpFontDir" in Docx4j.properties file added to your classpath.