Subversion Repositories SmartDukaan

Rev

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