printTools.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import gbk from '@/components/kk-printer/utils/printUtil-GBK.js';
  2. // 获取文本字节总数
  3. export const getBytesCount = function(str){
  4. var bytesCount = 0;
  5. if (str != null) {
  6. for (var i = 0; i < str.length; i++) {
  7. var c = str.charAt(i);
  8. if (c.match(/[^\x00-\xff]/ig) != null) //全角
  9. {
  10. bytesCount += 2;
  11. } else {
  12. bytesCount += 1;
  13. }
  14. }
  15. }
  16. return bytesCount;
  17. }
  18. // JS 包含中文的字符串按固定长度拆分换行算法
  19. export const getTextRows = function(str,iPageSize){
  20. var strArray = [];
  21. var tempStr = "";
  22. var iTotalLength = getBytesCount(str);
  23. for (var i = 0; i < str.length; i++) {
  24. var iCount = getBytesCount(str[i]);
  25. var iCountTemp = getBytesCount(tempStr);
  26. //iCountTemp + iCount >= iPageSize 这里可能出现两种情况:
  27. //长度等于 iPageSize 或 等于 iPageSize + 1(最后一个为中文字符就等于 9 + 2 情况)
  28. if (iCountTemp + iCount >= iPageSize) {
  29. tempStr += str[i];
  30. strArray.push(tempStr);
  31. tempStr = "";
  32. } else {
  33. tempStr += str[i];
  34. }
  35. }
  36. // //最后一次尾巴
  37. if (tempStr.length > 0){
  38. strArray.push(tempStr)
  39. }
  40. return strArray
  41. }
  42. // 格式化打印文本
  43. export const textFormat = function(command,text,maxFontNums,left,top,lightHeight,align,fontSize){
  44. let rowTop = top
  45. const maxNums = maxFontNums
  46. const rowHeight = lightHeight
  47. const textArr = getTextRows(text.replace(/\s+/g, ' '),maxNums*2)
  48. const rows = textArr.length
  49. for(let i=0;i<rows;i++){
  50. if(align=="center"){
  51. const ltxt = textArr[i]
  52. const ltlen = getBytesCount(ltxt)/2*fontSize
  53. command.setText(left+((maxNums-ltlen)/2)*rowHeight-2, rowTop, "TSS24.BF2", fontSize, fontSize, ltxt)
  54. }
  55. else{
  56. command.setText(left, rowTop, "TSS24.BF2", fontSize, fontSize, textArr[i])
  57. }
  58. rowTop = rowTop+rowHeight*fontSize+5
  59. }
  60. return rowTop
  61. }
  62. export const printText = function(tsc,text){
  63. const command = tsc.jpPrinter.createNew()
  64. command.init()
  65. command.setSize(40, 30) // 标签纸张宽高,单位mm
  66. command.setGap(3) // 标签上下间距,单位mm
  67. command.setCls() // 清除缓冲区数据
  68. command.setText(100, 48, "5", 4, 3, text)
  69. command.setPagePrint(1,1) // 打印分数1,每个标签重发打印2次
  70. return command
  71. }
  72. // 60 * 40 尺寸模板
  73. export const printTempl = function(tsc,data){
  74. let top = 32 // 距离顶部
  75. const left = 8 // 距离左边
  76. const lightHeight = 24 // 行高3mm,1mm = 8点
  77. const pageW = 60 // 页签宽度mm
  78. const pageH = 40 // 页签高度mm
  79. const maxFontNums = Math.floor((pageW*8-left*3)/lightHeight) // 每行最多字体数
  80. const leftMaxFnums = Math.floor(maxFontNums*0.4)
  81. const rightMaxFnums = leftMaxFnums + 1
  82. let rightLeft = (leftMaxFnums+0.5)*lightHeight
  83. // 初始化打印机
  84. const command = tsc.jpPrinter.createNew()
  85. command.init()
  86. command.setSize(pageW, pageH) // 标签纸张宽高,单位mm
  87. command.setGap(3) // 标签上下间距,单位mm
  88. command.setCls() // 清除缓冲区数据
  89. // 经销商文字高度,是否换行
  90. top = textFormat(command,data.dealerName,maxFontNums,left,top,lightHeight,"center",1)
  91. // 数字货架名称文字
  92. top = top+10
  93. top = textFormat(command,data.shelfName,maxFontNums,left,top,lightHeight,"center",1)
  94. // 二维码
  95. top = top+15
  96. command.setQR(left*2, top, "M", 5, "A", data.barCode)
  97. // 货位号
  98. top = top + 6
  99. const rightTop = textFormat(command,data.shelfPlaceCode,rightMaxFnums,rightLeft,top,lightHeight,"left",3)
  100. // 产品编码
  101. top = rightTop + 1
  102. top = textFormat(command,data.productCode,rightMaxFnums,rightLeft,top,lightHeight,"left",1)
  103. // 打印人打印时间
  104. top = top+3
  105. top = textFormat(command,data.printDate,rightMaxFnums,rightLeft,top,lightHeight,"left",1)
  106. // 打印分数1,每个标签重发打印2次
  107. command.setPagePrint(1,data.currentInven)
  108. return command
  109. }
  110. // 格式化cpcl打印文本
  111. export const textCpclFormat = function(cpcl,text,maxFontNums,left,top,lightHeight,align,fontName,fontSize,ellipsis=false){
  112. let rowTop = top
  113. const maxNums = maxFontNums*1.8
  114. const rowHeight = lightHeight
  115. const textArr = getTextRows(text.replace(/\s+/g, ' '),maxNums)
  116. const len = ellipsis ? 1 : textArr.length
  117. let result = ''
  118. for(let i=0;i<len;i++){
  119. const texts = !ellipsis||(textArr.length==1) ? textArr[i] : (textArr[i].substr(0,maxFontNums-3)+'...')
  120. if(align=="center"){
  121. result += cpcl.addCPCLLocation(2)
  122. }
  123. else{
  124. result += cpcl.addCPCLLocation(0)
  125. }
  126. result += cpcl.addCPCLText(left,rowTop,fontName,fontSize,0,texts);
  127. rowTop = rowTop+rowHeight+5
  128. }
  129. return {result:result,top:rowTop}
  130. }
  131. // 60 * 40 尺寸模板 cpcl 指令
  132. export const printCpclTempl = function(cpcl,data){
  133. console.log(cpcl)
  134. let top = 0 // 距离顶部
  135. const left = 8 // 距离左边
  136. const lightHeight = 24 // 行高3mm,1mm = 8点
  137. const pageW = 60 // 页签宽度mm
  138. const pageH = 40 // 页签高度mm
  139. const maxFontNums = Math.floor((pageW*8-left*2)/lightHeight) // 每行最多字体数
  140. const leftMaxFnums = Math.floor(maxFontNums*0.4)
  141. const rightMaxFnums = leftMaxFnums + 2
  142. let rightLeft = (leftMaxFnums+2)*lightHeight
  143. // 初始化打印机
  144. let strCmd =cpcl.CreatCPCLPage(pageW*8,pageH*8,data.currentInven,0,0);
  145. // 经销商文字高度,是否换行
  146. const a = textCpclFormat(cpcl,data.dealerName,maxFontNums,0,top,lightHeight,"center",'24',0);
  147. top = a.top;
  148. strCmd += a.result;
  149. // 数字货架名称文字
  150. top = top+10;
  151. const b = textCpclFormat(cpcl,data.shelfName,maxFontNums,0,top,lightHeight,"center",'24',0,true);
  152. top = b.top;
  153. strCmd += b.result;
  154. // 二维码
  155. top = top+15;
  156. strCmd += cpcl.addCPCLLocation(0);
  157. strCmd += cpcl.addCPCLQRCode(left*5,top,'M', 3, 5, data.barCode);
  158. // 货位号
  159. strCmd += cpcl.addCPCLSETMAG(4,4);
  160. const c = textCpclFormat(cpcl,data.shelfPlaceCode,rightMaxFnums,rightLeft,top,lightHeight,"left",'7',0);
  161. const rightTop = c.top;
  162. strCmd += c.result;
  163. // 产品编码
  164. top = rightTop + 65;
  165. strCmd += cpcl.addCPCLSETMAG(1,1);
  166. const d = textCpclFormat(cpcl,data.productCode,rightMaxFnums,rightLeft,top,lightHeight,"left",'7',0);
  167. top = d.top;
  168. strCmd += d.result;
  169. // 打印人打印时间
  170. top = top+4;
  171. strCmd += cpcl.addCPCLSETMAG(1,1);
  172. const e = textCpclFormat(cpcl,data.printDate,rightMaxFnums,rightLeft,top,lightHeight,"left",'7',0);
  173. top = e.top;
  174. strCmd += e.result;
  175. strCmd += 'FORM \n'
  176. strCmd += cpcl.addCPCLPrint();
  177. console.log(strCmd)
  178. return gbk.strToGBKByte(strCmd);
  179. }
  180. // 40 * 30 尺寸模板
  181. export const printMiniTempl = function(tsc,data){
  182. let top = 24 // 距离顶部
  183. const left = 4 // 距离左边
  184. const lightHeight = 24 // 行高3mm,1mm = 8点
  185. const pageW = 40 // 页签宽度mm
  186. const pageH = 30 // 页签高度mm
  187. const maxFontNums = Math.floor((pageW*8-left*2)/lightHeight) // 每行最多字体数
  188. const leftMaxFnums = Math.floor(maxFontNums*0.5)
  189. const rightMaxFnums = Math.floor(maxFontNums*0.4)
  190. let rightLeft = (leftMaxFnums+1)*lightHeight
  191. // 初始化打印机
  192. const command = tsc.jpPrinter.createNew()
  193. command.init()
  194. command.setSize(pageW, pageH) // 标签纸张宽高,单位mm
  195. command.setGap(3) // 标签上下间距,单位mm
  196. command.setCls() // 清除缓冲区数据
  197. // 经销商文字高度,是否换行
  198. top = textFormat(command,data.dealerName,maxFontNums,left,top,lightHeight,"center",1)
  199. // 数字货架名称文字
  200. top = top+10
  201. top = textFormat(command,data.shelfName,maxFontNums,left,top,lightHeight,"center",1)
  202. // 二维码
  203. top = top+10
  204. command.setQR(left, top, "M", 5, "A", data.barCode)
  205. // 货位号
  206. const rightTop = textFormat(command,data.shelfPlaceCode,rightMaxFnums,rightLeft,top,lightHeight,"left",3)
  207. // 产品编码
  208. top = rightTop
  209. top = textFormat(command,data.productCode,rightMaxFnums,rightLeft,top,lightHeight,"left",1)
  210. // 打印分数1,每个标签重发打印2次
  211. command.setPagePrint(1,data.currentInven)
  212. return command
  213. }