在不增加堆大小的情况下创建PDF

时间:2013-10-22 05:49:43

标签: jvm itext out-of-memory

我正在使用iText 1.4.1。

我正在尝试将简单表添加到文档中。

但是在创建PDF时丢失了内存异常。

该表从数据库中获取数据,其中包含数十万条记录。

这是我的示例代码。

    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();
      }

任何人都可以建议我如何在不增加jvm堆大小的情况下摆脱这个异常......

0 个答案:

没有答案