无法读取iText生成的pdf

时间:2013-03-03 10:41:23

标签: java pdf servlets 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“因为它不是支持的fily类型或beacuse文件已损坏“。

PDF是在我的网络应用程序中生成的,并通过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都是正确的。

有什么问题?我使用的方法相同,只是数据集不同。

2 个答案:

答案 0 :(得分:1)

尝试用以下内容替换内循环:

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

您的原始循环总是在文件末尾写入4096个字节,但文件的最后一个块可能比此短,因此您的原始循环在HTTP响应结束时写入垃圾,将其填充到多个4096字节。

答案 1 :(得分:0)

问题解决了。这是数据本身。方法无法核心获取数据并且文件被破坏。我改变了发送数据的方式,它工作了!