Subversion Repositories SmartDukaan

Rev

Rev 33741 | 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();
        }
    }
    public static void generateDocument(Document document, PdfWriter writer, EWayBillPdfModel eWayBillPdfModel) {

        try {
            // Set up document and writer
            /*PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("EWayBill.pdf"));
            document.open();*/

            // Load custom font (replace with the path to your specific font if you have it)
            BaseFont customFontBase = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
            Font customFont = new Font(customFontBase, 12);
            Font customBoldFont = new Font(customFontBase, 12, Font.BOLD);

            // Title
            Paragraph titleParagraph = new Paragraph("E-Way Bill System", new Font(customFontBase, 16, Font.BOLD));
            titleParagraph.setAlignment(Element.ALIGN_CENTER);
            document.add(titleParagraph);

            document.add(new Paragraph(" ", customFont)); // Empty space

            // E-Way Bill Info
            PdfPTable table = new PdfPTable(2);
            table.setWidths(new float[]{1, 2});
            table.setWidthPercentage(90);

            table.addCell(getCell("E-Way Bill No:", customBoldFont));
            table.addCell(getCell(eWayBillPdfModel.getEwbNumber(), customFont));

            table.addCell(getCell("E-Way Bill Date:", customBoldFont));
            table.addCell(getCell(eWayBillPdfModel.getEwbDate(), customFont));

            table.addCell(getCell("Generated By:", customBoldFont));
            table.addCell(getCell(eWayBillPdfModel.getGeneratedBy(), customFont));

            table.addCell(getCell("Valid From:", customBoldFont));
            table.addCell(getCell(eWayBillPdfModel.getValidFrom(), customFont));

            table.addCell(getCell("Valid Until:", customBoldFont));
            table.addCell(getCell(eWayBillPdfModel.getValidUntil(), customFont));

            document.add(table);

            document.add(new Paragraph(" ", customFont)); // Empty space

            // Part A
            PdfPTable partATable = new PdfPTable(2);
            partATable.setWidths(new float[]{1, 2});
            partATable.setWidthPercentage(90);
            partATable.setSpacingBefore(5);
            EWBPartAModel partAModel = eWayBillPdfModel.getEwbPartAModel();
            PdfPCell partACell = getCell("Part A", new Font(customFontBase, 14, Font.BOLD));
            partACell.setColspan(2);
            partATable.addCell(partACell);
            partATable.addCell(getCell("GSTIN of Supplier:", customBoldFont));
            partATable.addCell(getCell(partAModel.getSupplierGstin(), customFont));

            partATable.addCell(getCell("Place of Dispatch:", customBoldFont));
            partATable.addCell(getCell(partAModel.getPlaceOfDispatch(), customFont));

            partATable.addCell(getCell("GSTIN of Recipient:", customBoldFont));
            partATable.addCell(getCell(partAModel.getRecipientGstin(), customFont));

            partATable.addCell(getCell("Place of Delivery:", customBoldFont));
            partATable.addCell(getCell(partAModel.getPlaceOfDelivery(), customFont));

            partATable.addCell(getCell("Document No:", customBoldFont));
            partATable.addCell(getCell(partAModel.getDocumentNumber(), customFont));

            partATable.addCell(getCell("Document Date:", customBoldFont));
            partATable.addCell(getCell(partAModel.getDocumentDate(), customFont));

            partATable.addCell(getCell("Transaction Type:", customBoldFont));
            partATable.addCell(getCell(partAModel.getTransactType(), customFont));

            partATable.addCell(getCell("Value of Goods:", customBoldFont));
            partATable.addCell(getCell(partAModel.getValueOfGoods(), customFont));

            partATable.addCell(getCell("HSN Code(s):", customBoldFont));
            partATable.addCell(getCell(partAModel.getHsnCode(), customFont));

            partATable.addCell(getCell("Reason for Transportation:", customBoldFont));
            partATable.addCell(getCell(partAModel.getReasonForTransportation(), customFont));

            if(partAModel.getTransporter() != null ){
                partATable.addCell(getCell("Transporter", customBoldFont));
                partATable.addCell(getCell(partAModel.getTransporter(), customFont));

            }

            document.add(partATable);

            document.add(new Paragraph(" ", customFont)); // Empty space

            // Part B
            if (eWayBillPdfModel.getEwbPartBModel() != null) {
                // Part B Table
                PdfPCell partBCell = getCell("Part B", new Font(customFontBase, 14, Font.BOLD));
                partBCell.setColspan(7);
                PdfPTable partBTable = new PdfPTable(7); // 7 columns
                partBTable.setWidths(new float[]{2, 3, 3, 4, 4, 2, 2});
                partBTable.setWidthPercentage(90);
                partBTable.setSpacingBefore(5f);
                partBTable.setSpacingAfter(10f);

                // Header Row
                partBTable.addCell(partBCell);
                Font fontNew = new Font(customFontBase, 10, Font.NORMAL);
                Font fontNewBold = new Font(customFontBase, 10, Font.BOLD);
                partBTable.addCell(getCell("Mode", fontNewBold));
                partBTable.addCell(getCell("Vehicle No.", fontNewBold));
                partBTable.addCell(getCell("From", fontNewBold));
                partBTable.addCell(getCell("Entered Date", fontNewBold));
                partBTable.addCell(getCell("Entered By", fontNewBold));
                partBTable.addCell(getCell("CEWB No.", fontNewBold));
                partBTable.addCell(getCell("Multi Veh.Info", fontNewBold));

                // Data Row
                EWBPartBModel partBModel = eWayBillPdfModel.getEwbPartBModel();
                partBTable.addCell(getCell(partBModel.getMode(), fontNew));
                partBTable.addCell(getCell(partBModel.getVehicleNumber(), fontNew));
                partBTable.addCell(getCell(partBModel.getFrom(), fontNew));
                partBTable.addCell(getCell(partBModel.getEnteredDate(), fontNew));
                partBTable.addCell(getCell(partBModel.getEnteredBy(), fontNew));
                partBTable.addCell(getCell("-", fontNew));
                partBTable.addCell(getCell("-", fontNew)); // Assuming Multi Veh.Info is not provided

                document.add(partBTable);

                document.add(new Paragraph(" ", customFont)); // Empty space
            }

            // Add 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(150);
            document.add(new Paragraph(" ", customFont)); // Empty space
            document.add(barcodeImage);


            //document.add(new Paragraph("Note*: If any discrepancy in information please try after sometime.", customFont));


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

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