MaikJaek / flying-saucer

Automatically exported from code.google.com/p/flying-saucer
0 stars 0 forks source link

PDF rendering crashes when using iText 5 branch #188

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. run "ant browser" on iText 5 branch
2. generate PDF
3. observe NullPointerException in stacktrace

What is the expected output? What do you see instead?
Expected output is a valid PDF file (the file is generated but the PDF is 
invalid).

What version of the product are you using? On what operating system?
The iText 5 branch with iText 5.1.3 on Windows 7 64-bit.

Please provide any additional information below.
The problem is caused by an incorrect implementation of 
ITextRenderer.provideMetadataToPage. This method sets the byte array "metadata" 
even if there is no metadata. In this case a null array is passed to the 
pdfWriter who cannot deal with this and crashes. The problem can be easily 
fixed by only assigning metadata if there is actually metadata:

private void provideMetadataToPage(PdfWriter writer, PageBox page) throws 
IOException {
        byte[] metadata = null;
        if (page.getMetadata() != null) {
            try {
                String metadataBody = stringfyMetadata(page.getMetadata());
                if (metadataBody != null) {
                    metadata = createXPacket(stringfyMetadata(page.getMetadata())).getBytes("UTF-8");
                }
                writer.setPageXmpMetadata(metadata);
            } catch (UnsupportedEncodingException e) {
                // Can't happen
                throw new RuntimeException(e);
            }
        }
    }

Original issue reported on code.google.com by steve.va...@gmail.com on 9 Jan 2012 at 2:33