我正在使用ITextRenderer使用 HTML 字符串生成pdf,虽然它生成pdf但是当HTML字符串包含html实体(如& 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>
仍然是同样的错误。 任何帮助 提前谢谢。
答案 0 :(得分:1)
问题是&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>";