Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
17403 manish.sha 1
package in.shop2020.inventory.service;
2
 
3
import in.shop2020.model.v1.catalog.Item;
4
import in.shop2020.thrift.clients.CatalogClient;
5
import in.shop2020.utils.ModelUtils;
6
import java.awt.image.BufferedImage;
7
import java.io.ByteArrayOutputStream;
8
import java.io.File;
9
import java.io.FileOutputStream;
10
import java.io.IOException;
11
import java.io.OutputStream;
17435 manish.sha 12
 
13
import org.krysalis.barcode4j.impl.code128.Code128Bean;
17403 manish.sha 14
import org.krysalis.barcode4j.impl.code39.Code39Bean;
15
import org.krysalis.barcode4j.impl.upcean.EAN13Bean;
16
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
17
import org.krysalis.barcode4j.tools.UnitConv;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20
import com.itextpdf.text.Document;
21
import com.itextpdf.text.Element;
22
import com.itextpdf.text.Image;
23
import com.itextpdf.text.Rectangle;
24
import com.itextpdf.text.pdf.PdfPTable;
25
import com.itextpdf.text.pdf.PdfWriter;
26
 
27
 
28
public class ProductLabelGenerator{
29
	private static Logger logger = LoggerFactory
30
    .getLogger(ProductLabelGenerator.class);
31
 
17416 manish.sha 32
	public static ByteArrayOutputStream generatePdfLabelSheet(String itemId, String itemNumber) throws IOException{
17403 manish.sha 33
		ByteArrayOutputStream baosPDF = null;
34
		try {
35
			CatalogClient cClient = new CatalogClient();
36
	        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = cClient.getClient();
37
	        Item item = catalogClient.getItem(Long.parseLong(itemId));
38
        	baosPDF = new ByteArrayOutputStream();
39
 
40
            Document document = new Document();
41
            PdfWriter.getInstance(document, baosPDF);
42
            document.addAuthor("shop2020");
43
            document.addTitle("Label for item: "
44
                    + itemId);
45
            document.open();
46
 
17429 manish.sha 47
            PdfPTable poTable = new PdfPTable(new float[] { 0.3f, 0.05f, 0.3f, 0.05f, 0.3f});
17403 manish.sha 48
            poTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
49
            poTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
17435 manish.sha 50
            generateBarcode(ModelUtils.extractProductNameFromItem(item), "item_name_"+itemId, 4.0f);
51
            generateBarcode(itemNumber, "item_number"+itemId, 1.0f);
17403 manish.sha 52
 
53
            Image itemNameImage=null;
54
			try {
55
				itemNameImage = Image.getInstance("/tmp/"+"item_name_"+itemId+".png");
56
			} catch (Exception e) {
57
				logger.error("Exception during getting Iten Name Image : ", e);
58
			}
59
 
60
			Image itemNumberImage=null;
61
			try {
17435 manish.sha 62
				itemNumberImage = Image.getInstance("/tmp/"+"item_number"+itemId+".png");
17403 manish.sha 63
			} catch (Exception e) {
64
				logger.error("Exception during getting Iten Name Image : ", e);
65
			}
66
 
17435 manish.sha 67
			for(int i=1;i<8;i++){
17403 manish.sha 68
				for(int j=1; j<=5; j++){
69
					if(j%2==0){
70
						poTable.addCell(" ");
71
					}else{
72
						PdfPTable poSubTable = new PdfPTable(1);
73
						poSubTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
74
						poSubTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
75
						poSubTable.setSpacingAfter(2.0f);
76
 
77
						poSubTable.addCell(itemNameImage);
78
 
79
						poSubTable.addCell(itemNumberImage);
80
 
81
						poTable.addCell(poSubTable);
82
					}
83
				}
17429 manish.sha 84
				poTable.addCell("-------------------------");
85
				poTable.addCell(" ");
86
				poTable.addCell("-------------------------");
87
				poTable.addCell(" ");
88
				poTable.addCell("-------------------------");
17403 manish.sha 89
			}
90
 
91
            document.add(poTable);
92
            document.close();
17422 manish.sha 93
            baosPDF.close();
17403 manish.sha 94
        } catch (Exception e) {
95
            logger.error("Error while generating Invoice: ", e);
96
            e.printStackTrace();
97
        }
17422 manish.sha 98
		String tmpDir = System.getProperty("java.io.tmpdir");
99
        String filename = tmpDir + "/item-" + itemId +"-" +System.currentTimeMillis() + ".pdf";
100
        File f = new File(filename);
101
        FileOutputStream fos = new FileOutputStream(f);
102
        baosPDF.writeTo(fos);
17416 manish.sha 103
        return baosPDF;
17403 manish.sha 104
	}
17435 manish.sha 105
 
106
	/**
107
	 * @param args
108
	 * @throws IOException
109
	 */
17403 manish.sha 110
	/*
111
	public static void main(String[] args) throws IOException{
112
		ByteArrayOutputStream baosPDF = null;
113
		long itemId = 18267L;
114
        try {
115
        	baosPDF = new ByteArrayOutputStream();
116
 
117
            Document document = new Document();
118
            PdfWriter.getInstance(document, baosPDF);
119
            document.addAuthor("shop2020");
120
            document.addTitle("Label for item: "
121
                    + itemId);
122
            document.open();
123
 
124
            PdfPTable poTable = new PdfPTable(new float[] { 0.3f, 0.05f, 0.3f, 0.05f, 0.3f});
125
            poTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
126
            poTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
17435 manish.sha 127
            generateBarcode("Spice Mi -3330 Champ 1800 black", "item_name_"+itemId, 4.0f);
128
            generateBarcode("Pang42352637", "item_number"+itemId, 1.0f);
17403 manish.sha 129
 
130
            Image itemNameImage=null;
131
			try {
132
				itemNameImage = Image.getInstance("/tmp/"+"item_name_"+itemId+".png");
133
			} catch (Exception e) {
134
				logger.error("Exception during getting Iten Name Image : ", e);
135
			}
136
 
137
			Image itemNumberImage=null;
138
			try {
17435 manish.sha 139
				itemNumberImage = Image.getInstance("/tmp/"+"item_number"+itemId+".png");
17403 manish.sha 140
			} catch (Exception e) {
141
				logger.error("Exception during getting Iten Name Image : ", e);
142
			}
143
 
144
			for(int i=1;i<8;i++){
145
				for(int j=1; j<=5; j++){
146
					if(j%2==0){
147
						poTable.addCell(" ");
148
					}else{
149
						PdfPTable poSubTable = new PdfPTable(1);
150
						poSubTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
151
						poSubTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
152
						poSubTable.setSpacingAfter(2.0f);
153
 
154
						poSubTable.addCell(itemNameImage);
155
 
156
						poSubTable.addCell(itemNumberImage);
157
 
158
						poTable.addCell(poSubTable);
159
					}
160
				}
161
				poTable.addCell("-------------------------");
162
				poTable.addCell(" ");
163
				poTable.addCell("-------------------------");
164
				poTable.addCell(" ");
165
				poTable.addCell("-------------------------");
166
			}
167
 
168
            document.add(poTable);
169
            document.close();
170
            baosPDF.close();
171
        } catch (Exception e) {
172
            logger.error("Error while generating Invoice: ", e);
173
            e.printStackTrace();
174
        }
175
 
176
        String tmpDir = System.getProperty("java.io.tmpdir");
177
        String filename = tmpDir + "/item-" + itemId +"-" +System.currentTimeMillis() + ".pdf";
178
        File f = new File(filename);
179
        FileOutputStream fos = new FileOutputStream(f);
180
        baosPDF.writeTo(fos);
181
 
182
	}*/
183
 
184
	public static void generateBarcode(String barcodeString, String fileName, double fontSize){
17435 manish.sha 185
		Code128Bean bean = new Code128Bean();
17403 manish.sha 186
 
187
		final int dpi = 70;
188
 
189
		//Configure the barcode generator
190
		bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar 
191
		                                                 //width exactly one pixel
192
		bean.setFontSize(bean.getFontSize()+fontSize);
193
		bean.doQuietZone(false);
194
 
195
		try {
196
			File outputFile = new File("/tmp/"+fileName+".png");
197
			OutputStream out = new FileOutputStream(outputFile);
198
 
199
		    //Set up the canvas provider for monochrome PNG output 
200
		    BitmapCanvasProvider canvas = new BitmapCanvasProvider(
201
		            out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
202
 
203
		    //Generate the barcode
204
		    bean.generateBarcode(canvas, barcodeString);
205
 
206
		    //Signal end of generation
207
		    canvas.finish();
208
		    out.close();
209
 
210
		} 
211
		catch(Exception e){
212
			logger.error("Exception during generating Barcode : ", e);
213
		}
214
	}
215
 
216
	public static void generateEanBarcode(String barcodeString, String fileName, double fontSize){		
217
 
218
 
219
			EAN13Bean bean = new EAN13Bean(); // EAN-13 barcode
220
 
221
			final int dpi = 140; // dots per inch
222
 
223
			bean.setModuleWidth(UnitConv.in2mm(1.0f / (dpi/2) )); // one bar (module) width
224
			bean.setHeight(20); // barcode height
225
			bean.doQuietZone(true); // white padding
226
 
227
 
228
 
229
			try {
230
				File outputFile = new File("/tmp/"+fileName+".jpg"); // output file
231
				OutputStream out = new FileOutputStream(outputFile); // output stream
232
				// bitmap canvas for the barcode
233
				BitmapCanvasProvider canvas = new BitmapCanvasProvider( out, "image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
234
 
235
				// barcode generator method
236
				bean.generateBarcode(canvas, barcodeString);
237
 
238
				// end of barcode generation
239
				canvas.finish();
240
				out.close();
241
			} catch(Exception e){
242
				logger.error("Exception during generating Barcode : ", e);
243
			}
244
 
245
	}
246
}