[英]Cannot read pdf generated by iText
這是我生成pdf的代碼:
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”,因為它不是受支持的文件類型,或者是因為文件已損壞”。
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都是正確的。
怎么了? 我使用的是相同的方法,只是數據集不同。
嘗試將內部循環替換為:
int got;
while ( (got = fileIn.read( outputByte, 0, 4096 )) > 0) {
out.write( outputByte, 0, got );
}
您的原始循環始終在文件末尾寫入4096字節,但文件的最后一塊可能比這短,因此您的原始循環在HTTP響應末尾寫入了垃圾,將其填充為4096字節的倍數。
問題解決了。 它是數據本身。 方法無法正確獲取數據並且文件已損壞。 我更改了發送數據的方式,並且有效!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.