Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.inventory.service;

import in.shop2020.model.v1.catalog.Item;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.utils.ModelUtils;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.impl.upcean.EAN13Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


public class ProductLabelGenerator{
        private static Logger logger = LoggerFactory
    .getLogger(ProductLabelGenerator.class);
        
        public static ByteArrayOutputStream generatePdfLabelSheet(String itemId, String itemNumber) throws IOException{
                ByteArrayOutputStream baosPDF = null;
                try {
                        CatalogClient cClient = new CatalogClient();
                in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = cClient.getClient();
                Item item = catalogClient.getItem(Long.parseLong(itemId));
                baosPDF = new ByteArrayOutputStream();

            Document document = new Document();
            PdfWriter.getInstance(document, baosPDF);
            document.addAuthor("shop2020");
            document.addTitle("Label for item: "
                    + itemId);
            document.open();

            PdfPTable poTable = new PdfPTable(new float[] { 0.3f, 0.05f, 0.3f, 0.05f, 0.3f});
            poTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            poTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            generateBarcode(ModelUtils.extractProductNameFromItem(item), "item_name_"+itemId, 4.0f);
            generateBarcode(itemNumber, "item_number"+itemId, 1.0f);
            
            Image itemNameImage=null;
                        try {
                                itemNameImage = Image.getInstance("/tmp/"+"item_name_"+itemId+".png");
                        } catch (Exception e) {
                                logger.error("Exception during getting Iten Name Image : ", e);
                        }
                        
                        Image itemNumberImage=null;
                        try {
                                itemNumberImage = Image.getInstance("/tmp/"+"item_number"+itemId+".png");
                        } catch (Exception e) {
                                logger.error("Exception during getting Iten Name Image : ", e);
                        }
            
                        for(int i=1;i<8;i++){
                                for(int j=1; j<=5; j++){
                                        if(j%2==0){
                                                poTable.addCell(" ");
                                        }else{
                                                PdfPTable poSubTable = new PdfPTable(1);
                                                poSubTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                                                poSubTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                                                poSubTable.setSpacingAfter(2.0f);
        
                                                poSubTable.addCell(itemNameImage);
        
                                                poSubTable.addCell(itemNumberImage);
        
                                                poTable.addCell(poSubTable);
                                        }
                                }
                                poTable.addCell("-------------------------");
                                poTable.addCell(" ");
                                poTable.addCell("-------------------------");
                                poTable.addCell(" ");
                                poTable.addCell("-------------------------");
                        }

            document.add(poTable);
            document.close();
            baosPDF.close();
        } catch (Exception e) {
            logger.error("Error while generating Invoice: ", e);
            e.printStackTrace();
        }
                String tmpDir = System.getProperty("java.io.tmpdir");
        String filename = tmpDir + "/item-" + itemId +"-" +System.currentTimeMillis() + ".pdf";
        File f = new File(filename);
        FileOutputStream fos = new FileOutputStream(f);
        baosPDF.writeTo(fos);
        return baosPDF;
        }
        
        /**
         * @param args
         * @throws IOException
         */
        /*
        public static void main(String[] args) throws IOException{
                ByteArrayOutputStream baosPDF = null;
                long itemId = 18267L;
        try {
                baosPDF = new ByteArrayOutputStream();

            Document document = new Document();
            PdfWriter.getInstance(document, baosPDF);
            document.addAuthor("shop2020");
            document.addTitle("Label for item: "
                    + itemId);
            document.open();

            PdfPTable poTable = new PdfPTable(new float[] { 0.3f, 0.05f, 0.3f, 0.05f, 0.3f});
            poTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            poTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            generateBarcode("Spice Mi -3330 Champ 1800 black", "item_name_"+itemId, 4.0f);
            generateBarcode("Pang42352637", "item_number"+itemId, 1.0f);
            
            Image itemNameImage=null;
                        try {
                                itemNameImage = Image.getInstance("/tmp/"+"item_name_"+itemId+".png");
                        } catch (Exception e) {
                                logger.error("Exception during getting Iten Name Image : ", e);
                        }
                        
                        Image itemNumberImage=null;
                        try {
                                itemNumberImage = Image.getInstance("/tmp/"+"item_number"+itemId+".png");
                        } catch (Exception e) {
                                logger.error("Exception during getting Iten Name Image : ", e);
                        }
            
                        for(int i=1;i<8;i++){
                                for(int j=1; j<=5; j++){
                                        if(j%2==0){
                                                poTable.addCell(" ");
                                        }else{
                                                PdfPTable poSubTable = new PdfPTable(1);
                                                poSubTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                                                poSubTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                                                poSubTable.setSpacingAfter(2.0f);
        
                                                poSubTable.addCell(itemNameImage);
        
                                                poSubTable.addCell(itemNumberImage);
        
                                                poTable.addCell(poSubTable);
                                        }
                                }
                                poTable.addCell("-------------------------");
                                poTable.addCell(" ");
                                poTable.addCell("-------------------------");
                                poTable.addCell(" ");
                                poTable.addCell("-------------------------");
                        }

            document.add(poTable);
            document.close();
            baosPDF.close();
        } catch (Exception e) {
            logger.error("Error while generating Invoice: ", e);
            e.printStackTrace();
        }

        String tmpDir = System.getProperty("java.io.tmpdir");
        String filename = tmpDir + "/item-" + itemId +"-" +System.currentTimeMillis() + ".pdf";
        File f = new File(filename);
        FileOutputStream fos = new FileOutputStream(f);
        baosPDF.writeTo(fos);
        
        }*/
        
        public static void generateBarcode(String barcodeString, String fileName, double fontSize){
                Code128Bean bean = new Code128Bean();
                
                final int dpi = 70;

                //Configure the barcode generator
                bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar 
                                                                 //width exactly one pixel
                bean.setFontSize(bean.getFontSize()+fontSize);
                bean.doQuietZone(false);

                try {
                        File outputFile = new File("/tmp/"+fileName+".png");
                        OutputStream out = new FileOutputStream(outputFile);
                        
                    //Set up the canvas provider for monochrome PNG output 
                    BitmapCanvasProvider canvas = new BitmapCanvasProvider(
                            out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);

                    //Generate the barcode
                    bean.generateBarcode(canvas, barcodeString);

                    //Signal end of generation
                    canvas.finish();
                    out.close();
                    
                } 
                catch(Exception e){
                        logger.error("Exception during generating Barcode : ", e);
                }
        }
        
        public static void generateEanBarcode(String barcodeString, String fileName, double fontSize){          
                
                
                        EAN13Bean bean = new EAN13Bean(); // EAN-13 barcode

                        final int dpi = 140; // dots per inch

                        bean.setModuleWidth(UnitConv.in2mm(1.0f / (dpi/2) )); // one bar (module) width
                        bean.setHeight(20); // barcode height
                        bean.doQuietZone(true); // white padding

                        

                        try {
                                File outputFile = new File("/tmp/"+fileName+".jpg"); // output file
                                OutputStream out = new FileOutputStream(outputFile); // output stream
                                // bitmap canvas for the barcode
                                BitmapCanvasProvider canvas = new BitmapCanvasProvider( out, "image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);

                                // barcode generator method
                                bean.generateBarcode(canvas, barcodeString);

                                // end of barcode generation
                                canvas.finish();
                                out.close();
                        } catch(Exception e){
                                logger.error("Exception during generating Barcode : ", e);
                        }
                
        }
}