繁体   English   中英

无法读取iText生成的pdf

[英]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,并且当打开时显示消息“ Adob​​e 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM