{"id":736,"date":"2017-05-03T15:01:10","date_gmt":"2017-05-03T14:01:10","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=736"},"modified":"2022-07-25T08:35:45","modified_gmt":"2022-07-25T07:35:45","slug":"javascript-epson-printer-emulator","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-epson-printer-emulator\/","title":{"rendered":"Javascript Epson label printer emulator"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const repeat = (char, length) => Array(length + 1).join(char);\n\/\/ padleft\nfunction ljust(string, width, padding = \" \") {\n\tpadding = padding.slice(0, 1);\n\tif (string.length &lt; width)\n\t\treturn string + repeat(padding, width - string.length);\n\telse\n\t\treturn string;\n}\n\/\/ padright\nfunction rjust(string, width, padding = \" \") {\n\tpadding = padding.slice(0, 1);\n\tif (string.length &lt; width)\n\t\treturn repeat(padding, width - string.length) + string;\n\telse\n\t\treturn string;\n}\n\/\/ padcenter\nfunction center(string, width, padding = \" \") {\n\tpadding = padding.slice(0, 1);\n\tif (string.length &lt; width) {\n\t\tconst len = width - string.length;\n\t\tconst remain = (len % 2 == 0) ? \"\" : padding;\n\t\tconst pads = repeat(padding, parseInt(len \/ 2));\n\t\treturn pads + string + pads + remain;\n\t} else\n\t\treturn string;\n}\nconst ALIGN = {\n\tLEFT: 0,\n\tCENTER: 1,\n\tRIGHT: 2\n};\nconst mergeLine = (line, newLineData) => {\n\tlet newStr = \"\";\n\tfor (let i = 0; i &lt; newLineData.length; i += 1) {\n\t\tif (newLineData[i] != ' ') {\n\t\t\tnewStr += newLineData[i] || ' ';\n\t\t} else {\n\t\t\tnewStr += line[i] || ' ';\n\t\t}\n\t}\n\treturn newStr;\n};\nconst emulatePrintBon = printerCommands => {\n\t\/\/ printer character width\n\tconst bonCharWidth = 42;\n\t\/\/ remove newline characters\n\tconst printerText = printerCommands.replace('\\r', ' ').replace('\\n', ' ');\n\t\/\/ split on new line command\n\tconst commands = printerText.split('\\x1B|N');\n\t\/\/ regex om op printer commando te splitten\n\tconst regExCmd = \/\\x1B\\|?([a-z0-9]*)([^\\x1B]*)\/gi;\n\tconst output = [];\n\tlet line = null;\n\t\/\/ loop langs de printer regels\n\tfor (let i = 0; i &lt; commands.length; i += 1) {\n\t\tconst command = commands[i];\n\t\t\/\/ add current line to output\n\t\tif (line) {\n\t\t\toutput.push(line);\n\t\t}\n\t\t\/\/ and start with a new empty line\n\t\tline = repeat(' ', bonCharWidth);\n\t\t\/\/ reset command options\n\t\tlet align = ALIGN.LEFT;\n\t\tlet bold = false;\n\t\t\/\/ loop langs alle commando's\n\t\twhile (match = regExCmd.exec(command)) {\n\t\t\tconst cmd = match[1];\n\t\t\tconst text = (`${match[2]}`).trim();\n\t\t\tif (cmd) {\n\t\t\t\tswitch (cmd) {\n\t\t\t\t\tcase 'lA':\n\t\t\t\t\t\talign = ALIGN.LEFT;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'rA':\n\t\t\t\t\t\talign = ALIGN.RIGHT;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'cA':\n\t\t\t\t\t\talign = ALIGN.CENTER;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase '1B': \/\/ begin bon?\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'fP': \/\/ fP (einde bon of knip bon?)\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase '2C': \/\/?\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'bC':\n\t\t\t\t\t\tbold = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (text != '') {\n\t\t\t\tlet newLinePart = '';\n\t\t\t\tif (align === ALIGN.CENTER) {\n\t\t\t\t\tnewLinePart = center(text, bonCharWidth);\n\t\t\t\t} else if (align === ALIGN.RIGHT) {\n\t\t\t\t\tnewLinePart = rjust(text, bonCharWidth);\n\t\t\t\t} else {\n\t\t\t\t\tnewLinePart = ljust(text, bonCharWidth);\n\t\t\t\t}\n\t\t\t\tline = mergeLine(line, newLinePart)\n\t\t\t}\n\t\t}\n\t}\n\tif (line) {\n\t\toutput.push(line); \/\/ push last line\n\t}\n\tconsole.log(output.join('\\n'));\n\t\/\/return output;\n};\nconst generateTestBon = () => {\n\tconst filiaaladres = 'Street';\n\tconst filiaalpostcode = '1234AB';\n\tconst filiaalplaats = 'Rotterdam';\n\tconst filiaaltelnr = '123-4567890';\n\tconst filiaalemail = 'test@test.com';\n\tconst input = [ \n\t\t'\\x1B' + '|1B',\n\t\t'\\x1B' + '|lA ' + 'li' + '\\n',\n\t\t`\\x1B|cA ${filiaaladres}\\n`,\n\t\t'\\x1B' + '|rA ' + 're' + '\\n',\n\t\t`\\x1B|N\\x1B|cA ${filiaalpostcode} ${filiaalplaats}\\n`,\n\t\t`\\x1B|N\\x1B|cA Tel:${filiaaltelnr} ${filiaalemail}\\n\\n`\n\t];\n\t\/\/ const tmp = CCPrinterInfo.split(\"#\");\n\t\/\/\n\t\/\/ for( var i = 0; i &lt; tmp.length; i++) {\n\t\/\/  if( i == 0 ){\n\t\/\/      input.push('\\x1B' +'__________________________________________\\n');\n\t\/\/  } else {\n\t\/\/      if( (tmp[i] != '') &amp;&amp; (tmp[i].search(\"----\") == -1 ) ) \n\t\/\/          input.push('\\x1B'+'|N'+tmp[i]+'\\n');\n\t\/\/  }\n\t\/\/ }\n\t\/\/\n\t\/\/ if( tmp.length == 1 ) {\n\t\/\/  input.push('\\x1B' +'Bon niet beschikbaar!\\n');\n\t\/\/ }\n\tinput.push('\\x1B' + '__________________________________________\\n');\n\tinput.push('\\x1B' + '|fP');\n\treturn input.join('');\n};\n\nemulatePrintBon(generateTestBon());<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">li                Street                re\n             1234AB Rotterdam             \n__________________________________________<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Output:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-736","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=736"}],"version-history":[{"count":6,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/736\/revisions"}],"predecessor-version":[{"id":5363,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/736\/revisions\/5363"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}