0

I am using iText 1.4.1.

I am trying to add the simple table to the document.

But it was throwing Out of memory exception in creating the PDF.

The table is getting the data from the DB which has some lakh of records.

here is my sample code.

    public String createPDF(List<OutboundPriorityGridDTO> resultList,
        ExportInputDTO arg1) {

    Document document = new Document(PageSize.A4.rotate(), 15, 15, 25, 25);
    try 
    {
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
    TableHeader includeHeader = new TableHeader();
     writer.setPageEvent(includeHeader);
    document.open();
    document.addCreationDate();
        if (isEmptyList(resultList)){
            document.add(new Phrase(new Chunk("\n**** No Records are returned. ***", PdfConstants.normalFont12)));
            stampAndFlushFilePath(document, filePath, getPageOrientation(document));
            return filePath;
        }

   for (OutboundPriorityGridDTO gridDto : resultList) {
    light =  zeroIfNull(gridDto.getTotalOlpns())
                        .subtract(zeroIfNull(gridDto.getHeavyOlpns()));

                table.addCell(createPdfCell(cell,gridDto.getShipmentNumber(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
                addInterimColumn( table, fontType);
                table.addCell(createPdfCell(cell,gridDto.getCarrier(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
                addInterimColumn( table, fontType);
                table.addCell(createPdfCell(cell, gridDto.getDestination(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
                addInterimColumn( table, fontType);

                table.addCell(createPdfCell(cell,gridDto.getEstimatedWeight(),Element.ALIGN_RIGHT,0,BaseColor.BLACK,fontType));
                addInterimColumn( table, fontType);
                table.addCell(createPdfCell(cell,gridDto.getEstimatedCube(),Element.ALIGN_RIGHT,0,BaseColor.BLACK,fontType));
                addInterimColumn( table, fontType);
                table.addCell(createPdfCell(cell,gridDto.getDateCreated(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
                addInterimColumn( table, fontType);
                table.addCell(createPdfCell(cell,gridDto.getStop(),Element.ALIGN_RIGHT,0,BaseColor.BLACK,fontType));
                addInterimColumn( table, fontType);
                table.addCell(createPdfCell(cell,gridDto.getDockDoorName(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
                addInterimColumn( table, fontType);
                table.addCell(createPdfCell(cell,gridDto.getTrailerNumber(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
                addInterimColumn( table, fontType);
                table.addCell(createPdfCell(cell,light,Element.ALIGN_RIGHT,0,BaseColor.BLACK,fontType));
                addInterimColumn( table, fontType);
                table.addCell(createPdfCell(cell,gridDto.getHeavyOlpns(),Element.ALIGN_RIGHT,0,BaseColor.BLACK,fontType));
                addInterimColumn( table, fontType);
                table.addCell(createPdfCell(cell,gridDto.getTotalOlpns(),Element.ALIGN_RIGHT,0,BaseColor.BLACK,fontType));
                addInterimColumn( table, fontType);
                table.addCell(createPdfCell(cell,gridDto.getShipmentStatus(),Element.ALIGN_LEFT,0,BaseColor.BLACK,fontType));
                table.completeRow();
            }
     stampAndFlushFilePath(document, filePath, getPageOrientation(document));



      public static void stampAndFlushFilePath(Document document, String filePath, boolean isPageRotated) throws IOException, FileNotFoundException, DocumentException {
    document.close();
    LOGGER.info(filePath+" successfully created.");
    PdfReader reader = new PdfReader(new FileInputStream(filePath));
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filePath));

    int n = reader.getNumberOfPages();
    getFooterTable(n, document, stamper, isPageRotated);

    stamper.close();
      }

Can any one suggest me on how to get rid of this exception without increasing the jvm heap size...

Srinivas B
  • 1,821
  • 4
  • 17
  • 35

0 Answers0