UTF-8 encoding for HTML

By: Chris Green - c_g12
UTF-8 encoding for HTML
2003-06-09 11:46
Hi Teodor,

I've been using JasperReports to generate reports in PDF, HTML, and CSV. I need to be able to display international characters - basically every character in the Unicode spec. I noticed that when I generated HTML reports, some characters were displayed correctly, but some weren't.

I took a look at the JRStringUtil.xmlEncode() method that you use to encode special characters. I found that the characters you encode using this method were the ones that were NOT appearing correctly on the exported HTML report. I needed to change my browser's encoding to Western European in order for them to show up correctly; because the exported report was declared to be UTF-8 encoded, this was what the browser was set to by default.

I then changed the xmlEncode() method to simply encode the string to UTF-8, and this worked nicely - the characters in the exported report appeared correctly.

Since the exported reports are declared to be UTF-8 encoded, why did you use HTML entities rather than UTF-8 encoding? Can I add my change to the JasperReports code?

thanks,

Chris
2006 IR Open Discussion's picture
Joined: Aug 10 2006 - 3:29am
Last seen: 16 years 7 months ago

3 Answers:

Anybody running into this problem too? I have set the encoding to ISO-8859-1 (though it could be UTF-8) and it is not converting to the correct characters:

[IMG]]

I am not sure if it is related to the suggestion above. Any help would be greatly appreciated =)

Thanks!
Kris

Post edited by: krispyjala, at: 2007/06/05 19:16

Post edited by: krispyjala, at: 2007/06/05 19:17
Post edited by: krispyjala, at: 2007/06/05 19:18
krispyjala's picture
Joined: Feb 7 2007 - 7:19am
Last seen: 16 years 1 month ago
OK I found the solution on another thread (Thanks Teodor!)

Apparently I need to check the box that says "Is Styled Text" and then it won't convert my & to & and display the unicode chars correctly.

Here's the link to the original thread:
http://www.jasperforge.org/index.php?option=com_joomlaboard&Itemid=&func=view&catid=8&id=6928#6928


Sorry if I messed up this thread! (Doesn't seem like my problem had anything to do w/ the xmlParser util).

Kris.
Post edited by: krispyjala, at: 2007/06/05 21:34
krispyjala's picture
Joined: Feb 7 2007 - 7:19am
Last seen: 16 years 1 month ago

krispyjala
Wrote:

OK I found the solution on another thread (Thanks Teodor!)

Apparently I need to check the box that says "Is Styled Text" and then it won't convert my & to & and display the unicode chars correctly.

Here's the link to the original thread:
http://www.jasperforge.org/index.php?option=com_joomlaboard&Itemid=&func=view&catid=8&id=6928#6928


Sorry if I messed up this thread! (Doesn't seem like my problem had anything to do w/ the xmlParser util).

Kris.
Post edited by: krispyjala, at: 2007/06/05 21:34

Hello,

 

   I'm working with jasper 0.6.7, before print, i can see (HTML) the right display, but when I try to see the PDF, the chars were written unescaped:

   HTML:

   PDF: Ó Ô Õ Ö Ø Ù Ú Û Ü Ý Þ

   Some chars are converted ok as well, like Ú, for example. I attached the screen format of the text field.

I would see the same chars in HTML and PDF as well.

 

Thanks and regards,
Zulu.

Code:
    public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
        throws ServletException, IOException
    {
        try
        {
        	boolean novopdf = false;
            httpservletresponse.setContentType("application/pdf");
            List vector = (List)httpservletrequest.getAttribute("dataBeanCollection");
            JRBeanCollectionDataSource jrbeancollectiondatasource = null;
            if (null!=vector){
	            if (vector.get(0) instanceof RecuperarItensComposicaoPDFDTO){
	            	if ("1".equals(httpservletrequest.getParameter("icSituacao"))){
	            		novopdf =true;
	            	}else if (!"N".equals(httpservletrequest.getParameter("indItemSemPreco").trim())){
	            		novopdf =true;
	            	}
	            }
	            ValidarColunaPdf validarColunaPdf = new ValidarColunaPdf();
	            jrbeancollectiondatasource = new JRBeanCollectionDataSource(validarColunaPdf.preencheListPDF(httpservletrequest,vector));
            }else{
	            jrbeancollectiondatasource = new JRBeanCollectionDataSource(vector);
            	SipciHelper.log.info("VETOR DO PDF NULL");
            }
            Object obj = (Map)httpservletrequest.getAttribute("parameters");
 
            if(obj == null){
                obj = new HashMap();
            }
 
            String s = SipciHelper.realPath;
 
            ((Map) (obj)).put("pathImagens", s);
 
            httpservletrequest.setAttribute("parameters", obj);
            String s1 = "";
            if (novopdf){
            	s1 = "RecuperarItensComposicaoDetalheNovo.jasper";
            }else{
            	s1 = httpservletrequest.getParameter("file");
            }
 
            File file = new File((new StringBuilder()).append(s).append("/docroot/_report/").append(s1).toString());
 
            JasperReport jasperreport = (JasperReport)JRLoader.loadObject(file);
 
            JasperPrint jasperprint;
 
            if(vector != null)
            {
                jasperprint = JasperFillManager.fillReport(jasperreport, ((Map) (obj)), jrbeancollectiondatasource);
            } else
            {
                JREmptyDataSource jremptydatasource = new JREmptyDataSource();
                jasperprint = JasperFillManager.fillReport(jasperreport, ((Map) (obj)), jremptydatasource);
            }
 
            /*List<JRBasePrintPage> listajp = jasperprint.getPages();
            for (JRBasePrintPage jp:listajp){
 
            	System.out.print(jp.getElements());
            	List<JRPrintElement> lstp = jp.getElements();
            	for (JRPrintElement jr:lstp){
 
            		jr.
 
            	}
 
            }*/
 
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
 
            bytearrayoutputstream.write(JasperExportManager.exportReportToPdf(jasperprint));
 
            httpservletresponse.setContentType("application/pdf");
 
            httpservletresponse.setHeader("Content-Disposition", (new StringBuilder()).append("inline; filename=").append(file.toString()).toString());
 
            httpservletresponse.setContentLength(bytearrayoutputstream.size());
 
            ServletOutputStream servletoutputstream = httpservletresponse.getOutputStream();
 
            servletoutputstream.write(bytearrayoutputstream.toByteArray(), 0, bytearrayoutputstream.size());
 
            servletoutputstream.flush();
 
            servletoutputstream.close();
        }
        catch(Exception exception) {
        	if (exception.getCause() != null) {
        		SipciHelper.log.fatal("Erro principal na criacao do PDF: " + exception.getCause(), exception.getCause());
        	}
        	SipciHelper.log.fatal("Erro na criacao do PDF: " + exception, exception);
        	throw new ServletException((new StringBuilder()).append("Erro na gera\347\343o do PDF: ").append(exception.getMessage()).toString());
        }
    }
</td></tr></tbody></table>
zulubeba's picture
Joined: Sep 14 2011 - 2:18pm
Last seen: 11 years 6 months ago
Feedback