I have new problem when I convert HTML to docx it throws exception:
org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 73; The entity "nbsp" was referenced, but not declared
As I understood, it is because docx4j thinks that my file is XML and wants to convert it to docx but there are only 5 predefined entities in XML and such entities as nbsp are not defined in XML. How can I make docx4j convert HTML to doc, without declaring the entity nbsp in the doctype?
Is it incorrect work of docx4j or it's limitation?
Here is my code:
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();
}
}