Subversion Repositories SmartDukaan

Rev

Rev 35024 | Rev 35027 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.util;

import com.ibm.icu.text.RuleBasedNumberFormat;
import com.itextpdf.text.*;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.*;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.util.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class PdfUtils {

    private static final Font FONT_TITLE = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);
    private static Font FONT_NORMAL = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
    private static Font FONT_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);
    // private static Font fontTableHeader = new
    // Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD);
    public static final String INVOICE_TITLE = "TAX INVOICE";
    public static final String DEBIT_NOTE_TITLE = "DEBIT NOTE";
    public static final String SECURITY_DEPOSIT = "SECURITY DEPOSIT RECEIPT";

    private static float[] igstWidthsWithDiscount = new float[]{.2f, 2.6f, 0.7f, .4f, 0.7f, 0.6f, .7f, .6f, 0.7f};
    private static float[] stateWidthsWithDiscount = new float[]{.2f, 2.1f, 0.7f, .3f, 0.6f, 0.5f, .7f, .6f, .6f, .7f};

    private static float[] igstWidths = new float[]{.6f, 2.6f, 0.7f, .4f, 0.7f, .7f, .6f, 0.6f, 0.9f};
    private static float[] stateWidths = new float[]{.6f, 2.1f, 0.7f, .3f, 0.6f, .7f, .5f, .6f, .5f, .6f, .8f};

    private static float[] igstWidthsCrNote = new float[]{2.6f, 0.7f, .4f, 0.7f, .7f, .6f, 0.6f, 0.9f};
    private static float[] stateWidthsCrNote = new float[]{2.1f, 0.7f, .3f, 0.6f, .7f, .5f, .6f, .5f, .6f, .8f};

    private static final Locale indianLocale = Locale.getDefault();

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

    private static final URL iconUrl = PdfUtils.class.getClassLoader().getResource("sdlogo.png");
    private static Image iconImg = null;

    static {
        try {
            iconImg = Image.getInstance(iconUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //Debit Note generation logic has been changed
    //Debit Note considers price drops so amount in debit note is current price of item.
    //From 16Nov 2019 onwards all debit notes will be as per actuall billing value, all pricedrops
    //shall be rolledback or cancelled once the debit note is generated.

    public static void generateAndWrite(List<InvoicePdfModel> pdfModels, PrinterType printerType, ByteArrayOutputStream outputStream) throws ProfitMandiBusinessException {
        if (PrinterType.A4.equals(printerType)) {
            generateAndWrite(pdfModels, outputStream);
        } else {
            if (pdfModels.size() > 1) {
            }
            if (PrinterType.W80.equals(printerType)) {
                InvoiceFormatter.getInvoice(pdfModels.get(0), outputStream, InvoiceFormatter.WIDTH_80MM);
            } else if (PrinterType.W58.equals(printerType)) {
                InvoiceFormatter.getInvoice(pdfModels.get(0), outputStream, InvoiceFormatter.WIDTH_58MM);

            }
        }
    }


    //Standard
    public static void generateAndWrite(List<InvoicePdfModel> pdfModels, ByteArrayOutputStream outputStream) {
        try {
            boolean cancelledPages = false;
            List<Integer> caneclledPageList = new ArrayList<>();
            Document document = new Document();
            document.setMargins(0, 0, 25, 0);
            PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
            document.open();
            for (InvoicePdfModel pdfModel : pdfModels) {
                CustomCustomer customer = pdfModel.getCustomer();
                CustomRetailer retailer = pdfModel.getRetailer();
                boolean stateGst = false;
                if (customer.getAddress().getState().equals(retailer.getAddress().getState())) {
                    stateGst = true;
                }
                List<CustomOrderItem> orderItems = pdfModel.getOrderItems();
                if (pdfModel.isCancelled()) {
                    caneclledPageList.add(1);
                    cancelledPages = true;
                } else {
                    caneclledPageList.add(0);
                }
                document.addTitle(pdfModel.getTitle());
                document.addAuthor(pdfModel.getAuther());

                Paragraph paragraphTitle = new Paragraph(INVOICE_TITLE, FONT_TITLE);
                paragraphTitle.setAlignment(Element.ALIGN_CENTER);

                document.add(paragraphTitle);

                Rectangle rectangle = document.getPageSize();

                /*Paragraph paragraphTitle = new Paragraph(INVOICE_TITLE, FONT_TITLE);
                paragraphTitle.setAlignment(Element.ALIGN_CENTER);*/
                /*pdfCell.addElement(paragraphTitle);
                pdfCell.setBorder(Rectangle.NO_BORDER);*/
                if (pdfModel.getIrnModel() != null) {
                    addIrnDetails(pdfModel.getIrnModel(), rectangle, document);
                }


                PdfPTable tableCustomerRetailer = new PdfPTable(2);
                tableCustomerRetailer.setWidthPercentage(90);
                tableCustomerRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                PdfPCell columnCustomerInfo = new PdfPCell();
                columnCustomerInfo.addElement(new Paragraph("Customer Details", FONT_BOLD));
                columnCustomerInfo.addElement(new Paragraph(
                        StringUtils.capitalize(customer.getAddress().getName() + (customer.getAddress().getLastName() == null ? "" : " " + customer.getAddress().getLastName())), FONT_NORMAL));
                if (customer.getAddress() != null) {
                    if ((customer.getAddress().getLine1() != null && !customer.getAddress().getLine1().trim().isEmpty()) || (customer.getAddress().getLine2() != null && !customer.getAddress().getLine2().trim().isEmpty())) {
                        columnCustomerInfo.addElement(new Paragraph(StringUtils.capitalize(customer.getAddress().getLine1()) + ", " + StringUtils.capitalize(customer.getAddress().getLine2()), FONT_NORMAL));
                    }
                    if ((customer.getAddress().getCity() != null && !customer.getAddress().getCity().trim().isEmpty()) || (customer.getAddress().getState() != null && !customer.getAddress().getState().trim().isEmpty())) {
                        columnCustomerInfo.addElement(new Paragraph(StringUtils.capitalize(customer.getAddress().getCity()) + ", " + StringUtils.capitalize(customer.getAddress().getState()) + "(" + pdfModel.getCustomerAddressStateCode() + ")" + "\n" + customer.getAddress().getPinCode(), FONT_NORMAL));
                    }
                }

                columnCustomerInfo.addElement(new Paragraph("Mobile - " + customer.getAddress().getPhoneNumber(), FONT_NORMAL));
                if (customer.getGstNumber() != null && !customer.getGstNumber().isEmpty()) {
                    columnCustomerInfo.addElement(new Paragraph("GST Number - " + customer.getGstNumber(), FONT_NORMAL));
                }
                columnCustomerInfo.setBorder(Rectangle.NO_BORDER);
                PdfPCell columnRetailerInfo = new PdfPCell();
                columnRetailerInfo.addElement(new Paragraph(StringUtils.capitalize(retailer.getAddress().getName()), FONT_BOLD));
                columnRetailerInfo.addElement(new Paragraph(StringUtils.capitalize(retailer.getAddress().getLine1()) + ", " + StringUtils.capitalize(retailer.getAddress().getLine2()) + ", " + StringUtils.capitalize(retailer.getAddress().getCity()) + "-" + retailer.getAddress().getPinCode() + ", " + retailer.getAddress().getState() + "(" + (stateGst ? pdfModel.getCustomerAddressStateCode() : pdfModel.getPartnerAddressStateCode()) + ")", FONT_BOLD));
                columnRetailerInfo.addElement(new Paragraph("Contact No.- " + retailer.getAddress().getPhoneNumber(), FONT_BOLD));
                columnRetailerInfo.addElement(new Paragraph("GST NO. " + retailer.getGstNumber(), FONT_BOLD));
                columnRetailerInfo.setBorder(Rectangle.NO_BORDER);

                PdfPTable tableInvoiceDateRetailer = new PdfPTable(1);
                tableInvoiceDateRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);

                PdfPTable tableInvoiceDate = new PdfPTable(2);
                tableInvoiceDate.getDefaultCell().setBorder(Rectangle.NO_BORDER);

                PdfPCell invoiceNumberKey = new PdfPCell(new Paragraph("Invoice No:", FONT_NORMAL));
                invoiceNumberKey.setBorder(Rectangle.NO_BORDER);

                PdfPCell invoiceNumberValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceNumber(), FONT_NORMAL));
                invoiceNumberValue.setBorder(Rectangle.NO_BORDER);

                PdfPCell dateKey = new PdfPCell(new Paragraph("Date:", FONT_NORMAL));
                dateKey.setBorder(Rectangle.NO_BORDER);

                PdfPCell dateValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceDate(), FONT_NORMAL));
                dateValue.setBorder(Rectangle.NO_BORDER);
                tableInvoiceDate.addCell(invoiceNumberKey);
                // tableInvoiceDate.addCell(blankCell);
                tableInvoiceDate.addCell(invoiceNumberValue);
                tableInvoiceDate.addCell(dateKey);
                // tableInvoiceDate.addCell(blankCell);
                tableInvoiceDate.addCell(dateValue);
                tableInvoiceDateRetailer.addCell(tableInvoiceDate);
                tableInvoiceDateRetailer.addCell(columnRetailerInfo);

                tableCustomerRetailer.addCell(columnCustomerInfo);
                tableCustomerRetailer.addCell(tableInvoiceDateRetailer);

                PdfPTable orders = null;
                if (stateGst) {
                    orders = new PdfPTable(stateWidthsWithDiscount.length);
                    orders.setWidths(stateWidthsWithDiscount);
                } else {
                    orders = new PdfPTable(igstWidthsWithDiscount.length);
                    orders.setWidths(igstWidthsWithDiscount);
                }
                orders.setWidthPercentage(90);
                orders.addCell(new Paragraph("Sl", FONT_BOLD));
                orders.addCell(new Paragraph("Description", FONT_BOLD));
                orders.addCell(new Paragraph("HSN", FONT_BOLD));
                orders.addCell(new Paragraph("Qty", FONT_BOLD));
                orders.addCell(new Paragraph("Rate\n(Per pc)", FONT_BOLD));
                orders.addCell(new Paragraph("Discount", FONT_BOLD));
                orders.addCell(new Paragraph("Total\nTaxable", FONT_BOLD));
                if (!stateGst) {
                    orders.addCell(new Paragraph("IGST", FONT_BOLD));
                    // orders.setWidths(new float[]{1, 3, 1, 1, 1, 1, 1, 1});
                    // total 8f
                } else {
                    orders.addCell(new Paragraph("CGST", FONT_BOLD));
                    orders.addCell(new Paragraph("SGST", FONT_BOLD));
                    // orders.setWidths(new float[]{1, 3, 1, 1, 1, 1, 1, 1, 1, 1});
                    // total 8f
                }
                orders.addCell(new Paragraph("Total", FONT_BOLD));

                // orders.addCell(new Paragraph("Item Total (Rs)", FONT_BOLD));

                orders.setHeaderRows(1);
                // orders.setSkipFirstHeader(true);

                float igstTotalAmount = 0, cgstTotalAmount = 0, sgstTotalAmount = 0;
                int index = 1;
                for (CustomOrderItem orderItem : orderItems) {
                    orders.addCell(new Paragraph(String.valueOf(index++), FONT_NORMAL));
                    orders.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
                    orders.addCell(new Paragraph(orderItem.getHsnCode(), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.valueOf(orderItem.getQuantity()), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getRate()), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getDiscount()), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getAmount()), FONT_NORMAL));
                    if (!stateGst) {
                        orders.addCell(new Paragraph(String.format("%.2f%n(@%.0f%%)", orderItem.getIgstAmount(), orderItem.getIgstRate()), FONT_NORMAL));
                        igstTotalAmount = igstTotalAmount + orderItem.getIgstAmount();
                    } else {
                        orders.addCell(new Paragraph(String.format("%.2f%n(@%.0f%%)", orderItem.getCgstAmount(), orderItem.getCgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f%n(@%.0f%%)", orderItem.getSgstAmount(), orderItem.getSgstRate()), FONT_NORMAL));
                        cgstTotalAmount = cgstTotalAmount + orderItem.getCgstAmount();
                        sgstTotalAmount = sgstTotalAmount + orderItem.getSgstAmount();
                    }
                    orders.addCell(new Paragraph(String.format("%.0f", orderItem.getNetAmount()), FONT_NORMAL));
                    // orders.addCell(new Paragraph(String.format("%.2f",
                    // orderItem.getItemTotal()), FONT_NORMAL));
                }
                if (pdfModel.getInsurancePolicies() != null) {
                    for (CustomInsurancePolicy insurancePolicy : pdfModel.getInsurancePolicies()) {
                        orders.addCell(new Paragraph(String.valueOf(index++), FONT_NORMAL));
                        orders.addCell(new Paragraph(insurancePolicy.getDescription(), FONT_NORMAL));
                        orders.addCell(new Paragraph(insurancePolicy.getHsnCode(), FONT_NORMAL));
                        orders.addCell(new Paragraph("1", FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph("-", FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getRate()), FONT_NORMAL));
                        if (!stateGst) {
                            orders.addCell(new Paragraph(String.format("%.2f%n(@%.0f%%)", insurancePolicy.getIgstAmount(), insurancePolicy.getIgstRate()), FONT_NORMAL));
                            igstTotalAmount = igstTotalAmount + insurancePolicy.getIgstAmount();
                        } else {
                            orders.addCell(new Paragraph(String.format("%.2f%n(@%.0f%%)", insurancePolicy.getCgstAmount(), insurancePolicy.getCgstRate()), FONT_NORMAL));
                            orders.addCell(new Paragraph(String.format("%.2f%n(@%.0f%%)", insurancePolicy.getSgstAmount(), insurancePolicy.getSgstRate()), FONT_NORMAL));
                            cgstTotalAmount = cgstTotalAmount + insurancePolicy.getCgstAmount();
                            sgstTotalAmount = sgstTotalAmount + insurancePolicy.getSgstAmount();
                        }
                        orders.addCell(new Paragraph(String.format("%.0f", insurancePolicy.getNetAmount()), FONT_NORMAL));
                    }
                }

                iconImg.setAbsolutePosition(25, rectangle.getHeight() - 100);
                iconImg.scalePercent(30);

                document.add(iconImg);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(tableCustomerRetailer);

                document.add(Chunk.NEWLINE);
                document.add(orders);

                PdfPTable grandTotalTable = new PdfPTable(3);
                PdfPTable paymentsTable = new PdfPTable(2);
                paymentsTable.setWidthPercentage(95);
                paymentsTable.setWidths(new float[]{8f, 2f});

                if (stateGst) {
                    grandTotalTable.setWidths(new float[]{6.6f, .6f, .8f});
                } else {
                    grandTotalTable.setWidths(new float[]{6.5f, .6f, .9f});
                }
                grandTotalTable.setWidthPercentage(90);

                Paragraph grandTotalParagraph = new Paragraph("Grand total", FONT_BOLD);
                grandTotalParagraph.setIndentationRight(20);
                grandTotalTable.addCell(grandTotalParagraph);
                Paragraph rsParagraph = new Paragraph("Rs.", FONT_BOLD);
                grandTotalTable.addCell(rsParagraph);
                Paragraph amountParagraph = new Paragraph(String.format("%.2f", pdfModel.getTotalAmount()), FONT_BOLD);
                grandTotalTable.addCell(amountParagraph);

                document.add(grandTotalTable);

                PdfPTable amountInWordsTable = new PdfPTable(3);
                amountInWordsTable.setWidthPercentage(90);
                amountInWordsTable.addCell(new Paragraph("Amount in Words:", FONT_BOLD));
                if (!stateGst) {
                    amountInWordsTable.setWidths(new float[]{2, 5.1f, 0.9f});
                } else {
                    amountInWordsTable.setWidths(new float[]{2, 5.2f, 0.8f});
                }

                String amountInWords = toAmountInWords(pdfModel.getTotalAmount());
                amountInWordsTable.addCell(new Paragraph(amountInWords.toString(), FONT_BOLD));
                amountInWordsTable.addCell(new Paragraph("E & O.E", FONT_NORMAL));
                document.add(amountInWordsTable);

                if (pdfModel.getPaymentOptions() != null) {
                    PdfPTable paidAmountTable = new PdfPTable(2);
                    paidAmountTable.setWidthPercentage(90);
                    if (!stateGst) {
                        paidAmountTable.setWidths(new float[]{7.1f, 0.9f});
                    } else {
                        paidAmountTable.setWidths(new float[]{7.2f, 0.8f});
                    }
                    float totalPaidValue = 0;
                    for (CustomPaymentOption paymentOption : pdfModel.getPaymentOptions()) {
                        LOGGER.info("paymentOption - {}", paymentOption);
                        if (!"CASH DISCOUNT".equals(paymentOption.getPaymentOption())) {
                            PdfPCell cell = new PdfPCell(
                                    new Paragraph(10, "Paid Through " + paymentOption.getPaymentOption(), FONT_BOLD));
                            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                            cell.setPadding(5);
                            paidAmountTable.addCell(cell);

                            PdfPCell cell1 = new PdfPCell(new Paragraph(10, FormattingUtils.formatDecimal(paymentOption.getAmount()), FONT_BOLD));
                            cell1.setPadding(5);
                            paidAmountTable.addCell(cell1);
                            totalPaidValue += paymentOption.getAmount();
                        }
                    }
                    PdfPCell totalPaidCell = new PdfPCell(new Paragraph(10, "Total Paid", FONT_BOLD));
                    totalPaidCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    totalPaidCell.setPadding(5);
                    paidAmountTable.addCell(totalPaidCell);

                    PdfPCell totalPaidValueCell = new PdfPCell(
                            new Paragraph(10, FormattingUtils.formatDecimal(totalPaidValue), FONT_BOLD));
                    totalPaidValueCell.setPadding(5);
                    paidAmountTable.addCell(totalPaidValueCell);

                    document.add(paidAmountTable);
                }
                Paragraph autoGenerateParagraph = new Paragraph("Note - This is computer generated Invoice, no signature is required", FONT_NORMAL);
                autoGenerateParagraph.setAlignment(Element.ALIGN_CENTER);
                document.add(autoGenerateParagraph);

                if(pdfModel.getCreditTerms()!=null) {
                    // Use monospaced Courier font to preserve spaces/indentation

                    // Title
                    Paragraph title = new Paragraph("Credit terms :-\n", FONT_BOLD);
                    title.setIndentationLeft(25);
                    title.setIndentationRight(25);
                    document.add(title);

                    // Exact text block (with spaces preserved)
                    StringBuffer termsBuffer = new StringBuffer();
                    int count = 0;
                    for (String creditTerm : pdfModel.getCreditTerms()) {
                        count++;
                        termsBuffer.append(count).append(". ").append(creditTerm).append(".\n");
                    }


                    Paragraph body = new Paragraph(termsBuffer.toString(), FONT_NORMAL);
                    body.setIndentationLeft(25);
                    body.setIndentationRight(25);
                    document.add(body);

                }
                if (pdfModel.getTncs() != null) {
                    StringBuffer sb = new StringBuffer();
                    for (String tnc : pdfModel.getTncs()) {
                        sb.append(tnc).append("\n");
                    }
                    Paragraph warningParagraph = new Paragraph(sb.toString(), FONT_NORMAL);
                    warningParagraph.setIndentationLeft(40);
                    document.add(Chunk.NEWLINE);
                    document.add(warningParagraph);
                }

                document.newPage();
                if (pdfModel.geteWayBillPdfModel() != null) {

                    EWayBillPDF.generateDocument(document, pdfWriter, pdfModel.geteWayBillPdfModel());
                }
            }
            document.close(); // no need to close PDFwriter?
            if (cancelledPages) {
                stampCancelled(outputStream, caneclledPageList);
            }

        } catch (DocumentException e) {
            LOGGER.error("Unable to write data to pdf file : ", e);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static void addIrnDetails(IrnModel irnModel, Rectangle rectangle, Document document) throws IOException, DocumentException {
        PdfPTable taxTable = new PdfPTable(1);
        taxTable.setWidthPercentage(45);
        PdfPCell pdfCell = new PdfPCell();
        pdfCell.setBorder(Rectangle.NO_BORDER);
        Image img = Image.getInstance(irnModel.getQrCode().toURI().toURL());
        img.setAbsolutePosition(450f, rectangle.getHeight() - 140);
        document.add(img);
        Paragraph irnParagraph = new Paragraph("IRN No - " + irnModel.getIrnNumber(), FONT_NORMAL);
        irnParagraph.setAlignment(Element.ALIGN_LEFT);
        pdfCell.addElement(irnParagraph);

        Paragraph ackParagraph = new Paragraph("Ack No - " + irnModel.getAcknowledgeNumber(), FONT_NORMAL);
        ackParagraph.setAlignment(Element.ALIGN_LEFT);
        pdfCell.addElement(ackParagraph);

        Paragraph ackDateParagraph = new Paragraph("Ack Date - " + FormattingUtils.format(irnModel.getAcknowledgeDate()), FONT_NORMAL);
        ackDateParagraph.setAlignment(Element.ALIGN_LEFT);
        pdfCell.addElement(ackDateParagraph);
        taxTable.addCell(pdfCell);
        document.add(taxTable);
    }

    private static void stampCancelled(ByteArrayOutputStream byteStream, List<Integer> cancelledPage) throws IOException, DocumentException {
        ByteArrayInputStream bais = new ByteArrayInputStream(byteStream.toByteArray());
        PdfReader pdfReader = new PdfReader(bais);
        int n = pdfReader.getNumberOfPages();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfStamper pdfStamper = new PdfStamper(pdfReader, baos);
        pdfStamper.setRotateContents(false);
        // text watermark
        Font f = new Font(FontFamily.HELVETICA, 30);
        Phrase p = new Phrase("My watermark (text)", f);
        URL cancelledImgUrl = PdfUtils.class.getClassLoader().getResource("cancelled.png");
        URL waterMarkImgUrl = PdfUtils.class.getClassLoader().getResource("sd1.jpg");
        Image imgCancelled = Image.getInstance(cancelledImgUrl);
        Image imgWatermark = Image.getInstance(waterMarkImgUrl);
        imgWatermark.scaleAbsolute(imgWatermark.getScaledWidth() * 2.5f, imgWatermark.getScaledHeight() * 2.5f);
        float w = imgCancelled.getScaledWidth() / 2;
        float h = imgCancelled.getScaledHeight() / 2;
        float wWaterMark = imgWatermark.getScaledWidth() / 2;
        float hWatermark = imgWatermark.getScaledHeight() / 2;
        // transparency
        PdfGState gs1 = new PdfGState();
        gs1.setFillOpacity(0.4f);
        PdfGState gs2 = new PdfGState();
        gs2.setFillOpacity(0.05f);
        // properties
        PdfContentByte over;
        Rectangle pagesize;
        float x, y;

        // loop over every page
        for (int i = 1; i <= n; i++) {
            pagesize = pdfReader.getPageSize(i);
            x = (pagesize.getLeft() + pagesize.getRight()) / 2;
            y = (pagesize.getTop() + pagesize.getBottom()) / 2;
            over = pdfStamper.getOverContent(i);
            over.saveState();
            if (cancelledPage.get(i - 1) == 1) {
                over.setGState(gs1);
                over.addImage(imgCancelled, w, 0, 0, h, x - (w / 2), y - (h / 2));
                over.restoreState();
            } else {
                over.setGState(gs2);
                over.addImage(imgWatermark, wWaterMark, 0, 0, hWatermark, x - (wWaterMark / 2), y - (hWatermark / 2));
                over.restoreState();
            }
        }
        pdfStamper.close();
        pdfReader.close();
        baos.writeTo(byteStream);

    }

    public static void generateAndWriteDebitNote(List<DebitNotePdfModel> debitNotePdfModels, OutputStream outputStream) {
        Document document = new Document();
        document.setMargins(0, 0, 25, 0);
        try {
            for (DebitNotePdfModel debitNotePdfModel : debitNotePdfModels) {

                InvoicePdfModel pdfModel = debitNotePdfModel.getPdfModel();
                CustomCustomer customer = pdfModel.getCustomer();
                CustomRetailer retailer = pdfModel.getRetailer();
                boolean stateGst = false;

                LOGGER.info("Customer - {}", customer.getAddress().getState());
                LOGGER.info("retailer - {}", retailer.getAddress().getState());


                if (customer.getAddress().getState().equals(retailer.getAddress().getState())) {
                    stateGst = true;
                }
                List<CustomOrderItem> orderItems = pdfModel.getOrderItems();

                PdfWriter.getInstance(document, outputStream);

                document.open();
                document.addTitle(pdfModel.getTitle());
                document.addAuthor(pdfModel.getAuther());

                Paragraph paragraphTitle = new Paragraph(pdfModel.getTitle(), FONT_TITLE);
                paragraphTitle.setAlignment(Element.ALIGN_CENTER);

                PdfPCell blankCell = new PdfPCell();
                blankCell.setBorder(Rectangle.NO_BORDER);
                PdfPTable tableCustomerRetailer = new PdfPTable(3);
                tableCustomerRetailer.setWidthPercentage(95);
                PdfPCell partnerInfo = new PdfPCell();
                partnerInfo.addElement(new Paragraph("From Party:", FONT_BOLD));
                partnerInfo.addElement(
                        new Paragraph(StringUtils.capitalize(customer.getAddress().getName()), FONT_NORMAL));
                partnerInfo.addElement(new Paragraph(
                        (customer.getAddress().getLine1() == null ? "" : StringUtils.capitalize(customer.getAddress().getLine1()) + ", ") + (customer.getAddress().getLine2() == null ? "" : StringUtils.capitalize(customer.getAddress().getLine2()) + ", ") + (customer.getAddress().getCity() == null ? "" : StringUtils.capitalize(customer.getAddress().getCity()) + " - ") + (customer.getAddress().getPinCode() == null ? "" : StringUtils.capitalize(customer.getAddress().getPinCode())), FONT_NORMAL));

                partnerInfo.addElement(new Paragraph(
                        StringUtils.capitalize(customer.getAddress().getState()) + "(" + pdfModel.getCustomerAddressStateCode() + ")", FONT_NORMAL));
                partnerInfo.addElement(new Paragraph("Mobile - " + customer.getMobileNumber(), FONT_NORMAL));
                if (customer.getGstNumber() != null && !customer.getGstNumber().isEmpty()) {
                    partnerInfo.addElement(new Paragraph("GST No - " + customer.getGstNumber(), FONT_BOLD));
                }

                PdfPCell sellerParty = new PdfPCell();
                sellerParty.addElement(new Paragraph("To Party:", FONT_BOLD));
                sellerParty.addElement(
                        new Paragraph(StringUtils.capitalize(retailer.getAddress().getName()), FONT_NORMAL));
                sellerParty.addElement(new Paragraph((retailer.getAddress().getLine1() == null ? "" : StringUtils.capitalize(retailer.getAddress().getLine1()) + ", ") + (retailer.getAddress().getLine2() == null ? "" : StringUtils.capitalize(retailer.getAddress().getLine2()) + ", ") + (retailer.getAddress().getCity() == null ? "" : StringUtils.capitalize(retailer.getAddress().getCity()) + "-") + (retailer.getAddress().getPinCode() == null ? "" : StringUtils.capitalize(retailer.getAddress().getPinCode())), FONT_NORMAL));
                sellerParty.addElement(new Paragraph(
                        retailer.getAddress().getState() + "(" + pdfModel.getPartnerAddressStateCode() + ")", FONT_NORMAL));
                sellerParty.addElement(new Paragraph("Mobile - " + retailer.getAddress().getPhoneNumber(), FONT_NORMAL));
                sellerParty.addElement(new Paragraph("GST No - " + retailer.getGstNumber(), FONT_BOLD));

                PdfPTable tableInvoiceDateRetailer = new PdfPTable(1);
                tableInvoiceDateRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);

                PdfPTable tableInvoiceDate = new PdfPTable(2);
                tableInvoiceDate.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                tableInvoiceDate.setWidthPercentage(90);

                PdfPCell debitNoteDetails = new PdfPCell(new Paragraph("Debit Note Details", FONT_BOLD));
                debitNoteDetails.setColspan(2);
                debitNoteDetails.setBorder(Rectangle.NO_BORDER);

                PdfPCell debitNoteNumberKey = new PdfPCell(new Paragraph("Debit Note No:", FONT_NORMAL));
                debitNoteNumberKey.setBorder(Rectangle.NO_BORDER);
                PdfPCell debitNoteNumberValue = new PdfPCell(
                        new Paragraph(debitNotePdfModel.getDebitNoteNumber(), FONT_NORMAL));
                debitNoteNumberValue.setBorder(Rectangle.NO_BORDER);

                PdfPCell debitNoteDateKey = new PdfPCell(new Paragraph("Debit Note Dt:", FONT_NORMAL));
                debitNoteDateKey.setBorder(Rectangle.NO_BORDER);

                PdfPCell debitNoteDateValue = new PdfPCell(
                        new Paragraph(debitNotePdfModel.getDebitNoteDate(), FONT_NORMAL));
                debitNoteDateValue.setBorder(Rectangle.NO_BORDER);

                PdfPCell invoiceNumberKey = new PdfPCell(new Paragraph("Invoice Ref No:", FONT_NORMAL));
                invoiceNumberKey.setBorder(Rectangle.NO_BORDER);
                PdfPCell invoiceNumberValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceNumber(), FONT_NORMAL));
                invoiceNumberValue.setBorder(Rectangle.NO_BORDER);

                PdfPCell dateKey = new PdfPCell(new Paragraph("Invoice Dt:", FONT_NORMAL));
                dateKey.setBorder(Rectangle.NO_BORDER);
                PdfPCell dateValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceDate(), FONT_NORMAL));
                dateValue.setBorder(Rectangle.NO_BORDER);

                tableInvoiceDate.addCell(debitNoteDetails);
                tableInvoiceDate.addCell(debitNoteNumberKey);
                tableInvoiceDate.addCell(debitNoteNumberValue);
                tableInvoiceDate.addCell(debitNoteDateKey);
                tableInvoiceDate.addCell(debitNoteDateValue);
                tableInvoiceDate.addCell(invoiceNumberKey);
                tableInvoiceDate.addCell(invoiceNumberValue);
                tableInvoiceDate.addCell(dateKey);
                tableInvoiceDate.addCell(dateValue);

                tableCustomerRetailer.addCell(partnerInfo);
                tableCustomerRetailer.addCell(tableInvoiceDate);
                tableCustomerRetailer.addCell(sellerParty);

                PdfPTable orders = null;
                if (stateGst) {
                    orders = new PdfPTable(stateWidths.length);
                    orders.setWidths(stateWidths);
                } else {
                    orders = new PdfPTable(igstWidths.length);
                    orders.setWidths(igstWidths);
                }
                orders.setWidthPercentage(95);
                orders.addCell(new Paragraph("Order Id", FONT_BOLD));
                orders.addCell(new Paragraph("Description", FONT_BOLD));
                orders.addCell(new Paragraph("HSN", FONT_BOLD));
                orders.addCell(new Paragraph("Qty", FONT_BOLD));
                orders.addCell(new Paragraph("Rate\n(Per pc)", FONT_BOLD));
                orders.addCell(new Paragraph("Total\nTaxable", FONT_BOLD));
                if (!stateGst) {
                    orders.addCell(new Paragraph("IGST\n%", FONT_BOLD));
                    orders.addCell(new Paragraph("IGST", FONT_BOLD));
                    // orders.setWidths(new float[]{1, 3, 1, 1, 1, 1, 1, 1});
                    // total 8f
                } else {
                    orders.addCell(new Paragraph("CGST %", FONT_BOLD));
                    orders.addCell(new Paragraph("CGST", FONT_BOLD));
                    orders.addCell(new Paragraph("SGST %", FONT_BOLD));
                    orders.addCell(new Paragraph("SGST", FONT_BOLD));
                    // orders.setWidths(new float[]{1, 3, 1, 1, 1, 1, 1, 1, 1,
                    // 1});
                    // total 8f
                }
                orders.addCell(new Paragraph("Total", FONT_BOLD));

                orders.setHeaderRows(1);

                float igstTotalAmount = 0, cgstTotalAmount = 0, sgstTotalAmount = 0;
                for (CustomOrderItem orderItem : orderItems) {
                    orders.addCell(new Paragraph(String.valueOf(orderItem.getOrderId()), FONT_NORMAL));
                    orders.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
                    orders.addCell(new Paragraph(orderItem.getHsnCode(), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.valueOf(orderItem.getQuantity()), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getRate()), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getAmount()), FONT_NORMAL));
                    if (!stateGst) {
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstAmount()), FONT_NORMAL));
                        igstTotalAmount = igstTotalAmount + orderItem.getIgstAmount();
                    } else {
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstAmount()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstAmount()), FONT_NORMAL));
                        cgstTotalAmount = cgstTotalAmount + orderItem.getCgstAmount();
                        sgstTotalAmount = sgstTotalAmount + orderItem.getSgstAmount();
                    }
                    orders.addCell(new Paragraph(String.format("%.0f", orderItem.getNetAmount()), FONT_NORMAL));
                    LOGGER.info("IN FOR LOOP");
                }

                document.add(paragraphTitle);

                document.add(Chunk.NEWLINE);
                document.add(tableCustomerRetailer);

                document.add(Chunk.NEWLINE);
                document.add(orders);

                PdfPTable grandTotalTable = new PdfPTable(3);
                if (stateGst) {
                    grandTotalTable.setWidths(new float[]{6.6f, .6f, .8f});
                } else {
                    grandTotalTable.setWidths(new float[]{6.5f, .6f, .9f});
                }
                grandTotalTable.setWidthPercentage(95);

                Paragraph grandTotalParagraph = new Paragraph("Grand total", FONT_BOLD);
                grandTotalParagraph.setIndentationRight(20);
                grandTotalTable.addCell(grandTotalParagraph);
                Paragraph rsParagraph = new Paragraph("Rs.", FONT_BOLD);
                grandTotalTable.addCell(rsParagraph);
                Paragraph amountParagraph = new Paragraph(String.format("%.2f", pdfModel.getTotalAmount()), FONT_BOLD);
                grandTotalTable.addCell(amountParagraph);

                document.add(grandTotalTable);

                PdfPTable amountInWordsTable = new PdfPTable(3);
                if (!stateGst) {
                    amountInWordsTable.setWidths(new float[]{2, 5.1f, 0.9f});
                } else {
                    amountInWordsTable.setWidths(new float[]{2, 5.2f, 0.8f});
                }
                amountInWordsTable.setWidthPercentage(95);
                amountInWordsTable.addCell(new Paragraph("Amount in Words:", FONT_BOLD));

                String amountInWords = toAmountInWords(pdfModel.getTotalAmount());
                amountInWordsTable.addCell(new Paragraph(amountInWords.toString(), FONT_BOLD));
                amountInWordsTable.addCell(new Paragraph("E & O.E", FONT_NORMAL));
                document.add(amountInWordsTable);

                document.newPage();
            }
            document.close(); // no need to close PDFwriter?

        } catch (DocumentException e) {
            LOGGER.error("Unable to write data to pdf file : ", e);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static String toAmountInWords(float amount) {
        RuleBasedNumberFormat amountInWordsFormat = new RuleBasedNumberFormat(indianLocale, RuleBasedNumberFormat.SPELLOUT);
        StringBuilder amountInWords = new StringBuilder("Rs. ");
        amountInWords.append(StringUtils.capitalize(amountInWordsFormat.format((int) amount)));
        amountInWords.append(" and ");
        amountInWords.append(StringUtils.capitalize(amountInWordsFormat.format((int) (amount * 100) % 100)));
        amountInWords.append(" paise");
        return amountInWords.toString();
    }

    public static void generateAndWriteCustomerCreditNotes(List<CreditNotePdfModel> creditNotes, OutputStream outputStream) {
        Document document = new Document();
        document.setMargins(0, 0, 25, 0);
        try {
            PdfWriter.getInstance(document, outputStream);

            document.open();
            document.addTitle(creditNotes.get(0).getPdfModel().getTitle());
            document.addAuthor(creditNotes.get(0).getPdfModel().getAuther());
            for (CreditNotePdfModel creditNotePdfModel : creditNotes) {
                InvoicePdfModel pdfModel = creditNotePdfModel.getPdfModel();
                CustomCustomer customer = pdfModel.getCustomer();
                CustomRetailer retailer = pdfModel.getRetailer();
                boolean stateGst = false;

                if (customer.getAddress().getState().equals(retailer.getAddress().getState())) {
                    stateGst = true;
                }
                List<CustomOrderItem> orderItems = pdfModel.getOrderItems();

                Paragraph paragraphTitle = new Paragraph(pdfModel.getTitle(), FONT_TITLE);
                paragraphTitle.setAlignment(Element.ALIGN_CENTER);

                PdfPCell blankCell = new PdfPCell();
                blankCell.setBorder(Rectangle.NO_BORDER);
                PdfPTable tableCustomerRetailer = new PdfPTable(3);
                tableCustomerRetailer.setWidthPercentage(95);
                PdfPCell partnerInfo = new PdfPCell();
                partnerInfo.addElement(new Paragraph("To Party:", FONT_BOLD));
                partnerInfo.addElement(
                        new Paragraph(StringUtils.capitalize(customer.getAddress().getName()), FONT_NORMAL));
                partnerInfo.addElement(new Paragraph(
                        StringUtils.capitalize(customer.getAddress().getLine1()) + ", " + (customer.getAddress().getLine2() == null ? "" : StringUtils.capitalize(customer.getAddress().getLine2())) + ", " + customer.getAddress().getCity() + " - " + customer.getAddress().getPinCode(), FONT_NORMAL));

                partnerInfo.addElement(new Paragraph(
                        StringUtils.capitalize(customer.getAddress().getState()) + "(" + pdfModel.getCustomerAddressStateCode() + ")", FONT_NORMAL));
                partnerInfo.addElement(new Paragraph("Mobile - " + customer.getMobileNumber(), FONT_NORMAL));
                if (customer.getGstNumber() != null && !customer.getGstNumber().isEmpty()) {
                    partnerInfo.addElement(new Paragraph("GST No - " + customer.getGstNumber(), FONT_BOLD));
                }

                PdfPCell sellerParty = new PdfPCell();
                sellerParty.addElement(new Paragraph("From Party:", FONT_BOLD));
                sellerParty.addElement(
                        new Paragraph(StringUtils.capitalize(retailer.getAddress().getName()), FONT_NORMAL));
                sellerParty.addElement(new Paragraph((retailer.getAddress().getLine1() == null ? "" : StringUtils.capitalize(retailer.getAddress().getLine1()) + ", ") + (retailer.getAddress().getLine2() == null ? "" : StringUtils.capitalize(retailer.getAddress().getLine2()) + ", ") + StringUtils.capitalize(retailer.getAddress().getCity()) + "-" + retailer.getAddress().getPinCode() + ", ", FONT_NORMAL));
                sellerParty.addElement(new Paragraph(
                        retailer.getAddress().getState() + "(" + pdfModel.getPartnerAddressStateCode() + ")", FONT_NORMAL));
                sellerParty.addElement(new Paragraph("Mobile - " + retailer.getAddress().getPhoneNumber(), FONT_NORMAL));
                sellerParty.addElement(new Paragraph("GST No - " + retailer.getGstNumber(), FONT_BOLD));

                PdfPTable tableInvoiceDateRetailer = new PdfPTable(1);
                tableInvoiceDateRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);

                PdfPTable tableInvoiceDate = new PdfPTable(2);
                tableInvoiceDate.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                tableInvoiceDate.setWidthPercentage(90);

                PdfPCell debitNoteDetails = new PdfPCell(new Paragraph("Credit Note Details", FONT_BOLD));
                debitNoteDetails.setColspan(2);
                debitNoteDetails.setBorder(Rectangle.NO_BORDER);

                PdfPCell debitNoteNumberKey = new PdfPCell(new Paragraph("Credit Note No:", FONT_NORMAL));
                debitNoteNumberKey.setBorder(Rectangle.NO_BORDER);
                PdfPCell debitNoteNumberValue = new PdfPCell(
                        new Paragraph(creditNotePdfModel.getCreditNoteNumber(), FONT_NORMAL));
                debitNoteNumberValue.setBorder(Rectangle.NO_BORDER);

                PdfPCell debitNoteDateKey = new PdfPCell(new Paragraph("Credit Note Dt:", FONT_NORMAL));
                debitNoteDateKey.setBorder(Rectangle.NO_BORDER);

                PdfPCell debitNoteDateValue = new PdfPCell(
                        new Paragraph(creditNotePdfModel.getCreditNoteDate(), FONT_NORMAL));
                debitNoteDateValue.setBorder(Rectangle.NO_BORDER);
                tableInvoiceDate.addCell(debitNoteDetails);
                tableInvoiceDate.addCell(debitNoteNumberKey);
                tableInvoiceDate.addCell(debitNoteNumberValue);
                tableInvoiceDate.addCell(debitNoteDateKey);
                tableInvoiceDate.addCell(debitNoteDateValue);
                if (pdfModel.getInvoiceNumber() != null) {
                    PdfPCell dateKey = new PdfPCell(new Paragraph("Invoice Dt:", FONT_NORMAL));
                    dateKey.setBorder(Rectangle.NO_BORDER);
                    PdfPCell dateValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceDate(), FONT_NORMAL));
                    dateValue.setBorder(Rectangle.NO_BORDER);

                    PdfPCell invoiceNumberKey = new PdfPCell(new Paragraph("Invoice Ref No:", FONT_NORMAL));
                    invoiceNumberKey.setBorder(Rectangle.NO_BORDER);
                    PdfPCell invoiceNumberValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceNumber(), FONT_NORMAL));
                    invoiceNumberValue.setBorder(Rectangle.NO_BORDER);
                    tableInvoiceDate.addCell(invoiceNumberKey);
                    tableInvoiceDate.addCell(invoiceNumberValue);
                    tableInvoiceDate.addCell(dateKey);
                    tableInvoiceDate.addCell(dateValue);
                }

                tableCustomerRetailer.addCell(partnerInfo);
                tableCustomerRetailer.addCell(tableInvoiceDate);
                tableCustomerRetailer.addCell(sellerParty);

                PdfPTable orders = null;
                if (stateGst) {
                    orders = new PdfPTable(stateWidthsCrNote.length);
                    orders.setWidths(stateWidthsCrNote);
                } else {
                    orders = new PdfPTable(igstWidthsCrNote.length);
                    orders.setWidths(igstWidthsCrNote);
                }
                orders.setWidthPercentage(95);
                orders.addCell(new Paragraph("Description", FONT_BOLD));
                orders.addCell(new Paragraph("HSN", FONT_BOLD));
                orders.addCell(new Paragraph("Qty", FONT_BOLD));
                orders.addCell(new Paragraph("Rate\n(Per pc)", FONT_BOLD));
                orders.addCell(new Paragraph("Total\nTaxable", FONT_BOLD));
                if (!stateGst) {
                    orders.addCell(new Paragraph("IGST%", FONT_BOLD));
                    orders.addCell(new Paragraph("IGST", FONT_BOLD));
                    // orders.setWidths(new float[]{1, 3, 1, 1, 1, 1, 1, 1});
                    // total 8f
                } else {
                    orders.addCell(new Paragraph("CGST %", FONT_BOLD));
                    orders.addCell(new Paragraph("CGST", FONT_BOLD));
                    orders.addCell(new Paragraph("SGST %", FONT_BOLD));
                    orders.addCell(new Paragraph("SGST", FONT_BOLD));
                    // orders.setWidths(new float[]{1, 3, 1, 1, 1, 1, 1, 1, 1,
                    // 1});
                    // total 8f
                }
                orders.addCell(new Paragraph("Total", FONT_BOLD));

                orders.setHeaderRows(1);

                float igstTotalAmount = 0, cgstTotalAmount = 0, sgstTotalAmount = 0;
                for (CustomOrderItem orderItem : orderItems) {
                    LOGGER.info("Custom Order Item - {}", orderItem);
                    orders.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
                    orders.addCell(new Paragraph(orderItem.getHsnCode(), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.valueOf(orderItem.getQuantity()), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getRate()), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getAmount()), FONT_NORMAL));
                    if (!stateGst) {
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstAmount()), FONT_NORMAL));
                        igstTotalAmount = igstTotalAmount + orderItem.getIgstAmount();
                    } else {
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstAmount()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstAmount()), FONT_NORMAL));
                        cgstTotalAmount = cgstTotalAmount + orderItem.getCgstAmount();
                        sgstTotalAmount = sgstTotalAmount + orderItem.getSgstAmount();
                    }
                    orders.addCell(new Paragraph(String.format("%.0f", orderItem.getNetAmount()), FONT_NORMAL));
                    LOGGER.info("IN FOR LOOP");
                }

                document.add(paragraphTitle);

                document.add(Chunk.NEWLINE);
                document.add(tableCustomerRetailer);

                document.add(Chunk.NEWLINE);
                document.add(orders);

                PdfPTable grandTotalTable = new PdfPTable(3);
                if (stateGst) {
                    grandTotalTable.setWidths(new float[]{6.6f, .6f, .8f});
                } else {
                    grandTotalTable.setWidths(new float[]{6.5f, .6f, .9f});
                }
                grandTotalTable.setWidthPercentage(95);

                Paragraph grandTotalParagraph = new Paragraph("Grand total", FONT_BOLD);
                grandTotalParagraph.setIndentationRight(20);
                grandTotalTable.addCell(grandTotalParagraph);
                Paragraph rsParagraph = new Paragraph("Rs.", FONT_BOLD);
                grandTotalTable.addCell(rsParagraph);
                Paragraph amountParagraph = new Paragraph(String.format("%.2f", pdfModel.getTotalAmount()), FONT_BOLD);
                grandTotalTable.addCell(amountParagraph);

                document.add(grandTotalTable);

                PdfPTable amountInWordsTable = new PdfPTable(3);
                if (!stateGst) {
                    amountInWordsTable.setWidths(new float[]{2, 5.1f, 0.9f});
                } else {
                    amountInWordsTable.setWidths(new float[]{2, 5.2f, 0.8f});
                }
                amountInWordsTable.setWidthPercentage(95);
                amountInWordsTable.addCell(new Paragraph("Amount in Words:", FONT_BOLD));

                String amountInWords = toAmountInWords(pdfModel.getTotalAmount());
                amountInWordsTable.addCell(new Paragraph(amountInWords.toString(), FONT_BOLD));
                amountInWordsTable.addCell(new Paragraph("E & O.E", FONT_NORMAL));
                document.add(amountInWordsTable);

                document.newPage();
            }
            document.close(); // no need to close PDFwriter?
        } catch (DocumentException e) {
            LOGGER.error("Unable to write data to pdf file : ", e);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static byte[] mergePdfFiles(List<byte[]> pdfFiles) throws IOException, DocumentException {
        ByteArrayOutputStream mergedOutputStream = new ByteArrayOutputStream();
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, mergedOutputStream);
        document.open();

        for (byte[] pdf : pdfFiles) {
            PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf));
            int n = reader.getNumberOfPages();
            for (int i = 1; i <= n; i++) {
                copy.addPage(copy.getImportedPage(reader, i));
            }
            copy.freeReader(reader);
            reader.close();
        }

        document.close();
        return mergedOutputStream.toByteArray();
    }

}