严智鑫
2025-11-13 945bc394f40d8af1072a53da9a94f24207124e6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package com.northglass.listener;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.PrinterName;
 
import org.apache.struts2.components.ElseIf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
 
import com.northglass.constants.StateConstants.printStatu;
import com.northglass.entity.PrintName;
import com.northglass.entity.PrintSize;
import com.northglass.repository.PrintNameDao;
import com.northglass.repository.PrintSizeDao;
import com.northglass.service.device.Device;
import com.northglass.service.device.DeviceService;
import com.northglass.util.JbarcodeUtil;
import com.sun.mail.handlers.image_gif;
 
public class GMMSPrinter implements Printable {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(GMMSPrinter.class);
 
    private String customer;// 客户名称
    private String application;// 条形码
    private String production;// 成品名称
    private String floorNumber;// 楼层编号
    private String orderNumber;// 订单序号
    private double length;// 长度
    private double width;// 宽度
    private String apartId;// 流程卡号
    private String pieceSign;// 片标记
    private String remark;// 明细备注
    private String line;// 生产线
    private String printname;// 打印机名称
    private String printstatu;// 打印机打法类型 (横打/竖打)
    private int pieces;
    private Long glassId;
    private String arrangeStatu;
    private String thingsout;
 
    public GMMSPrinter(String customer, String application, String production, String floorNumber, String orderNumber,
            double length, double width, String apartId, String pieceSign, String remark, String line, int pieces,
            Long glassId, String printname, String printstatu, String arrangeStatu,String thingsout) {
        super();
        this.customer = customer;
        this.application = application;
        this.production = production;
        this.floorNumber = floorNumber;
        this.orderNumber = orderNumber;
        this.length = length;
        this.width = width;
        this.apartId = apartId;
        this.pieceSign = pieceSign;
        this.remark = remark;
        this.line = line;
        this.pieces = pieces;
        this.glassId = glassId;
        this.printname = printname;
        this.printstatu = printstatu;
        this.arrangeStatu = arrangeStatu;
        this.thingsout = thingsout;
    }
 
    public GMMSPrinter(String customer, String application, String production, String floorNumber, String line,
            Long glassId) {
        super();
        this.customer = customer;
        this.application = application;
        this.production = production;
        this.floorNumber = floorNumber;
        this.line = line;
        this.glassId = glassId;
    }
    // @Autowired
    // private DeviceService service;
    //
    // PrintSize printSize = service.findPrintSize();
 
    /**
     * 
     * @param Graphic指明打印的图形环境
     * 
     * @param PageFormat指明打印页格式(页面大小以点为计量单位,1点为1英才的1/72,1英寸为25.4毫米。A4纸大致为595×
     *            842点)
     * 
     * @param pageIndex指明页号
     * 
     **/
    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        Graphics2D g2 = (Graphics2D) graphics;
        // 设置打印的颜色
        g2.setPaint(Color.black);
        // 转换坐标,确定打印边界
        g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        String lengthStr = String.valueOf(length).substring(0, String.valueOf(length).indexOf("."));
        String widthStr = String.valueOf(width).substring(0, String.valueOf(width).indexOf("."));
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        if (remark == null ) {
            remark ="";
        }
        if (orderNumber == null ) {
            orderNumber ="";
        }
        
        // 设置打印的字体
        if (printstatu.equals(printStatu.ACROSS)) {
            Font font = new Font("黑体", Font.BOLD, 12);
            g2.setFont(font);
            g2.drawString(apartId, 20, 15);
 
            Font font5 = new Font("黑体", Font.BOLD, 30);
            g2.setFont(font5);
            g2.drawString(arrangeStatu, 140, 25);
 
            LOGGER.debug("production.length():" + production.length());
            if (production.length() < 12) {
                Font font2 = new Font("黑体", Font.BOLD, 11);
                g2.setFont(font2);
            } else if (production.length() >= 12 && production.length() < 20) {
                Font font2 = new Font("黑体", Font.BOLD, 9);
                g2.setFont(font2);
            } else if (production.length() >= 20) {
                Font font2 = new Font("黑体", Font.BOLD, 8);
                g2.setFont(font2);
            }
 
            g2.drawString(customer + " " + production, 20, 30);
            Font font3 = new Font("黑体", Font.BOLD, 20);
            g2.setFont(font3);
            g2.drawString(lengthStr + "*" + widthStr, 25, 50);
            Font font1 = new Font("黑体", Font.BOLD, 8);
            g2.setFont(font1);
            g2.drawString(floorNumber, 20, 60);
            if (orderNumber.length() > 20) {
                g2.drawString(orderNumber.substring(0, 20), 20, 70);
                g2.drawString(orderNumber.substring(20), 20, 80);
                if (remark.length() < 20) {
                    g2.drawString(remark, 20, 90);
                } else {
                    g2.drawString(remark.substring(0, 20), 20, 90);
                    g2.drawString(remark.substring(20), 20, 100);
                }
            } else {
                g2.drawString(orderNumber, 20, 70);
                if (remark.length() < 20) {
                    g2.drawString(remark, 20, 90);
                } else {
                    g2.drawString(remark.substring(0, 20), 20, 90);
                    g2.drawString(remark.substring(20), 20, 100);
                }
            }
 
        } else if (printstatu.equals(printStatu.ERECT)) {
            Font font = new Font("黑体", Font.BOLD, 8);
            g2.setFont(font);
            g2.drawString(apartId, 20, 10);
 
            Font font5 = new Font("黑体", Font.BOLD, 20);
            g2.setFont(font5);
            g2.drawString(arrangeStatu+"号", 150, 37);
 
            LOGGER.debug("production.length():" + production.length());
            String string = customer + " " + production;
            Font font2 = new Font("黑体", Font.BOLD, 10);
            if (string.length() > 15) {
                font2 = new Font("黑体", Font.BOLD, 8);
            } 
            g2.setFont(font2);
            g2.drawString(string, 20, 20);
            Font font3 = new Font("黑体", Font.BOLD, 20);
            g2.setFont(font3);
            g2.drawString(lengthStr + "*" + widthStr, 25, 37);
            Font font1 = new Font("黑体", Font.BOLD, 8);
            g2.setFont(font1);
            g2.drawString(floorNumber, 20, 55);
            if (orderNumber.length() > 28) {
                g2.drawString(orderNumber.substring(0, 28), 20, 63);
                g2.drawString(orderNumber.substring(28), 20, 71);
                if (remark.length() > 28) {
                    g2.drawString(remark.substring(0, 28), 20, 80);
                } else {
                    g2.drawString(remark, 20, 85);
                }
            } else {
                g2.drawString(orderNumber, 20, 65);
                Font font6 = new Font("黑体", Font.BOLD, 8);
                if (remark.length() > 28) {
                    font6 = new Font("黑体", Font.BOLD, 6);
                }
                g2.setFont(font6);
                g2.drawString(remark, 20, 75);
            }
        }else {
            //空打印
            if (length ==0 && width == 0) {
                Font font = new Font("黑体", Font.BOLD, 12);
                g2.setFont(font);
                g2.drawString("数据异常", 100, 10);
                return Printable.PAGE_EXISTS;
            }
            //流程卡号
            Font font = new Font("黑体", Font.BOLD, 12);
            g2.setFont(font);
            g2.drawString(apartId, 100, 10);
            g2.drawString(pieceSign, 15, 27);
            Font font0 = new Font("黑体", Font.BOLD, 10);
            g2.setFont(font0);
            if (thingsout==null) {
                g2.drawString("无", 130, 27);
            }else {
                g2.drawString(thingsout, 135, 27);
            }
            //客户名称
            Font font1 = new Font("黑体", Font.BOLD, 15);
            g2.setFont(font1);
            g2.drawString(customer, 15, 15);
            //片标记(改成当前片玻璃成品名称)
            
            //组号
            Font font2 = new Font("黑体", Font.BOLD, 20);
            g2.setFont(font2);
            g2.drawString(arrangeStatu+"号", 155, 45);
            //大小和片数
            Font font3 = new Font("黑体", Font.BOLD, 20);
            g2.setFont(font3);
            g2.drawString(lengthStr + "x" + widthStr+ "=" + pieces, 20, 45);
            //楼层号
            Font font4 = new Font("黑体", Font.BOLD, 12);
            g2.setFont(font4);
            if (floorNumber.length()>16) {
                Font f = new Font("黑体", Font.BOLD, 10);
                g2.setFont(f);
                g2.drawString(floorNumber, 15, 72);
            }else {
                g2.drawString(floorNumber, 15, 72);
            }
            g2.drawString(floorNumber, 15, 72);
            //成品名称(简称)
            Font font5 = new Font("黑体", Font.BOLD, 12);
            g2.setFont(font5);
            String name [] = production.split("\\+");
            if (name.length == 1) {
                production = delete(production, 'm');
                production = delete(production, '玻');
                production = delete(production, '钢');
                production = delete(production, '双');
                production = delete(production, '中');
                production = delete(production, '空');
                LOGGER.debug(production);
                g2.drawString(production, 15, 60);
            }else {
                if (name.length>=3) {
                    String s1 = delete(name[0], 'm');
                    s1 = delete(s1, '玻');
                    s1 = delete(s1, '钢');
                    s1 = delete(s1, '双');
                    s1 = delete(s1, '中');
                    s1 = delete(s1, '空');
                    String s2 = delete(name[2], 'm');
                    s2 = delete(s2, '玻');
                    s2 = delete(s2, '钢');
                    s2 = delete(s2, '双');
                    s2 = delete(s2, '中');
                    s2 = delete(s2, '空');
                    String string = s1+"/"+s2+"/"+name[1];
                    if (string.length()<=12) {
                        g2.drawString(string, 15, 60);
                    }else {
                        Font font6 = new Font("黑体", Font.BOLD, 10);
                        g2.setFont(font6);
                        g2.drawString(string, 15, 60);
                    }
                    
                }else {
                    String string="";
                    for (int i = 0; i < name.length; i++) {
                        if (i==0) {
                            string = name[i];
                        }
                        string =string+"/"+name[i];
                    }
                    g2.drawString(string, 15, 60);
                }
                
            }
        }
        //条形码
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Pattern pattern = Pattern.compile("^\\d{10}$");
        Matcher isNum = pattern.matcher(application);
        LOGGER.debug("isNum.matches()" + isNum.matches());
        if (isNum.matches()) {
            JbarcodeUtil jbarcodeUtil = new JbarcodeUtil();
            BufferedImage bufferedImage = jbarcodeUtil.generateBarCode128(application, "2", "10");
            Font font7 = new Font("黑体", Font.BOLD , 12);
            g2.setFont(font7);
            if (printstatu.equals(printStatu.ACROSS)) {
                g2.rotate(Math.toRadians(90), 115, 125);
                g2.drawImage(bufferedImage, 15, 50, 90, 130, 14, 4, 780, 99, null);// 横打
            } else if(printstatu.equals(printStatu.ERECT)){
                g2.drawImage(bufferedImage, 10, 85, 85, 140, 10, 10, 800, 99, null);// 竖打
            }else {
                g2.drawImage(bufferedImage, 10, 75, 105, 160, 10, 10, 800, 99, null);// 第二版竖打
                //明细备注
                Font font8 = new Font("黑体", Font.BOLD , 10);
                g2.setFont(font8);
                if (remark.length()>9) {
                    Font font9 = new Font("黑体", Font.BOLD , 8);
                    g2.setFont(font9);
                    g2.drawString(remark.substring(0,9), 105, 85);
                    if (remark.length()>18) {
                        g2.drawString(remark.substring(9,18), 105, 95);
                        g2.drawString(remark.substring(18), 105, 105);
                    }else {
                        g2.drawString(remark.substring(9), 105, 95);
                    }
                }else {
                    g2.drawString(remark, 105, 90);
                }
            }
 
            /**
             * drawImage(bufferedImage, a, b, c, d, e, f, g, h, l);
             * bufferedImage表示打印的码类别图片 a - 目标矩形第一个角的 x 坐标。 b - 目标矩形第一个角的 y 坐标。 c
             * - 目标矩形第二个角的 x 坐标。 d - 目标矩形第二个角的 y 坐标。 e - 源矩形第一个角的 x 坐标。 f -
             * 源矩形第一个角的 y 坐标。 g - 源矩形第二个角的 x 坐标。 h - 源矩形第二个角的 y 坐标。 l
             * 当缩放并转换了更多图像时要通知的对象。
             * 
             */
 
        }
        LOGGER.debug("结束打印一维码");
        return Printable.PAGE_EXISTS;
    }
 
    /**
     * 打印玻璃长和宽
     * 
     * @param length
     * @param width
     * @throws PrinterException
     */
    public void doPrint() {
        LOGGER.debug("> Start doPrint");
 
        // 收到光电信号打印机执行打印
        PrinterJob printerJob = PrinterJob.getPrinterJob();
        Book book = new Book();
 
        // 设置成竖打
        PageFormat pageFormat = new PageFormat();
        Paper paper = new Paper();
        if (printstatu.equals(printStatu.ACROSS)) {
            paper.setSize(112, 190);
            paper.setImageableArea(0, 0, 112, 190);// 设置打印区域
            pageFormat.setOrientation(PageFormat.LANDSCAPE);
        } else {
            pageFormat.setOrientation(PageFormat.PORTRAIT);
            paper.setSize(190, 112);
            paper.setImageableArea(0, 0, 190, 112);// 设置打印区域
        }
        // 设置页面的空白边距和可打印区域
 
        pageFormat.setPaper(paper);
 
        // 把 PageFormat和 Printable 添加到书中
        book.append(new GMMSPrinter(customer, application, production, floorNumber, orderNumber, length, width, apartId,
                pieceSign, remark, line, pieces, glassId, printname, printstatu, arrangeStatu,thingsout), pageFormat);
        printerJob.setPageable(book);
        HashAttributeSet hs = new HashAttributeSet();
        String printerName = printname;
        // 定义打印机名称
        // printerName = "Zebra GK888t";
        // printerName = "Golive-two";
        // printerName = "ZDesigner GK888t (EPL)";
        // 将当前打印机添加到打印服务
        PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, null);
        PrintService[] realPss = null;
        LOGGER.debug("pss.length:" + pss.length);
        if (pss.length > 0) {
            for (int i = 0; i < pss.length; i++) {
                String realPrinterName = pss[i].getName();
                LOGGER.debug("realPrinterName:" + realPrinterName);
                if (realPrinterName.contains(printerName)) {
                    hs.add(new PrinterName(realPrinterName, null));
                    realPss = PrintServiceLookup.lookupPrintServices(null, hs);
                    break;
                }
            }
        }
        if (pss.length > 0 && realPss != null) {
            try {
                printerJob.setPrintService(realPss[0]);
                printerJob.print();
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        } else {
            LOGGER.debug("【" + line + "】号线没找到相应的打印机!");
        }
    }
 
    public static void main(String[] args) {
        new GMMSPrinter("王鲜明", "010000046312", "10mm白钢", "", "切割(正)->磨边(反)->清洗(正)->钢化(反)->库(正)", 1866, 1688,
                "20181014.01A", "B", "倒安全角=4,印钜润3C标志", "1", 1, (long) 123, "ZDesigner GK888t (EPL)", printStatu.ACROSS,
                "4","归来地").doPrint();
    }
    public String delete(String name ,char date){
        String deleteString = "";
        for (int i = 0; i < name.length(); i++) {
            if (name.charAt(i)!=date) {
                deleteString += name.charAt(i);
            }
        }
        return deleteString;
    }
}