简体   繁体   中英

ITextRenderer is not accepting the special symbols of html

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 &deg , &nbsp** 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&deg;C is mixed with 1 L of water at 0&deg;**</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 &deg; is considered as a Html tag. So escape the & with &amp; will solve your problem.

Example :

 String myString = "<html><head></head><body><div>**1L of water at 100&amp;deg;C is mixed with 1 L of water at 0&amp;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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM