import { Buffer } from "buffer"; import _, {floor} from "lodash"; const statics: any = { 'ESC': '1B', 'FF': '0C' } const mmToInch = 0.0393701; // 26x51 label, or 1" x 2" const pageWidth = getPageDimensionParameters(2); const pageHeight = getPageDimensionParameters(1); const command = ` ESC i a 00h ESC @ ESC i L 01h ESC ( C 02h 00h ${pageWidth.mL} ${pageWidth.mH} ESC $ CBh 00h ESC ( V 02h 00h ${pageHeight.mL} ${pageHeight.mH} ESC k 0Bh ESC X 00h 40h 00h ESC i t0 r0 h E0h 00h w1 e0 z0 f1 B 123456789 \\ FF `; console.warn('output: ', parseEscToHex(command)) function inchToDots(value: number, dpi = 203) { const dots = value * dpi; const margin = 6 * mmToInch * dpi; return Math.round(dots - margin); } function toHex(value: number) { return value.toString(16).padStart(2, '0') + 'h'; } function getPageDimensionParameters(inInches: number) { const dots = inchToDots(inInches); const mH = Math.floor(dots / 265); const mL = dots - mH * 265; return { mH: toHex(mH), mL: toHex(mL) } } function parseEscToHex(text: string): string { return text.trim().split(/\s+/) .map(chunk => { if (_.has(statics, chunk)) { // Found static mapping return statics[chunk] } if(chunk.length == 3 && chunk.endsWith('h')) { // parse as hex return chunk.slice(0, -1) } return Buffer.from(chunk).toString('hex') }) .join(' ') }