I am using ITextRenderer to generate the pdf using HTML String, though its generate the pdf but give me error when the HTML String contain html entity like ° ,  ** and so on symbol which output error as
org.xml.sax.SAXParseException: The entity "deg" was referenced, but not declared.
like for example
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>
and my java code is
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();
}
even added meta tag like
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
and also some stuff like
<?xml version=\'1.0\' encoding=\'UTF-8\'?><html xmlns=\'http://www.w3.org/1999/xhtml\' lang=\'en\'><head>
Still the same error. Any help Thanks in advance.
The problem is ° is considered as a Html tag. So escape the & with & will solve your problem.
Example :
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>";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.