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;
|
}
|
}
|