Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.document;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

import com.spice.profitmandi.common.document.section.FooterSection;
import com.spice.profitmandi.common.document.section.HeaderSection;
import com.spice.profitmandi.common.document.section.ItemsTableSection;
import com.spice.profitmandi.common.document.section.PartiesSection;
import com.spice.profitmandi.common.document.section.Section;
import com.spice.profitmandi.common.document.section.SummarySection;
import com.spice.profitmandi.common.document.support.CancelledStamp;
import com.spice.profitmandi.common.util.EWayBillPDF;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Renders a list of {@link DocumentData} into a single PDF by driving the canonical section
 * pipeline (V2's look) for each document. Replaces the duplicated bodies of the legacy
 * {@code generateInvoiceV2 / generateAndWrite / generateAndWriteDebitNote /
 * generateAndWriteCustomerCreditNotes} methods.
 */
public class DocumentRenderer {

    private static final Logger LOGGER = LogManager.getLogger(DocumentRenderer.class);

    // Stateless, reusable. Order defines the page layout.
    private static final List<Section> SECTIONS = Arrays.asList(
            new HeaderSection(),
            new PartiesSection(),
            new ItemsTableSection(),
            new SummarySection(),
            new FooterSection());

    public void render(List<DocumentData> docs, OutputStream out) {
        try {
            ByteArrayOutputStream pdfBuffer = new ByteArrayOutputStream();
            boolean cancelledPages = false;
            List<Integer> cancelledPageList = new ArrayList<>();

            Document document = new Document(PageSize.A4, 28, 28, 34, 34);
            PdfWriter pdfWriter = PdfWriter.getInstance(document, pdfBuffer);
            document.open();

            for (DocumentData data : docs) {
                if (data.isCancelled()) {
                    cancelledPageList.add(1);
                    cancelledPages = true;
                } else {
                    cancelledPageList.add(0);
                }

                PdfContext ctx = new PdfContext(document, data);
                for (Section section : SECTIONS) {
                    section.render(ctx);
                }

                document.newPage();

                if (data.eWayBill() != null) {
                    EWayBillPDF.generateDocument(document, pdfWriter, data.eWayBill());
                }
            }

            document.close();
            if (cancelledPages) {
                CancelledStamp.apply(pdfBuffer, cancelledPageList);
            }
            pdfBuffer.writeTo(out);
        } catch (DocumentException e) {
            LOGGER.error("Unable to write data to pdf file : ", e);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}