> //Try writing b to response.getOutputStrem().
> response.getOutputStream().write(readBytes);

The comment and code don't match.

You're reading the bytes into "b", then using in.read() again, when it's 
already at the EOF.  Use this instead of the while loop:

response.getOutputStream().write(b); 

Please restrict your questions to those directly related to iText in the future.
 
--Mark Storer
  Senior Software Engineer
  Cardiff.com
 
import legalese.Disclaimer;
Disclaimer<Cardiff> DisCard = null;
 
 


________________________________

        From: Klas Lindbäck [mailto:[email protected]] 
        Sent: Monday, August 16, 2010 3:57 AM
        To: Post all your questions about iText here
        Subject: Re: [iText-questions] problem in serving pdf to browser
        
        
        See comments in the code below.
        
        /Klas
        
        Venkata Pachawa skrev 20:59: 

                Hello,
                
                I am using JFreeChart and iText.
                I am creating a chart and saving it as pdf in my file system.
                When I open it, it works (pdf with content is displayed).
                
                When I tried to serve the pdf to browser as attachment, I get 
error as Adobe cannot open the file as it is damaged or corrupted.
                If I try feeding the pdf inline, the header of the webpage 
changes and everything else remains the same. Status bar shows Done.
                For a long time nothing happens. (pdf file size on file system 
is 120 kb)
                
                My code for serving pdf to browser is:
                
                String pdfFileName = System.getProperty("user.home")
                                            + "/pragmatic" + remoteAddr + 
".pdf";
                
                                    File file = new File(pdfFileName);
                                    BufferedInputStream in = new 
BufferedInputStream(new FileInputStream(file));
                
                                    byte[] b = new byte[(int) file.length()];
                                    in.read(b);
                


        The pdf is now in b and "in" is at EOF.
        
        

                                    response.setContentType("application/pdf");
                                    response.setContentLength(b.length);
                                    response.addHeader("Content-Disposition", 
"attachment; filename=" + pdfFileName);
                                    response.setHeader("Expires", "0");
                                    response.setHeader("Cache-Control", 
"must-revalidate, post-check=0, pre-check=0");
                                    response.setHeader("Pragma", "public");
                
                                    response.setContentLength((int) 
file.length());
                
                                    int readBytes = 0;
                
                                    while ((readBytes = in.read()) != -1) {
                


        Try writing b to response.getOutputStrem().
        
        

                                        
response.getOutputStream().write(readBytes);
                                    }
                
                
                                    response.getOutputStream().flush();
                
                Can you help me out in figuring what the problem might be and 
what can I do to overcome that!
                
                Thanks.
                
                Appreciate your help.
                
                Regards,
                Venkata Rakesh Pachawa
                


        No virus found in this incoming message.
        Checked by AVG - www.avg.com
        Version: 9.0.851 / Virus Database: 271.1.1/3068 - Release Date: 
08/15/10 23:35:00
        


------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to