Search code examples
itextitext7

How can I get rid of PDF startxref error and AbstractRenderer warning messages when generating PDF documents and then merging it using iText 7?


I am using iText version 7.1.6 to generate PDF documents and in the end I am trying to merge it.

Below is the code used for merging along with comments.

List<byte[]> pdfDocumentList= new ArrayList<byte[]>();

// pdfDocumentList has list of byte arrays generated from other ways
ByteArrayOutputStream mergeOutputStream = new ByteArrayOutputStream();
PdfDocument pdfMerged = new PdfDocument(new PdfWriter(mergeOutputStream));
PdfMerger merger = new PdfMerger(pdfMerged);
ByteArrayOutputStream finalOutputStream = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(finalOutputStream);
PdfDocument pdf = new PdfDocument(writer);

// sb is containing the concatenated HTML sources
HtmlConverter.convertToPdf(sb.toString(), pdf, properties);
pdf.close();
pdfDocumentList.add(finalOutputStream.toByteArray());
if(!pdfDocumentList.isEmpty()){
    for(byte[] bytes : pdfDocumentList){
        PdfDocument externalPdf = new PdfDocument(new PdfReader(new ByteArrayInputStream(bytes)));
        merger.merge(externalPdf, 1, externalPdf.getNumberOfPages());
    }
}
pdfMerged.close();
return mergeOutputStream.toByteArray();

When I am merging the list of PDF documents, I get the below error and warning. In addition, the warning keeps getting printed multiple times. How can I fix it?

Warning

WARNING: The background rectangle has negative or zero sizes. It will not be displayed.
Jul 18, 2019 2:24:24 PM com.itextpdf.layout.renderer.AbstractRenderer drawBackground

Error

<Jul 18, 2019, 2:27:19,964 PM AST> <Error> <com.itextpdf.kernel.pdf.PdfReader> <BEA-000000> <Error occurred while reading cross reference table. Cross reference table will be rebuilt.
com.itextpdf.io.IOException: PDF startxref not found.
    at com.itextpdf.io.source.PdfTokenizer.getStartxref(PdfTokenizer.java:262)
    at com.itextpdf.kernel.pdf.PdfReader.readXref(PdfReader.java:753)
    at com.itextpdf.kernel.pdf.PdfReader.readPdf(PdfReader.java:538)
    at com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1818)
    at com.itextpdf.kernel.pdf.PdfDocument.<init>(PdfDocument.java:238)
    Truncated. see log file for complete stacktrace
>
2019-07-18 14:27:19 ERROR user: KALASINX ip: 127.0.0.1 (ServiceInterceptor.java:59) ~ ServiceInterceptor Error:
com.itextpdf.kernel.PdfException: Trailer not found.
    at com.itextpdf.kernel.pdf.PdfReader.rebuildXref(PdfReader.java:1064) ~[kernel-7.1.6.jar:?]

Solution

  • After analyzing the HTML code and with repeated testing, I was able to get rid of the warning message. I had to remove the background-color style in the CSS of the HTML that was related to the table, tr, and td tags.