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