我正在使用itextrenderer使用 html 字符串来生成PDF,尽管它会生成PDF,但是当HTML字符串包含html html html Entity时,请给我错误& deg ,& nbsp ** 等符号等等,输出错误为
org.xml.sax.SAXParseException: The entity "deg" was referenced, but not declared.
例如
String myString=<html><head></head><body><div>**1L of water at 100°C is mixed with 1 L of water at 0°**</div></body></html>
我的Java代码是
StringBuffer buf = new StringBuffer();
buf.append(myString);
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new StringBufferInputStream(buf.toString()));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.getFontResolver();
renderer.layout();
java.io.OutputStream os = response.getOutputStream();
renderer.createPDF(os);
os.flush();
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
甚至添加了
的元标记 <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
还有一些东西,例如
<?xml version='1.0' encoding='UTF-8'?><html xmlns='http://www.w3.org/1999/xhtml' lang='en'><head>
仍然存在相同的错误。任何帮助预先感谢。
问题是&amp; deg; 被视为HTML标签。因此,逃脱&amp; ,使用&amp; amp; 将解决您的问题。
示例:
String myString = "<html><head></head><body><div>**1L of water at 100&deg;C is mixed with 1 L of water at 0&deg;**</div></body></html>";