使用docx4j将HTML字符串转换为Docx时出现NullPointer异常

时间:2015-07-13 07:22:12

标签: docx4j

我使用docx4j将html字符串转换为docx。

以下是代码。

public function getLastGuidesSamples()
{
    return $this->_em->createQuery("
        SELECT
            document.slug,
            document.title,
            document.description,
            document.addingDate,
            document.addingAuthor,
            image.source,
            image.alternative,
            image.width,
            image.height
        FROM AppBundle:Document document
        JOIN AppBundle:Image image
        WITH image.id = document.id
        WHERE document.type = 3
        AND document.isPublished = true
        ORDER BY document.addingDate
    ")->setMaxResults(5)->getResult();
}

当我运行它时,我在以下行得到错误。

package docx4j;

import org.docx4j.convert.in.xhtml.FormattingOption;
import org.docx4j.convert.in.xhtml.XHTMLImporter;
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

public class ConvertHTMLToDoc {

    public static void main(String[] args) throws Docx4JException {

        String outputfilepath = "style-example-OUT30.rtf";

        String text = getHTMLString();

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

        XHTMLImporter xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);

/*      xHTMLImporter.setParagraphFormatting(FormattingOption.CLASS_PLUS_OTHER);
        xHTMLImporter.setRunFormatting(FormattingOption.CLASS_PLUS_OTHER);
*/
        wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(text, null));

/*      wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "Testing Title");
        wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Subtitle", "Testing Subtitle");
*/
        SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
        saver.save(outputfilepath);
    }

    private static String getHTMLString() {
        String text = "<html><head><title></title></head><body>" + "<p class=\"Title\">Testing Title</p>"
                + "<p class=\"Subtitle\">Testing Subtitle</p>" + "</body></html>";
        return text;
    }

}

以下是错误消息。

wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(text, null));

空指针异常的原因是什么?

1 个答案:

答案 0 :(得分:1)

有两种可能的方法可以解决这个问题:

不太优雅的方式(您需要在项目中创建此类。请记住,docx4j需要包com.sun.xml.internal.bind.marshaller。您无法更改):

package com.sun.xml.internal.bind.marshaller;

public abstract class NamespacePrefixMapper {

private static final String[] EMPTY_STRING = new String[0];

public abstract String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix);


    public String[] getPreDeclaredNamespaceUris() {
        return EMPTY_STRING;
    }

    public String[] getPreDeclaredNamespaceUris2() {
        return EMPTY_STRING;
    }

public String[] getContextualNamespaceDecls() {
    return EMPTY_STRING;
}
 }

更优雅的方式,但我不知道它是否适用于JBOSS 6.2,因为它适用于版本7: Docx4j and JBoss 7