123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- import gbk from '@/components/kk-printer/utils/printUtil-GBK.js';
- // 获取文本字节总数
- export const getBytesCount = function(str){
- var bytesCount = 0;
- if (str != null) {
- for (var i = 0; i < str.length; i++) {
- var c = str.charAt(i);
- if (c.match(/[^\x00-\xff]/ig) != null) //全角
- {
- bytesCount += 2;
- } else {
- bytesCount += 1;
- }
- }
- }
- return bytesCount;
- }
- // JS 包含中文的字符串按固定长度拆分换行算法
- export const getTextRows = function(str,iPageSize){
- var strArray = [];
- var tempStr = "";
- var iTotalLength = getBytesCount(str);
- for (var i = 0; i < str.length; i++) {
- var iCount = getBytesCount(str[i]);
- var iCountTemp = getBytesCount(tempStr);
- //iCountTemp + iCount >= iPageSize 这里可能出现两种情况:
- //长度等于 iPageSize 或 等于 iPageSize + 1(最后一个为中文字符就等于 9 + 2 情况)
- if (iCountTemp + iCount >= iPageSize) {
- tempStr += str[i];
- strArray.push(tempStr);
- tempStr = "";
- } else {
- tempStr += str[i];
- }
- }
-
- // //最后一次尾巴
- if (tempStr.length > 0){
- strArray.push(tempStr)
- }
- return strArray
- }
- // 格式化打印文本
- export const textFormat = function(command,text,maxFontNums,left,top,lightHeight,align,fontSize){
- let rowTop = top
- const maxNums = maxFontNums
- const rowHeight = lightHeight
- const textArr = getTextRows(text.replace(/\s+/g, ' '),maxNums*2)
- const rows = textArr.length
- for(let i=0;i<rows;i++){
- if(align=="center"){
- const ltxt = textArr[i]
- const ltlen = getBytesCount(ltxt)/2*fontSize
- command.setText(left+((maxNums-ltlen)/2)*rowHeight-2, rowTop, "TSS24.BF2", fontSize, fontSize, ltxt)
- }
- else{
- command.setText(left, rowTop, "TSS24.BF2", fontSize, fontSize, textArr[i])
- }
- rowTop = rowTop+rowHeight*fontSize+5
- }
- return rowTop
- }
- export const printText = function(tsc,text){
- const command = tsc.jpPrinter.createNew()
- command.init()
- command.setSize(40, 30) // 标签纸张宽高,单位mm
- command.setGap(3) // 标签上下间距,单位mm
- command.setCls() // 清除缓冲区数据
- command.setText(100, 48, "5", 4, 3, text)
- command.setPagePrint(1,1) // 打印分数1,每个标签重发打印2次
- return command
- }
- // 60 * 40 尺寸模板
- export const printTempl = function(tsc,data){
- let top = 32 // 距离顶部
- const left = 8 // 距离左边
- const lightHeight = 24 // 行高3mm,1mm = 8点
- const pageW = 60 // 页签宽度mm
- const pageH = 40 // 页签高度mm
- const maxFontNums = Math.floor((pageW*8-left*4)/lightHeight) // 每行最多字体数
- const leftMaxFnums = Math.floor(maxFontNums*0.4)
- const rightMaxFnums = leftMaxFnums + 1
- let rightLeft = (leftMaxFnums+2.5)*lightHeight
- // 初始化打印机
- const command = tsc.jpPrinter.createNew()
- command.init()
- command.setSize(pageW, pageH) // 标签纸张宽高,单位mm
- command.setGap(3) // 标签上下间距,单位mm
- command.setCls() // 清除缓冲区数据
- // 经销商文字高度,是否换行
- top = textFormat(command,data.dealerName,maxFontNums,left,top,lightHeight,"center",1)
- // 数字货架名称文字
- top = top+10
- top = textFormat(command,data.shelfName,maxFontNums,left,top,lightHeight,"center",1)
- // 二维码
- top = top+15
- command.setQR(left*6, top, "M", 5, "A", data.barCode)
- // 货位号
- const rightTop = textFormat(command,data.shelfPlaceCode,rightMaxFnums,rightLeft,top,lightHeight,"left",4)
- // 产品编码
- top = rightTop + 1
- top = textFormat(command,data.productCode,rightMaxFnums,rightLeft,top,lightHeight,"left",1)
-
- // 打印人打印时间
- top = top+3
- top = textFormat(command,data.printDate,rightMaxFnums,rightLeft,top,lightHeight,"left",1)
-
- // 打印分数1,每个标签重发打印2次
- command.setPagePrint(1,data.currentInven)
- return command
- }
- // 格式化cpcl打印文本
- export const textCpclFormat = function(cpcl,text,maxFontNums,left,top,lightHeight,align,fontName,fontSize,ellipsis=false){
- let rowTop = top
- const maxNums = maxFontNums*1.8
- const rowHeight = lightHeight
- const textArr = getTextRows(text.replace(/\s+/g, ' '),maxNums)
- const len = ellipsis ? 1 : textArr.length
- let result = ''
- for(let i=0;i<len;i++){
- const texts = !ellipsis||(textArr.length==1) ? textArr[i] : (textArr[i].substr(0,maxFontNums-3)+'...')
- if(align=="center"){
- result += cpcl.addCPCLLocation(2)
- }
- else{
- result += cpcl.addCPCLLocation(0)
- }
- result += cpcl.addCPCLText(left,rowTop,fontName,fontSize,0,texts);
- rowTop = rowTop+rowHeight+5
- }
- return {result:result,top:rowTop}
- }
- // 60 * 40 尺寸模板 cpcl 指令
- export const printCpclTempl = function(cpcl,data){
- console.log(cpcl)
- let top = 0 // 距离顶部
- const left = 8 // 距离左边
- const lightHeight = 24 // 行高3mm,1mm = 8点
- const pageW = 60 // 页签宽度mm
- const pageH = 40 // 页签高度mm
- const maxFontNums = Math.floor((pageW*8-left*2)/lightHeight) // 每行最多字体数
- const leftMaxFnums = Math.floor(maxFontNums*0.4)
- const rightMaxFnums = leftMaxFnums + 2
- let rightLeft = (leftMaxFnums+2)*lightHeight
- // 初始化打印机
- let strCmd =cpcl.CreatCPCLPage(pageW*8,pageH*8,data.currentInven,0,0);
- // 经销商文字高度,是否换行
- const a = textCpclFormat(cpcl,data.dealerName,maxFontNums,0,top,lightHeight,"center",'24',0);
- top = a.top;
- strCmd += a.result;
- // 数字货架名称文字
- top = top+10;
- const b = textCpclFormat(cpcl,data.shelfName,maxFontNums,0,top,lightHeight,"center",'24',0,true);
- top = b.top;
- strCmd += b.result;
- // 二维码
- top = top+15;
- strCmd += cpcl.addCPCLLocation(0);
- strCmd += cpcl.addCPCLQRCode(left*5,top,'M', 3, 5, data.barCode);
- // 货位号
- strCmd += cpcl.addCPCLSETMAG(4,4);
- const c = textCpclFormat(cpcl,data.shelfPlaceCode,rightMaxFnums,rightLeft,top,lightHeight,"left",'7',0);
- const rightTop = c.top;
- strCmd += c.result;
- // 产品编码
- top = rightTop + 65;
- strCmd += cpcl.addCPCLSETMAG(1,1);
- const d = textCpclFormat(cpcl,data.productCode,rightMaxFnums,rightLeft,top,lightHeight,"left",'7',0);
- top = d.top;
- strCmd += d.result;
- // 打印人打印时间
- top = top+4;
- strCmd += cpcl.addCPCLSETMAG(1,1);
- const e = textCpclFormat(cpcl,data.printDate,rightMaxFnums,rightLeft,top,lightHeight,"left",'7',0);
- top = e.top;
- strCmd += e.result;
- strCmd += 'FORM \n'
- strCmd += cpcl.addCPCLPrint();
- console.log(strCmd)
- return gbk.strToGBKByte(strCmd);
- }
- // 40 * 30 尺寸模板
- export const printMiniTempl = function(tsc,data){
- let top = 24 // 距离顶部
- const left = 4 // 距离左边
- const lightHeight = 24 // 行高3mm,1mm = 8点
- const pageW = 40 // 页签宽度mm
- const pageH = 30 // 页签高度mm
- const maxFontNums = Math.floor((pageW*8-left*2)/lightHeight) // 每行最多字体数
- const leftMaxFnums = Math.floor(maxFontNums*0.5)
- const rightMaxFnums = Math.floor(maxFontNums*0.4)
- let rightLeft = (leftMaxFnums+1)*lightHeight
- // 初始化打印机
- const command = tsc.jpPrinter.createNew()
- command.init()
- command.setSize(pageW, pageH) // 标签纸张宽高,单位mm
- command.setGap(3) // 标签上下间距,单位mm
- command.setCls() // 清除缓冲区数据
- // 经销商文字高度,是否换行
- top = textFormat(command,data.dealerName,maxFontNums,left,top,lightHeight,"center",1)
- // 数字货架名称文字
- top = top+10
- top = textFormat(command,data.shelfName,maxFontNums,left,top,lightHeight,"center",1)
- // 二维码
- top = top+10
- command.setQR(left, top, "M", 5, "A", data.barCode)
- // 货位号
- const rightTop = textFormat(command,data.shelfPlaceCode,rightMaxFnums,rightLeft,top,lightHeight,"left",3)
- // 产品编码
- top = rightTop
- top = textFormat(command,data.productCode,rightMaxFnums,rightLeft,top,lightHeight,"left",1)
- // 打印分数1,每个标签重发打印2次
- command.setPagePrint(1,data.currentInven)
- return command
- }
|