/** * cpcl 命令打印工具类 * 2021.04.26 uni-app版本 * @auth boolTrue */ var gbk = require("./printUtil-GBK.js"); var jpPrinter = { createNew: function() { var jpPrinter = {}; var data = ""; var command = [] var rawCommand = '' jpPrinter.name = "标签模式"; jpPrinter.addCommand = function(content) { //将指令转成数组装起 rawCommand += content } jpPrinter.init = function(width, height, printNum, rotation = 0, offset = 0) { var strCmd = '! ' + offset + ' 200 200 ' + height + ' ' + printNum + '\n'; strCmd += "PAGE-WIDTH " + width + '\n'; if (rotation == 1) strCmd += "ZPROTATE90\n"; else if (rotation == 2) strCmd += "ZPROTATE180\n"; else if (rotation == 3) strCmd += "ZPROTATE270\n"; else strCmd += "ZPROTATE0\n"; jpPrinter.addCommand(strCmd) }; jpPrinter.setText = function(x, y, font, xsize, ysize, str) { //横向打印文字 data = "T " + "4" + " " + xsize + " " + x + " " + y + " " + str + "\n" jpPrinter.addCommand(data) }; jpPrinter.setGap = function(pageWidth) { //设置标签模式 data = "GAP-SENSE" + "\n"; jpPrinter.addCommand(data) }; jpPrinter.setSpeed = function(printSpeed) { //设置打印机速度0 - 5 data = "SPEED " + printSpeed + "\n"; jpPrinter.addCommand(data) }; jpPrinter.setDensity = function(printDensity) { //设置打印机浓度最亮的打印输出为对比度级别 0。最暗的对比度级别为 3。 data = "CONTRAST " + printDensity + "\n"; jpPrinter.addCommand(data) }; jpPrinter.setMag = function(w,h) { //字体放大指定的放大倍数。 data = "SETMAG " + w + " " + h + "\n" jpPrinter.addCommand(data) }; jpPrinter.setLine = function(x0, y0, x1, y1, width) { //绘制线条 data = "LINE " + x0 + " " + y0 + " " + x1 + " " + y1 + " " + width + "\n" jpPrinter.addCommand(data) }; jpPrinter.setBox = function(x_start, y_start, x_end, y_end, thickness) { //绘制方框 data = "BOX " + x_start + " " + y_start + " " + x_end + " " + y_end + " " + thickness + "\n"; jpPrinter.addCommand(data) }; jpPrinter.setFeed = function(feed) { //将纸向前推出n data = "PREFEED " + feed + "\n"; jpPrinter.addCommand(data) }; jpPrinter.setQR = function(x, y, level, ver, scale, content) { //打印二维码 var strCmd = 'B QR ' + x + ' ' + y + ' M ' + ver + ' U ' + scale + '\n' + level + 'A,' + content + '\n'; strCmd += 'ENDQR\n'; jpPrinter.addCommand(strCmd) }; jpPrinter.setBar = function(type, width, ratio, height, x, y, content) { //打印条形码 data = "BARCODE " + type + " " + width + " " + ratio + " " + height + " " + x + " " + y + " " + content + "\n" jpPrinter.addCommand(data) }; jpPrinter.setSound = function(interval) { //控制蜂鸣器,蜂鸣持续时间 data = "BEEP " + interval + "\n"; jpPrinter.addCommand(data) }; jpPrinter.setPageForm = function() { //换页 data = "FORM "+"\n" jpPrinter.addCommand(data) }; jpPrinter.setPagePrint = function() { //打印页面 data = "PRINT "+"\n" jpPrinter.addCommand(data) }; // 添加命令 jpPrinter.RawCommand = function(data) { jpPrinter.addCommand(data) } //获取打印数据 jpPrinter.getData = function() { let buffer = gbk.strToGBKByte(rawCommand) return buffer; }; jpPrinter.getRawData = function() { return rawCommand; }; jpPrinter.clearCommand = function() { rawCommand = '' }; return jpPrinter; } }; module.exports.jpPrinter = jpPrinter;