title: Cannot read pdf generated by iText
body: <p>This is my code which generates pdf:</p>
  
  <pre><code>public String generateList( Group group, List&lt;GroupTerm&gt; terms, List&lt;Child&gt; children, int begin, int finish )
  {
      String pathForList = "C:\\...\\List.pdf";
  
      File filePath = new File( pathForList );
      filePath.delete();
  
      try
      {
  
          Document document = new Document( PageSize.A4.rotate() );
  
          PdfWriter.getInstance( document, new FileOutputStream( pathForList ) );
  
          document.open();
  
          // CONTENT
  
          BaseFont helvetica = BaseFont.createFont( BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED );
          Font helvetica9 = new Font( helvetica, 9 );
          Font helvetica9bold = new Font( helvetica, 9, Font.BOLD );
  
          Paragraph paragraph1 =
              new Paragraph( "Godziny: " + group.getStartHours() + "-" + group.getFinishHours() + "  Miejsce: " + group.getPlace()
                  + "  Grupa wiekowa: " + group.getAgeGroupName() + "  Poziom: " + group.getLevel() + "  Instruktor: " + group.getInstructor(),
                             helvetica9bold );
  
          paragraph1.setAlignment( Element.ALIGN_LEFT );
  
          document.add( paragraph1 );
          document.add( new Paragraph( " " ) ); 
  
          PdfPTable table = new PdfPTable( 12 ); // 12 columns.
  
          PdfPCell cell01 = new PdfPCell( new Paragraph( "Imię", helvetica9 ) );
          PdfPCell cell02 = new PdfPCell( new Paragraph( "Nazwisko", helvetica9 ) );
          table.addCell( cell01 );
          table.addCell( cell02 );
  
          for ( int i = begin; i &lt; finish; i++ 
          {
              GroupTerm term = new GroupTerm();
  
              int iterator = -1;
              int a = i + 1;
  
              while ( term.getTermID() != a )
              {
                  iterator++;
                  term = terms.get( iterator );
              }
  
              table.addCell( new PdfPCell( new Paragraph( conv.dateFromCalToString( term.getTerm() ), helvetica9 ) ) );
          }
  
          for ( int j = 0; j &lt; children.size(); j++ )
          {
              table.addCell( new PdfPCell( new Paragraph( children.get( j ).getName() ) ) );
              table.addCell( new PdfPCell( new Paragraph( children.get( j ).getSurname() ) ) );
              for ( int k = 0; k &lt; 10; k++ )
              {
                  table.addCell( new PdfPCell( new Paragraph( "" ) ) );
              }
          }
  
          document.add( table );
  
          document.close();
      }
      catch ( Exception e )
      {
  
      }
  
      return pathForList;
  }
  </code></pre>
  
  <p>And the problem is as goes: I generate pdf for a certain data, it creates List.pdf, it's well done. But, then I'm trying to generate one more for another set of data and generated file has size 0kb, and whene opened shows message "Adobe Reader could not open "List.pdf" beacuse it is either not a supported fily type or beacuse the file has been damaged".</p>
  
  <p>PDF is generated in my web app and send via servlet in response so I'm oppening it through browser.</p>
  
  <p>EDIT: My servlet code:</p>
  
  <pre><code>protected void doGet( HttpServletRequest request, HttpServletResponse response )
      throws ServletException, IOException
  {
      ... // Getting data here
  
      String path = pdf.generateList( group, terms, children, begin, finish );
  
      // download
  
      response.setContentType( "application/octet-stream" );
      response.setHeader( "Content-Disposition", "attachment;filename=List.pdf" );
  
      ServletOutputStream out = null;
  
      try
      {
          File file = new File( path );
          FileInputStream fileIn = new FileInputStream( file );
          out = response.getOutputStream();
  
          byte[] outputByte = new byte[4096];
          // copy binary contect to output stream
          while ( fileIn.read( outputByte, 0, 4096 ) != -1 )
          {
              out.write( outputByte, 0, 4096 );
          }
          fileIn.close();
          out.flush();
          out.close();
  
          int i = 0;
      }
      catch ( Exception e )
      {
          if ( out != null )
          {
              out.close();
          }
      }
      finally
      {
  
      }
  
      response.sendRedirect( "calendar.jsp" );
  }
  </code></pre>
  
  <p>EDIT: I also use iText to generate invoice in this app, and it works fine. All pdf's are correct, no matter what data I use.</p>
  
  <p>What is wrong? I'm using the same method, only set of data is different. </p>