Subversion Repositories SmartDukaan

Rev

Rev 17422 | Rev 17429 | Go to most recent revision | 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;
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
 
17416 manish.sha 30
	public static ByteArrayOutputStream generatePdfLabelSheet(String itemId, String itemNumber) throws IOException{
17403 manish.sha 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
			}
83
 
84
            document.add(poTable);
85
            document.close();
17422 manish.sha 86
            baosPDF.close();
17403 manish.sha 87
        } catch (Exception e) {
88
            logger.error("Error while generating Invoice: ", e);
89
            e.printStackTrace();
90
        }
17422 manish.sha 91
		String tmpDir = System.getProperty("java.io.tmpdir");
92
        String filename = tmpDir + "/item-" + itemId +"-" +System.currentTimeMillis() + ".pdf";
93
        File f = new File(filename);
94
        FileOutputStream fos = new FileOutputStream(f);
95
        baosPDF.writeTo(fos);
17416 manish.sha 96
        return baosPDF;
17403 manish.sha 97
	}
98
	/*
99
	public static void main(String[] args) throws IOException{
100
		ByteArrayOutputStream baosPDF = null;
101
		long itemId = 18267L;
102
        try {
103
        	baosPDF = new ByteArrayOutputStream();
104
 
105
            Document document = new Document();
106
            PdfWriter.getInstance(document, baosPDF);
107
            document.addAuthor("shop2020");
108
            document.addTitle("Label for item: "
109
                    + itemId);
110
            document.open();
111
 
112
            PdfPTable poTable = new PdfPTable(new float[] { 0.3f, 0.05f, 0.3f, 0.05f, 0.3f});
113
            poTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
114
            poTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
115
            generateBarcode("Spice Mi -3330 Champ 1800 black", "item_name_"+itemId, 6.0f);
116
            generateEanBarcode("8906050994080", "item_number"+itemId, 1.0f);
117
 
118
            Image itemNameImage=null;
119
			try {
120
				itemNameImage = Image.getInstance("/tmp/"+"item_name_"+itemId+".png");
121
			} catch (Exception e) {
122
				logger.error("Exception during getting Iten Name Image : ", e);
123
			}
124
 
125
			Image itemNumberImage=null;
126
			try {
127
				itemNumberImage = Image.getInstance("/tmp/"+"item_number"+itemId+".jpg");
128
			} catch (Exception e) {
129
				logger.error("Exception during getting Iten Name Image : ", e);
130
			}
131
 
132
			for(int i=1;i<8;i++){
133
				for(int j=1; j<=5; j++){
134
					if(j%2==0){
135
						poTable.addCell(" ");
136
					}else{
137
						PdfPTable poSubTable = new PdfPTable(1);
138
						poSubTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
139
						poSubTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
140
						poSubTable.setSpacingAfter(2.0f);
141
 
142
						poSubTable.addCell(itemNameImage);
143
 
144
						poSubTable.addCell(itemNumberImage);
145
 
146
						poTable.addCell(poSubTable);
147
					}
148
				}
149
				poTable.addCell("-------------------------");
150
				poTable.addCell(" ");
151
				poTable.addCell("-------------------------");
152
				poTable.addCell(" ");
153
				poTable.addCell("-------------------------");
154
			}
155
 
156
            document.add(poTable);
157
            document.close();
158
            baosPDF.close();
159
        } catch (Exception e) {
160
            logger.error("Error while generating Invoice: ", e);
161
            e.printStackTrace();
162
        }
163
 
164
        String tmpDir = System.getProperty("java.io.tmpdir");
165
        String filename = tmpDir + "/item-" + itemId +"-" +System.currentTimeMillis() + ".pdf";
166
        File f = new File(filename);
167
        FileOutputStream fos = new FileOutputStream(f);
168
        baosPDF.writeTo(fos);
169
 
170
	}*/
171
 
172
	public static void generateBarcode(String barcodeString, String fileName, double fontSize){
173
		Code39Bean bean = new Code39Bean();
174
 
175
		final int dpi = 70;
176
 
177
		//Configure the barcode generator
178
		bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar 
179
		                                                 //width exactly one pixel
180
		bean.setFontSize(bean.getFontSize()+fontSize);
181
		bean.doQuietZone(false);
182
 
183
		try {
184
			File outputFile = new File("/tmp/"+fileName+".png");
185
			OutputStream out = new FileOutputStream(outputFile);
186
 
187
		    //Set up the canvas provider for monochrome PNG output 
188
		    BitmapCanvasProvider canvas = new BitmapCanvasProvider(
189
		            out, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
190
 
191
		    //Generate the barcode
192
		    bean.generateBarcode(canvas, barcodeString);
193
 
194
		    //Signal end of generation
195
		    canvas.finish();
196
		    out.close();
197
 
198
		} 
199
		catch(Exception e){
200
			logger.error("Exception during generating Barcode : ", e);
201
		}
202
	}
203
 
204
	public static void generateEanBarcode(String barcodeString, String fileName, double fontSize){		
205
 
206
 
207
			EAN13Bean bean = new EAN13Bean(); // EAN-13 barcode
208
 
209
			final int dpi = 140; // dots per inch
210
 
211
			bean.setModuleWidth(UnitConv.in2mm(1.0f / (dpi/2) )); // one bar (module) width
212
			bean.setHeight(20); // barcode height
213
			bean.doQuietZone(true); // white padding
214
 
215
 
216
 
217
			try {
218
				File outputFile = new File("/tmp/"+fileName+".jpg"); // output file
219
				OutputStream out = new FileOutputStream(outputFile); // output stream
220
				// bitmap canvas for the barcode
221
				BitmapCanvasProvider canvas = new BitmapCanvasProvider( out, "image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
222
 
223
				// barcode generator method
224
				bean.generateBarcode(canvas, barcodeString);
225
 
226
				// end of barcode generation
227
				canvas.finish();
228
				out.close();
229
			} catch(Exception e){
230
				logger.error("Exception during generating Barcode : ", e);
231
			}
232
 
233
	}
234
}