您的位置:首页 > 编程语言 > Java开发

Java调用热敏打印机

2018-02-02 16:11 176 查看
本人使用的是佳博GB-58L打印机

public class PrintUtil implements Printable{

/*标题*/
private static String TITLENAME;
/*流水单ID*/
private static String ORDERID;
/*操作员*/
private static String USERNAME;
/*地址*/
private static String ADRESS;
/*联系电话*/
private static String TEL;
/*订单总额*/
private static String TOTALORDER;
/*实付总额*/
private static String TOTALPAID;
/*商品列表*/
private static String[] GOODSARRAY;
/*当前时间*/
private static String NOWTIME;
/*支付方式*/
private static String PAYTYPE;

@Override
public int print(Graphics g,PageFormat pf,int page) throws PrinterException {

if (page > 0) {
return NO_SUCH_PAGE;
}
int height = 20;
Graphics2D g2d = (Graphics2D) g;
g2d.setFont(new Font("Default", Font.PLAIN, 10));
g2d.drawString(TITLENAME, 40, height);
height = height + 10;
g2d.drawString("-------------------------------------", 7, height);
height = height + 15;
g2d.setFont(new Font("Default", Font.PLAIN, 7));
g2d.drawString("凭证号:" + ORDERID, 7, height);
height = height + 20;
g2d.setFont(new Font("Default", Font.PLAIN, 10));
g2d.drawString("-------------------------------------", 7, height);
g2d.setFont(new Font("Default", Font.PLAIN, 8));
if (GOODSARRAY.length > 0) {
for (int i = 0;i < GOODSARRAY.length;i ++) {
String strNow = GOODSARRAY[i];
String[] goods = strNow.split(",");
// 商品名
String goodsName = goods[0];
// 数量
String count = goods[1];
// 单价
String price = goods[2];
// 商品名超过10个字 ,需要换行
if (goodsName.length() > 10) {
height = height + 20;
g2d.drawString(goodsName, 7, height);
height = height + 20;
g2d.drawString("×" + count, 87, height);
g2d.drawString(price, 107, height);
} else {
height = height + 20;
g2d.drawString(goodsName, 7, height);
g2d.drawString("×" + count, 87, height);
g2d.drawString(price, 107, height);
}
}
}
height = height + 20;
g2d.drawString("合计", 77, height);
g2d.drawString("¥"+TOTALORDER, 97, height);
g2d.setFont(new Font("Default", Font.PLAIN, 10));
height = height + 15;
g2d.drawString("-------------------------------------", 7, height);
height = height + 20;
g2d.setFont(new Font("Default", Font.PLAIN, 8));
g2d.drawString("实付金额", 7, height);
g2d.drawString("¥"+TOTALPAID, 57, height);
height = height + 20;
g2d.drawString("支付方式", 7, height);
g2d.drawString(PAYTYPE, 57, height);
height = height + 15;
g2d.setFont(new Font("Default", Font.PLAIN, 10));
g2d.drawString("-------------------------------------", 7, height);
height = height + 15;
g2d.setFont(new Font("Default", Font.PLAIN, 7));
g2d.drawString("*收银员:" + USERNAME + "*", 7, height);
height = height + 30;
g2d.drawString("日期:" + NOWTIME, 7, height);
height = height + 20;
g2d.drawString("地址:" + ADRESS, 7, height);
height = height + 20;
g2d.drawString("电话:" + TEL, 7, height);
return PAGE_EXISTS;
}

public static void main(String[] args) throws ParseException{

/*标题*/
TITLENAME = args[0];
/*流水单ID*/
ORDERID = args[1];
/*操作员*/
USERNAME = args[2];
/*地址*/
ADRESS = args[3];
/*电话*/
TEL = args[4];
/*订单总额*/
TOTALORDER = args[5];
/*订单实付*/
TOTALPAID = args[6];
/*支付方式*/
PAYTYPE = args[7];
// 商品信息
String strJson = args[8];
JSONObject jsonObject = JSONObject.parseObject(strJson);
JSONArray jsonArray = jsonObject.getJSONArray("goods");
/*商品列表*/
GOODSARRAY = new String[jsonArray.size()];
for (int i = 0; i < jsonArray.size();i++) {
JSONObject subJson = (JSONObject)jsonArray.get(i);
String goodsName = "";
if (subJson.get("goodsName") != null) {
goodsName = String.valueOf(subJson.get("goodsName"));
}
String count = "";
if (subJson.get("count") != null) {
count = String.valueOf(subJson.get("count"));
}
String price = "";
if (subJson.get("price") != null) {
price = String.valueOf(subJson.get("price"));
}
GOODSARRAY[i] = goodsName + "," + count + "," + price;
}
/*当前时间*/
NOWTIME = DateUtil.getNowTime();
int height = 155 + 3 * 35 + 20 + jsonArray.size() * 40;

// 通俗理解就是书、文档
Book book = new Book();

// 打印格式
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);

// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper p = new Paper();
p.setSize(165, height); // 纸张大小A4纸(595, 842),经测试58mm为165
p.setImageableArea(5, 5, 155, height + 20); //设置打印区域,A4纸的默认X,Y边距是72
pf.setPaper(p);

// 把 PageFormat 和 Printable 添加到书中,组成一个页面
book.append(new PrintUtil(), pf);

// 获取打印服务对象
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(book);
try {
job.print();
} catch (PrinterException e) {
System.out.println("================打印出现异常");
}

}

}以上为工具类
下面是调用的代码

/**
* 打印流水单
* @return
*/
private Boolean printOrder(String orderId,String totalOrder,String totalPaid) throws Exception{
OrderDto orderDto = orderService.findDetail(orderId);
if (orderDto != null) {
/*标题*/
String TITLENAME = Constant.TICKET_NAME;
/*流水单ID*/
String ORDERID = orderId.substring(orderId.length() - 15);
/*操作员*/
String USERNAME = Constant.ADMIN_USER_NAME;
/*地址*/
String ADRESS = Constant.TICKET_ADRESS;
/*订单总额*/
String TOTALORDER = totalOrder;
/*实付总额*/
String TOTALPAID = totalPaid;
/*支付方式*/
String PAYTYPE = Constant.PAY_TYPE_NAME;
/*支付方式*/
String TEL = Constant.TEL;
// 子订单列表
List<OrderDetailDto> detailDtoList = orderDto.getDetailDtoList();
JSONArray jsonArray = new JSONArray();
for (OrderDetailDto detailDto:detailDtoList) {
// 商品
JSONObject subJson = new JSONObject();
subJson.put("goodsName",detailDto.getGoodsName());
subJson.put("count",detailDto.getQuantity());
subJson.put("price",detailDto.getPrice());
jsonArray.add(subJson);
}
JSONObject jsonGoods = new JSONObject();
jsonGoods.put("goods",jsonArray);
String strJson = jsonGoods.toString();
PrintUtil.main(new String[]{TITLENAME,ORDERID,USERNAME,ADRESS,TEL,TOTALORDER,TOTALPAID,PAYTYPE,strJson});
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  热敏打印机 GP-58L