java - 无法读取 iText 生成的 pdf

标签 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”,因为它不是受支持的苍蝇类型,或者因为文件已损坏”。

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 都是正确的。

出了什么问题?我用的是同样的方法,只是数据集不同。

最佳答案

尝试将内部循环替换为:

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

您的原始循环始终在文件末尾写入 4096 字节,但文件的最后一个 block 可能比这个短,因此您的原始循环在 HTTP 响应末尾写入垃圾,将其填充为多个4096 字节。

关于java - 无法读取 iText 生成的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15184798/

相关文章:

java - 莫里斯图表从外部 JSON 中破坏

java - 无法验证从 Android 应用程序获取的 id token

'IF' 语句中的 Java boolean 值不起作用

java - 管理员和用户的访问权限

java - 修复缺少数字的代码

python - 直接在 Python 中处理来自网络的 pdf 吗?

C#/ASP.NET - 从 PDF/DOC 文件获取缩略图

java - 如何使用 PDFBox 将 PDF 文件的一部分渲染为图像?

Java匿名类的使用

java - 不朽的 HttpSession?