printTools.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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*4)/lightHeight) // 每行最多字体数
  80. const leftMaxFnums = Math.floor(maxFontNums*0.4)
  81. const rightMaxFnums = leftMaxFnums + 1
  82. let rightLeft = (leftMaxFnums+2.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*6, top, "M", 5, "A", data.barCode)
  97. // 货位号
  98. const rightTop = textFormat(command,data.shelfPlaceCode,rightMaxFnums,rightLeft,top,lightHeight,"left",4)
  99. // 产品编码
  100. top = rightTop + 1
  101. top = textFormat(command,data.productCode,rightMaxFnums,rightLeft,top,lightHeight,"left",1)
  102. // 打印人打印时间
  103. top = top+3
  104. top = textFormat(command,data.printDate,rightMaxFnums,rightLeft,top,lightHeight,"left",1)
  105. // 打印分数1,每个标签重发打印2次
  106. command.setPagePrint(1,data.currentInven)
  107. return command
  108. }
  109. // 格式化cpcl打印文本
  110. export const textCpclFormat = function(cpcl,text,maxFontNums,left,top,lightHeight,align,fontName,fontSize){
  111. let rowTop = top
  112. const maxNums = maxFontNums
  113. const rowHeight = lightHeight
  114. const textArr = getTextRows(text.replace(/\s+/g, ' '),maxNums*2)
  115. const rows = textArr.length
  116. let result = ''
  117. for(let i=0;i<rows;i++){
  118. if(align=="center"){
  119. result += cpcl.addCPCLLocation(2)
  120. }
  121. else{
  122. result += cpcl.addCPCLLocation(0)
  123. }
  124. result += cpcl.addCPCLText(left,rowTop,fontName,fontSize,0,textArr[i]);
  125. rowTop = rowTop+rowHeight+5
  126. }
  127. return {result:result,top:rowTop}
  128. }
  129. // 60 * 40 尺寸模板 cpcl 指令
  130. export const printCpclTempl = function(cpcl,data){
  131. console.log(cpcl)
  132. let top = 0 // 距离顶部
  133. const left = 8 // 距离左边
  134. const lightHeight = 24 // 行高3mm,1mm = 8点
  135. const pageW = 60 // 页签宽度mm
  136. const pageH = 40 // 页签高度mm
  137. const maxFontNums = Math.floor((pageW*8-left*4)/lightHeight) // 每行最多字体数
  138. const leftMaxFnums = Math.floor(maxFontNums*0.4)
  139. const rightMaxFnums = leftMaxFnums + 2
  140. let rightLeft = (leftMaxFnums+2)*lightHeight
  141. // 初始化打印机
  142. let strCmd =cpcl.CreatCPCLPage(pageW*8,pageH*8,data.currentInven,0);
  143. // 经销商文字高度,是否换行
  144. const a = textCpclFormat(cpcl,data.dealerName,maxFontNums,0,top,lightHeight,"center",'24',0);
  145. top = a.top;
  146. strCmd += a.result;
  147. // 数字货架名称文字
  148. top = top+10;
  149. const b = textCpclFormat(cpcl,data.shelfName,maxFontNums,0,top,lightHeight,"center",'24',0);
  150. top = b.top;
  151. strCmd += b.result;
  152. // 二维码
  153. top = top+15;
  154. strCmd += cpcl.addCPCLLocation(0);
  155. strCmd += cpcl.addCPCLQRCode(left*5,top,'M', 3, 5, data.barCode);
  156. // 货位号
  157. strCmd += cpcl.addCPCLSETMAG(4,4);
  158. const c = textCpclFormat(cpcl,data.shelfPlaceCode,rightMaxFnums,rightLeft,top,lightHeight,"left",'7',0);
  159. const rightTop = c.top;
  160. strCmd += c.result;
  161. // 产品编码
  162. top = rightTop + 65;
  163. strCmd += cpcl.addCPCLSETMAG(1,1);
  164. const d = textCpclFormat(cpcl,data.productCode,rightMaxFnums,rightLeft,top,lightHeight,"left",'7',0);
  165. top = d.top;
  166. strCmd += d.result;
  167. // 打印人打印时间
  168. top = top+4;
  169. strCmd += cpcl.addCPCLSETMAG(1,1);
  170. const e = textCpclFormat(cpcl,data.printDate,rightMaxFnums,rightLeft,top,lightHeight,"left",'7',0);
  171. top = e.top;
  172. strCmd += e.result;
  173. strCmd += 'FORM \n'
  174. strCmd += cpcl.addCPCLPrint();
  175. console.log(strCmd)
  176. return gbk.strToGBKByte(strCmd);
  177. }
  178. // 40 * 30 尺寸模板
  179. export const printMiniTempl = function(tsc,data){
  180. let top = 24 // 距离顶部
  181. const left = 4 // 距离左边
  182. const lightHeight = 24 // 行高3mm,1mm = 8点
  183. const pageW = 40 // 页签宽度mm
  184. const pageH = 30 // 页签高度mm
  185. const maxFontNums = Math.floor((pageW*8-left*2)/lightHeight) // 每行最多字体数
  186. const leftMaxFnums = Math.floor(maxFontNums*0.5)
  187. const rightMaxFnums = Math.floor(maxFontNums*0.4)
  188. let rightLeft = (leftMaxFnums+1)*lightHeight
  189. // 初始化打印机
  190. const command = tsc.jpPrinter.createNew()
  191. command.init()
  192. command.setSize(pageW, pageH) // 标签纸张宽高,单位mm
  193. command.setGap(3) // 标签上下间距,单位mm
  194. command.setCls() // 清除缓冲区数据
  195. // 经销商文字高度,是否换行
  196. top = textFormat(command,data.dealerName,maxFontNums,left,top,lightHeight,"center",1)
  197. // 数字货架名称文字
  198. top = top+10
  199. top = textFormat(command,data.shelfName,maxFontNums,left,top,lightHeight,"center",1)
  200. // 二维码
  201. top = top+10
  202. command.setQR(left, top, "M", 5, "A", data.barCode)
  203. // 货位号
  204. const rightTop = textFormat(command,data.shelfPlaceCode,rightMaxFnums,rightLeft,top,lightHeight,"left",3)
  205. // 产品编码
  206. top = rightTop
  207. top = textFormat(command,data.productCode,rightMaxFnums,rightLeft,top,lightHeight,"left",1)
  208. // 打印分数1,每个标签重发打印2次
  209. command.setPagePrint(1,data.currentInven)
  210. return command
  211. }