2013-03-03 102 views
0

生成的PDF這是我的代碼生成PDF:無法讀取的iText

public String generateList(Group group, List<GroupTerm> terms, List<Child> 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 < 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 < 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 < 10; k++) 
      { 
       table.addCell(new PdfPCell(new Paragraph(""))); 
      } 
     } 

     document.add(table); 

     document.close(); 
    } 
    catch (Exception e) 
    { 

    } 

    return pathForList; 
} 

而且問題是雲:我生成PDF一定的數據,它會創建List.pdf,它做得很好。但是,然後我試圖爲另一組數據生成一個,生成的文件大小爲0kb,並打開顯示消息「Adobe Reader無法打開」List.pdf「,因爲它不是支持的fily類型或beacuse該文件已被損壞「。

PDF在我的web應用程序中生成,並通過servlet發送,以便通過瀏覽器進行操作。

編輯:我的servlet代碼:

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"); 
} 

編輯:我也用iText的生成此應用程序的發票,並能正常工作。無論我使用什麼數據,所有的pdf都是正確的。

有什麼不對?我使用相同的方法,只有一組數據是不同的。

+1

如果出現問題,您將永遠不會因爲空的catch(Exception e)塊。 – jlordo 2013-03-03 10:48:10

+0

文檔關閉文件流嗎?沖洗並手動關閉。 – 2013-03-03 10:48:39

+0

@jlordo:catch只有簡單的日誌實現,它對我沒有任何回報。 Michael-O:我已經在上面添加了我的servlet代碼。當debbuging似乎出來,如果流正在關閉(關閉屬性更改false-> true) – eBEER 2013-03-03 10:56:06

回答

0

問題解決了。這是數據本身。方法無法正確獲取數據並且文件被破壞。我改變了我發送數據的方式,它的工作原理!

1

試着用替換你的內循環:

int got; 
while ((got = fileIn.read(outputByte, 0, 4096)) > 0) { 
    out.write(outputByte, 0, got); 
} 

你原來的循環總是寫4096個字節的文件的末尾,但該文件的最後一塊可能比短,所以你的原始循環寫道:垃圾郵件在HTTP響應的末尾,填充爲4096字節的倍數。

+0

它沒有幫助,但這個循環似乎更合理,然後我的,所以我會保留它。 – eBEER 2013-03-03 11:27:17

+0

問題解決了。這是數據本身。方法無法正確獲取數據並且文件被破壞。我改變了我發送數據的方式,它的工作原理! – eBEER 2013-03-03 11:29:06

+1

@eBEER:將其添加爲答案並將其標記爲已接受,所以現在人們無需閱讀每條評論即可解決問題。 – jlordo 2013-03-03 11:35:36