生成的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都是正確的。
有什麼不對?我使用相同的方法,只有一組數據是不同的。
如果出現問題,您將永遠不會因爲空的catch(Exception e)塊。 – jlordo 2013-03-03 10:48:10
文檔關閉文件流嗎?沖洗並手動關閉。 – 2013-03-03 10:48:39
@jlordo:catch只有簡單的日誌實現,它對我沒有任何回報。 Michael-O:我已經在上面添加了我的servlet代碼。當debbuging似乎出來,如果流正在關閉(關閉屬性更改false-> true) – eBEER 2013-03-03 10:56:06