0

これは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を作成します、それはよくできています。しかし、別のデータセット用にもう1つ生成しようとしていますが、生成されたファイルのサイズは0kbで、開いたときに「AdobeReaderは「List.pdf」を開くことができませんでした。サポートされているファイルタイプではないか原因です。ファイルが破損しています。」

PDFは私のWebアプリで生成され、それに応じてサーブレットを介して送信されるので、ブラウザーを介してそれを操作しています。

編集:私のサーブレットコード:

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は正しいです。

なにが問題ですか?同じ方法を使用していますが、データセットのみが異なります。

4

2 に答える 2

1

内部ループを次のように置き換えてみてください。

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

元のループは常にファイルの最後に 4096 バイトを書き込みましたが、ファイルの最後のブロックはそれよりも短い可能性があるため、元のループは HTTP 応答の最後にジャンクを書き込み、4096 バイトの倍数までパディングしました。 .

于 2013-03-03T11:03:47.730 に答える
0

問題は解決しました。それはデータそのものでした。メソッドがデータを正しく取得できず、ファイルが壊れていました。データの送信方法を変更したところ、うまくいきました!

于 2013-03-07T18:23:41.507 に答える