Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HttpServletResponse - CharacterEncoding - PDF

843842Aug 7 2008 — edited Aug 7 2008
I'm trying to write a PDF that containts special (French) characters, but the characters are displayed incorrectly.

response = HttpServletResponse
doc= byte[] that contains my PDF
response.setContentType("application/pdf;charset=UTF-8;");
response.setHeader("Content-Disposition", "filename=document-" + doc.getId() + ".pdf");
        		
response.setCharacterEncoding("UTF-8");
                
response.getOutputStream().write(doc);
response.flushBuffer();
When I try to write � I get some weird useless characters.
I read that response.setCharacterEncoding("UTF-8"); doesn't do anthing if you write to response.getOutputStream()

So I changed that to:
response.setContentType("application/pdf;charset=UTF-8;");
response.setHeader("Content-Disposition", "filename=document-" + doc.getId() + ".pdf");
        		
response.setCharacterEncoding("UTF-8");
        
Writer goodWriter = new java.io.OutputStreamWriter(response.getOutputStream(), "UTF-8" );
        
for (int i = 0 ; i <  doc.length ; i++) {
     goodWriter.write(doc);
}

response.flushBuffer();
However, that also doens't work. I still get the same useless characters. Which I find strange because the following code does display the characters correctly (to HTML, not to PDF)
response.setContentType( "text/html;charset=UTF-8" );
response.setHeader("Content-Disposition", "filename=document-" + doc.getId() + ".pdf");

response.setCharacterEncoding("UTF-8");

Writer goodWriter = new java.io.OutputStreamWriter(response.getOutputStream(), "UTF-8" );

for (int i = 0 ; i < doc.length ; i++) {
goodWriter.write("���");
}

response.flushBuffer();
So the last code block, gives me the desired result: "���".
Anyone can tell me how I can get "���" in my PDF? Am I missing something?

(the last for-loop is useless, I'm just trying to get it to work)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 4 2008
Added on Aug 7 2008
3 comments
1,612 views