Subversion Repositories SmartDukaan

Rev

Rev 33742 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.spice.profitmandi.common.model.EWBPartAModel;
import com.spice.profitmandi.common.model.EWBPartBModel;
import com.spice.profitmandi.common.model.EWayBillPdfModel;

import java.io.FileOutputStream;


public class EWayBillPDF {

    public static void main(String[] args) {
        EWayBillPDF eWayBillPDF = new EWayBillPDF();
        EWayBillPdfModel eWayBillPdfModel = new EWayBillPdfModel();
        EWBPartAModel partAModel = new EWBPartAModel();
        eWayBillPdfModel.setEwbPartAModel(partAModel);
        eWayBillPDF.generateDocument(eWayBillPdfModel);

    }

    public static void generateDocument(EWayBillPdfModel eWayBillPdfModel) {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("EWayBill.pdf"));
            document.open();
            EWayBillPDF.generateDocument(document, writer, eWayBillPdfModel);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // Compact fonts matching invoice style
    private static final BaseColor EWB_BORDER = new BaseColor(204, 204, 204);
    private static final BaseColor EWB_HEADER_BG = new BaseColor(242, 242, 242);
    private static final Font EWB_TITLE = new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD);
    private static final Font EWB_SECTION = new Font(Font.FontFamily.HELVETICA, 8, Font.BOLD);
    private static final Font EWB_NORMAL = new Font(Font.FontFamily.HELVETICA, 7, Font.NORMAL);
    private static final Font EWB_BOLD = new Font(Font.FontFamily.HELVETICA, 7, Font.BOLD);

    public static void generateDocument(Document document, PdfWriter writer, EWayBillPdfModel eWayBillPdfModel) {
        try {
            // Title
            Paragraph titleParagraph = new Paragraph("E-Way Bill System", EWB_TITLE);
            titleParagraph.setAlignment(Element.ALIGN_CENTER);
            titleParagraph.setSpacingAfter(6f);
            document.add(titleParagraph);

            // E-Way Bill Info
            PdfPTable table = new PdfPTable(2);
            table.setWidths(new float[]{1.2f, 2.8f});
            table.setWidthPercentage(80);
            table.setSpacingAfter(6f);

            ewbRow(table, "E-Way Bill No:", eWayBillPdfModel.getEwbNumber());
            ewbRow(table, "E-Way Bill Date:", eWayBillPdfModel.getEwbDate());
            ewbRow(table, "Generated By:", eWayBillPdfModel.getGeneratedBy());
            ewbRow(table, "Valid From:", eWayBillPdfModel.getValidFrom());
            ewbRow(table, "Valid Until:", eWayBillPdfModel.getValidUntil());

            document.add(table);

            // Part A
            EWBPartAModel partAModel = eWayBillPdfModel.getEwbPartAModel();
            PdfPTable partATable = new PdfPTable(2);
            partATable.setWidths(new float[]{1.2f, 2.8f});
            partATable.setWidthPercentage(80);
            partATable.setSpacingAfter(6f);

            PdfPCell partAHeader = new PdfPCell(new Phrase("Part A", EWB_SECTION));
            partAHeader.setColspan(2);
            partAHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
            partAHeader.setBackgroundColor(EWB_HEADER_BG);
            partAHeader.setBorderColor(EWB_BORDER);
            partAHeader.setPadding(3f);
            partATable.addCell(partAHeader);

            ewbRow(partATable, "GSTIN of Supplier:", partAModel.getSupplierGstin());
            ewbRow(partATable, "Place of Dispatch:", partAModel.getPlaceOfDispatch());
            ewbRow(partATable, "GSTIN of Recipient:", partAModel.getRecipientGstin());
            ewbRow(partATable, "Place of Delivery:", partAModel.getPlaceOfDelivery());
            ewbRow(partATable, "Document No:", partAModel.getDocumentNumber());
            ewbRow(partATable, "Document Date:", partAModel.getDocumentDate());
            ewbRow(partATable, "Transaction Type:", partAModel.getTransactType());
            ewbRow(partATable, "Value of Goods:", partAModel.getValueOfGoods());
            ewbRow(partATable, "HSN Code(s):", partAModel.getHsnCode());
            ewbRow(partATable, "Reason for Transportation:", partAModel.getReasonForTransportation());
            if (partAModel.getTransporter() != null) {
                ewbRow(partATable, "Transporter:", partAModel.getTransporter());
            }

            document.add(partATable);

            // Part B
            if (eWayBillPdfModel.getEwbPartBModel() != null) {
                PdfPTable partBTable = new PdfPTable(7);
                partBTable.setWidths(new float[]{2, 3, 3, 4, 4, 2, 2});
                partBTable.setWidthPercentage(80);
                partBTable.setSpacingAfter(6f);

                PdfPCell partBHeader = new PdfPCell(new Phrase("Part B", EWB_SECTION));
                partBHeader.setColspan(7);
                partBHeader.setHorizontalAlignment(Element.ALIGN_CENTER);
                partBHeader.setBackgroundColor(EWB_HEADER_BG);
                partBHeader.setBorderColor(EWB_BORDER);
                partBHeader.setPadding(3f);
                partBTable.addCell(partBHeader);

                String[] headers = {"Mode", "Vehicle No.", "From", "Entered Date", "Entered By", "CEWB No.", "Multi Veh."};
                for (String h : headers) {
                    PdfPCell hc = new PdfPCell(new Phrase(h, EWB_BOLD));
                    hc.setHorizontalAlignment(Element.ALIGN_CENTER);
                    hc.setBorderColor(EWB_BORDER);
                    hc.setBackgroundColor(EWB_HEADER_BG);
                    hc.setPadding(2f);
                    partBTable.addCell(hc);
                }

                EWBPartBModel partBModel = eWayBillPdfModel.getEwbPartBModel();
                String[] data = {partBModel.getMode(), partBModel.getVehicleNumber(), partBModel.getFrom(),
                        partBModel.getEnteredDate(), partBModel.getEnteredBy(), "-", "-"};
                for (String d : data) {
                    PdfPCell dc = new PdfPCell(new Phrase(d != null ? d : "-", EWB_NORMAL));
                    dc.setHorizontalAlignment(Element.ALIGN_CENTER);
                    dc.setBorderColor(EWB_BORDER);
                    dc.setPadding(2f);
                    partBTable.addCell(dc);
                }

                document.add(partBTable);
            }

            // Barcode
            Barcode128 barcode = new Barcode128();
            barcode.setCode(eWayBillPdfModel.getEwbNumber());
            barcode.setCodeType(Barcode.CODE128);
            Image barcodeImage = barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.BLACK);
            barcodeImage.setAlignment(Element.ALIGN_CENTER);
            barcodeImage.scalePercent(120);
            document.add(barcodeImage);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void ewbRow(PdfPTable table, String label, String value) {
        PdfPCell lc = new PdfPCell(new Phrase(label, EWB_BOLD));
        lc.setHorizontalAlignment(Element.ALIGN_RIGHT);
        lc.setBorderColor(EWB_BORDER);
        lc.setPadding(3f);
        table.addCell(lc);
        PdfPCell vc = new PdfPCell(new Phrase(value != null ? value : "", EWB_NORMAL));
        vc.setBorderColor(EWB_BORDER);
        vc.setPadding(3f);
        table.addCell(vc);
    }

    private static PdfPCell getCell(String text, Font font) {
        PdfPCell cell = new PdfPCell(new Phrase(text, font));
        cell.setPadding(5);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setBorder(PdfPCell.BOX);
        return cell;
    }
}