Docx4j将html转换为docx

时间:2017-09-24 15:21:15

标签: java docx4j

当我将HTML转换为docx时,我遇到了新问题,它会引发异常:

  

org.xml.sax.SAXParseException; lineNumber:4; columnNumber:73;实体" nbsp"被引用,但未声明

据我所知,这是因为docx4j认为我的文件是XML并希望将其转换为docx,但XML中只有5个预定义实体,并且这些实体没有在XML中定义。如何在不声明doctype中的实体的情况下,将docx4j转换为HTML转换为doc?

docx4j的工作不正确还是它的限制?

这是我的代码:

package ru.simplexsoftware.constructorOfDocuments.web.rest;
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import ru.simplexsoftware.constructorOfDocuments.dao.TemplateDao;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;


public class DocxFileDownloadServlet implements HttpRequestHandler {

@Autowired
TemplateDao templateDao;
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String parameter = request.getParameter("documentId");

    Long documentId = Long.parseLong(parameter);

    WordprocessingMLPackage wordMLPackage = null;
    try {
        wordMLPackage = WordprocessingMLPackage.createPackage();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    }

    NumberingDefinitionsPart ndp = null;
    try {
        ndp = new NumberingDefinitionsPart();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    }
    try {
        wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    }
    try {
        ndp.unmarshalDefaultNumbering();
    } catch (JAXBException e) {
        e.printStackTrace();
    }

    XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    xHTMLImporter.setHyperlinkStyle("Hyperlink");

    String htmlString=templateDao.get(documentId).html;
    htmlString = htmlString.replaceAll("<br>","<br/>");
    InputStream stream = new ByteArrayInputStream(htmlString.getBytes(StandardCharsets.UTF_8.name()));
    // Convert the XHTML, and add it into the empty docx we made
    try {
        wordMLPackage.getMainDocumentPart().getContent().addAll(
                xHTMLImporter.convert(htmlString, null));
    } catch (Docx4JException e) {
        e.printStackTrace();
    }

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    try {
        wordMLPackage.save(outputStream);
    } catch (Docx4JException e) {
        e.printStackTrace();
    }


    response.setContentType("application/msword");
    response.getOutputStream().write(outputStream.toString().getBytes("UTF-8"));
    response.flushBuffer();

}
}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用 AltChunkType 类型将HTML字符串插入docx段落

wordMLPackage.getMainDocumentPart().addAltChunk(AltChunkType.Xhtml, htmlString .getBytes());