1.配置文件中进行配置:
#----------------打印机配置-----------------------
#打印机字体
font_GK888t_URL: C:\ts24.lib
#打印机条logo图片
logo_GK888t_URL: C:\bjac.png
#打印机配置
dz_GK888t_URL: ZDesigner GK888t (EPL)
2.java代码实现调用打印机
package com.iboyaa.util.ZPL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.standard.PrinterName;
@Service
public class ZplPrinter {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private String printerURI = null;//打印机完整路径
private PrintService printService = null;//打印机服务
private byte[] dotFont;
private String begin = "^XA"; //标签格式以^XA开始
private String end = "^XZ"; //标签格式以^XZ结束
private String content = "";
String fontURL ="";
String dz_GK888t_URL ="";
String logo_URL = "";
public ZplPrinter() {
super();
this.fontURL ="C:/ts24.lib";
this.logo_URL ="C:\\bjac.png";
this.dz_GK888t_URL ="ZDesigner GK888t (EPL)";
}
public ZplPrinter(String fontURL, String logo_url, String dz_gk888t_url) {
this.fontURL =fontURL;
this.logo_URL =logo_url;
this.dz_GK888t_URL =dz_gk888t_url;
}
public static void main(String[] args) {
//ZplPrinter p = new ZplPrinter("ZDesigner GK888t (EPL)");
//1.打印单个条码
// p.resetZpl();//注意要清除上次的打印信息
//2.打印中、英、数字、条码混合
//左边的条码
/*
String bar1 = "07";
p.setChar(bar1, 190, 130, 60, 60);
String bar1Zpl = "^FO100,200^BY8,3.0,240^BCR,,N,N,N^FD${data}^FS";//条码样式模板
p.setBarcode(bar1,bar1Zpl);
//下边的条码
String bar2 = "00000999990018822969";//20位
String bar2Paper = "^FO380,600^BY3,3.0,100^BCN,,Y,N,N^FD${data}^FS";//条码样式模板
p.setBarcode(bar2,bar2Paper);
*/
//start 字体功能模块,,
/*p.setText("国药控股www湖南有限公司", 220, 220, 50, 50, 30, 1, 1, 24);
p.setChar("CSS0BPKPPR", 80, 100,40, 40);
String zpl2 = p.getZpl();
logger.info("-----------"+zpl2);
p.print(zpl2);*/
//end 字体功能模块,,
// p.resetZpl();//注意要清除上次的打印信息
// String s=p.getZpl(new Fnthex().printImage("C:\\Users\\Public\\Pictures\\Sample Pictures\\2.jpg",200,200));
// logger.info("----"+s);
// boolean result2 = p.print(s);
// new ZplPrinter().printBZ(12,"立案调查","L1001");
new ZplPrinter().printBZ(12,"立案调查","L1001","SL2877_343");
}
/**
* 构造方法
* @param printerURI 打印机路径
*/
public ZplPrinter(String printerURI,String fontURL){
this.printerURI = printerURI;
//加载字体
File file = new File(fontURL);
if(file.exists()){
FileInputStream fis;
try {
fis = new FileInputStream(file);
dotFont = new byte[fis.available()];
fis.read(dotFont);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}else{
logger.info("C://ts24.lib文件不存在");
}
//初始化打印机
PrintService[] services = PrintServiceLookup.lookupPrintServices(null,null);
if (services != null && services.length > 0) {
for (PrintService service : services) {
logger.info("-----------------------------------"+service.getName());
if (printerURI.equals(service.getName())) {
printService = service;
break;
}
}
}
if (printService == null) {
logger.info("没有找到打印机:["+printerURI+"]");
//循环出所有的打印机
if (services != null && services.length > 0) {
logger.info("可用的打印机列表:");
for (PrintService service : services) {
logger.info("["+service.getName()+"]");
}
}
}else{
logger.info("找到打印机:["+printerURI+"]");
logger.info("打印机名称:["+printService.getAttribute(PrinterName.class).getValue()+"]");
}
}
/**
* 设置条形码
* @param barcode 条码字符
* @param zpl 条码样式模板
*/
public void setBarcode(String barcode,String zpl) {
content += zpl.replace("${data}", barcode);
}
/**
* 中文字符、英文字符(包含数字)混合
* @param str 中文、英文
* @param x x坐标
* @param y y坐标
* @param eh 英文字体高度height
* @param ew 英文字体宽度width
* @param es 英文字体间距spacing
* @param mx 中文x轴字体图形放大倍率。范围1-10,默认1
* @param my 中文y轴字体图形放大倍率。范围1-10,默认1
* @param ms 中文字体间距。24是个比较合适的值。
*/
public void setText(String str, int x, int y, int eh, int ew, int es, int mx, int my, int ms) {
byte[] ch = str2bytes(str);
for (int off = 0; off < ch.length;) {
if (((int) ch[off] & 0x00ff) >= 0xA0) {
int qcode = ch[off] & 0xff;
int wcode = ch[off + 1] & 0xff;
content += String.format("^FO%d,%d^XG0000%01X%01X,%d,%d^FS\n", x, y, qcode, wcode, mx, my);
begin += String.format("~DG0000%02X%02X,00072,003,\n", qcode, wcode);
qcode = (qcode + 128 - 32) & 0x00ff;
wcode = (wcode + 128 - 32) & 0x00ff;
int offset = ((int) qcode - 16) * 94 * 72 + ((int) wcode - 1) * 72;
for (int j = 0; j < 72; j += 3) {
qcode = (int) dotFont[j + offset] & 0x00ff;
wcode = (int) dotFont[j + offset + 1] & 0x00ff;
int qcode1 = (int) dotFont[j + offset + 2] & 0x00ff;
begin += String.format("%02X%02X%02X\n", qcode, wcode, qcode1);
}
x = x + ms * mx;
off = off + 2;
} else if (((int) ch[off] & 0x00FF) < 0xA0) {
setChar(String.format("%c", ch[off]), x, y, eh, ew);
x = x + es;
off++;
}
}
}
/**
* 英文字符串(包含数字)
* @param str 英文字符串
* @param x x坐标
* @param y y坐标
* @param h 高度
* @param w 宽度
*/
public void setChar(String str, int x, int y, int h, int w) {
content += "^FO" + x + "," + y + "^A0," + h + "," + w + "^FD" + str + "^FS";
}
/**
* 英文字符(包含数字)顺时针旋转90度
* @param str 英文字符串
* @param x x坐标
* @param y y坐标
* @param h 高度
* @param w 宽度
*/
public void setCharR(String str, int x, int y, int h, int w) {
content += "^FO" + x + "," + y + "^A0R," + h + "," + w + "^FD" + str + "^FS";
}
/**
* 获取完整的ZPL
* @return
*/
public String getZpl() {
return begin + content + end;
}
//+"^LL80^PW60"
public String getZpl(String text) {
return begin +text + end;
}
/**
* 重置ZPL指令,当需要打印多张纸的时候需要调用。
*/
public void resetZpl() {
begin = "^XA";
end = "^XZ";
content = "";
}
/**
* 打印
* @param zpl 完整的ZPL
*/
public boolean print(String zpl){
if(printService==null){
logger.info("打印出错:没有找到打印机:["+printerURI+"]");
return false;
}
DocPrintJob job = printService.createPrintJob();
byte[] by = zpl.getBytes();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(by, flavor, null);
try {
job.print(doc, null);
logger.info("已打印");
return true;
} catch (PrintException e) {
logger.info("打印失败!");
e.printStackTrace();
return false;
}
}
/**
* 字符串转byte[]
* @param s
* @return
*/
private byte[] str2bytes(String s) {
if (null == s || "".equals(s)) {
return null;
}
byte[] abytes = null;
try {
abytes = s.getBytes("gb2312");
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
return abytes;
}
//进行切纸指令运行
public static String ZPL_Start()
{
StringBuilder builder = new StringBuilder();
builder.append("^XA"); //指令块的开始
builder.append("^MMC,Y");
builder.append("^PRE,E");
builder.append("^MD30"); //MD是设置色带颜色的深度
builder.append("^LH0,0");
builder.append("^ABN,50,50");
builder.append("^CW1,D:MSUNG.FNT");
builder.append("^SEE:GB8BIT.DAT");
builder.append("^CI26");
builder.append("^XZ"); //指令块的结束
return builder.toString();
}
public static void ZPL_Two(ZplPrinter p,String bar0, int x, int y) {
//start 二维码打印功能模块,,
String bar0Zpl ="^FO"+x+","+y
+"^BQ,2,8"
+"^FDQA,${data}^FS";
p.setBarcode(bar0, bar0Zpl);
//return p.getZpl();
}
//导服系统的打印机
public void printBZ(Integer number,String reasonName,String num){
logger.info(dz_GK888t_URL+"------------------"+fontURL);
ZplPrinter p = new ZplPrinter(dz_GK888t_URL,fontURL);
//ZplPrinter p = new ZplPrinter("ZDesigner GK888t (EPL)");
p.resetZpl();//注意要清除上次的打印信息
p.setText("_______________", 194, 210, 30, 30, 30, 1, 1, 24);
p.setText("北京仲裁委员会", 334, 270, 30, 30, 30, 1, 1, 24);
p.setText("当前待办人数: "+number+"人", 270, 380, 30, 30, 30, 1, 1, 24);
p.setText("来访事由: "+reasonName, 270, 480, 30, 30, 30, 1, 1, 24);
p.setText("您的业务号码", 270, 580, 30, 30, 30, 2, 2, 24);
p.setText(num, 345, 680, 80, 80, 30, 4, 4, 24);
//前面的ZPL_Start()是打印指令
String s1=ZPL_Start()+p.getZpl2(new Fnthex().printImage(logo_URL,250,50));
//运行切纸指令;
// String s1= ZPL_Start();
//二维码
// String s1= ZPL_Two(p);
p.print(s1);
}
public String getZpl2(String text) {
return begin +text+content + end;
}
//导服系统的打印机
public void printBZ(Integer number, String reasonName, String num, String caseNumber) {
logger.info(dz_GK888t_URL+"------------------"+fontURL);
ZplPrinter p = new ZplPrinter(dz_GK888t_URL,fontURL);
//ZplPrinter p = new ZplPrinter("ZDesigner GK888t (EPL)");
p.resetZpl();//注意要清除上次的打印信息
p.setText("_______________", 194, 210, 30, 30, 30, 1, 1, 24);
//p.setText("北京仲裁委员会", 334, 270, 30, 30, 30, 1, 1, 24);
p.setText("当前待办人数: "+number+"人", 270, 270, 30, 30, 30, 1, 1, 24);
p.setText("来访事由: "+reasonName, 270, 320, 30, 30, 30, 1, 1, 24);
//二维码;caseNumber号码;;需要进行布局
ZPL_Two(p,caseNumber,334,370);
p.setText("您的业务号码", 270, 580, 30, 30, 30, 2, 2, 24);
p.setText(num, 345, 680, 80, 80, 30, 4, 4, 24);
//前面的ZPL_Start()是打印指令
String s1=ZPL_Start()+p.getZpl2(new Fnthex().printImage(logo_URL,250,50));
// String s1=p.getZpl2(new Fnthex().printImage(logo_URL,250,50));
//运行切纸指令;
// String s1= ZPL_Start();
p.print(s1);
}
}