Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE in PdfContentByte ... table cannot be drawn when exceeding space and uses background color ... now with test #370

Closed
arnthom opened this issue May 13, 2020 · 6 comments

Comments

@arnthom
Copy link

arnthom commented May 13, 2020

Hello there!
I use several tables in my created pdf document to set the text layout.
At some step I use up to three tables (with background), if the next table would cause a page break a NullPointerException occurs.
This NPE occurs at
Caused by: com.lowagie.text.DocumentException: java.lang.NullPointerException at com.lowagie.text.pdf.PdfDocument.add(PdfDocument.java:770) at com.lowagie.text.Document.add(Document.java:293) at de.msg.vorsorge.all.pdf.PdfCTabelle.go(PdfCTabelle.java:337) ... 10 more Caused by: java.lang.NullPointerException at com.lowagie.text.pdf.PdfContentByte.setGState(PdfContentByte.java:3042) at com.lowagie.text.pdf.PdfContentByte.saveColorFill(PdfContentByte.java:2350) at com.lowagie.text.pdf.PdfContentByte.setRGBColorFill(PdfContentByte.java:2233) at com.lowagie.text.pdf.PdfContentByte.setColorFill(PdfContentByte.java:2343) at com.lowagie.text.pdf.PdfContentByte.rectangle(PdfContentByte.java:1029) at com.lowagie.text.pdf.PdfDocument.renderCells(PdfDocument.java:3037) at com.lowagie.text.pdf.PdfDocument.addPdfTable(PdfDocument.java:2694) at com.lowagie.text.pdf.PdfDocument.add(PdfDocument.java:729) ... 12 more

In this context a PdfContentByte(null) is created in PdfDocument.addPdfTable.
Then, in PdfContentByte.setGState, the (not initalized) writer is used ... NPE occurs.

It occurs as I see now, when a page break occurs and the table uses a background color (lightgrey).
I use .setTableFitsPage(true), but when using .setTableFitsPage(false), there is the NPE as well.

As long as the the table with background color does not exceed the remaining space (or no background color ist used), it is drawn, too.
So the NPE occurs when exceeding the remaining page size and when background color is used.

NOTE: with openPdf 1.3.11 my code worked and no NPE occured. Is it a bug since 1.3.12 or a bug or too-old-fashioned code in my project?

@arnthom arnthom changed the title need help with table in PdfDocument ... suddenly there ist a NPE in PdfContentByte need help with table in PdfDocument ... suddenly there is a NPE in PdfContentByte May 14, 2020
@arnthom arnthom changed the title need help with table in PdfDocument ... suddenly there is a NPE in PdfContentByte NPE in PdfContentByte ... table cannot be drawn when exceeding space and uses background color May 14, 2020
@arnthom
Copy link
Author

arnthom commented May 26, 2020

OK, I have a test class for this error, maybe this helps ...
If the cell exceeds the remaining space AND a background is used, the NEP occurs.

import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;

public class TestClass {
    public static void main(String[] args) {
        try {
        	final String loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." +  "\r\n" + "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.\r\n" + 
        			"\r\n" + 
        			"Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.\r\n" + 
        			"\r\n" + 
        			"Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. ";
        	final Cell cLoremIpsum = new Cell(new Paragraph(loremIpsum));
        	cLoremIpsum.setBackgroundColor(Color.LIGHT_GRAY);
            Document document = new Document(PageSize.A4);

            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("fail.pdf"));

            Table table = new Table(1, 2);
            table.setWidth(100);
//            table.setCellsFitPage(true);
            table.addCell(cLoremIpsum);
            table.addCell(cLoremIpsum);

            document.open();

            document.add(new Paragraph("Hello World"));
            
            document.add(table);
            
            document.close();
            writer.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

@arnthom arnthom changed the title NPE in PdfContentByte ... table cannot be drawn when exceeding space and uses background color NPE in PdfContentByte ... table cannot be drawn when exceeding space and uses background color ... now with test May 26, 2020
@arnthom
Copy link
Author

arnthom commented May 26, 2020

as I said, in openPDF 1.3.11 this works, with 1.3.12 up to 1.3.17, the NPE occurs.

table.setConvert2pdfptable(true);
prevents the NPE, but I cannot use this approach because of rowspan > 1, which does not work with PdfPCell.

@andreasrosdal
Copy link
Contributor

Thanks for reporting this bug.

This seems to be the relevant code where the NullpointerException occurs:
https://github.com/LibrePDF/OpenPDF/blame/1.3.12/openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java#L3041

This seems to be the relevant pull request:
#282

Perhaps we can just add some checks to protect this method from NPExceptions?
Pull requests welcome!

@andreasrosdal
Copy link
Contributor

I have added a fix to the master branch here: 2c53d75

@arnthom Can you please try the latest master branch from git of OpenPDF, and see if the problem has been solved for you there?

@arnthom
Copy link
Author

arnthom commented Jun 2, 2020

Yes! @andreasrosdal it works, thank you!

@andreasrosdal
Copy link
Contributor

OpenPDF 1.3.18 has been released today, with this fix included.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants