const repeat = (char, length) => Array(length + 1).join(char);
// padleft
function ljust(string, width, padding = " ") {
padding = padding.slice(0, 1);
if (string.length < width)
return string + repeat(padding, width - string.length);
else
return string;
}
// padright
function rjust(string, width, padding = " ") {
padding = padding.slice(0, 1);
if (string.length < width)
return repeat(padding, width - string.length) + string;
else
return string;
}
// padcenter
function center(string, width, padding = " ") {
padding = padding.slice(0, 1);
if (string.length < width) {
const len = width - string.length;
const remain = (len % 2 == 0) ? "" : padding;
const pads = repeat(padding, parseInt(len / 2));
return pads + string + pads + remain;
} else
return string;
}
const ALIGN = {
LEFT: 0,
CENTER: 1,
RIGHT: 2
};
const mergeLine = (line, newLineData) => {
let newStr = "";
for (let i = 0; i < newLineData.length; i += 1) {
if (newLineData[i] != ' ') {
newStr += newLineData[i] || ' ';
} else {
newStr += line[i] || ' ';
}
}
return newStr;
};
const emulatePrintBon = printerCommands => {
// printer character width
const bonCharWidth = 42;
// remove newline characters
const printerText = printerCommands.replace('\r', ' ').replace('\n', ' ');
// split on new line command
const commands = printerText.split('\x1B|N');
// regex om op printer commando te splitten
const regExCmd = /\x1B\|?([a-z0-9]*)([^\x1B]*)/gi;
const output = [];
let line = null;
// loop langs de printer regels
for (let i = 0; i < commands.length; i += 1) {
const command = commands[i];
// add current line to output
if (line) {
output.push(line);
}
// and start with a new empty line
line = repeat(' ', bonCharWidth);
// reset command options
let align = ALIGN.LEFT;
let bold = false;
// loop langs alle commando's
while (match = regExCmd.exec(command)) {
const cmd = match[1];
const text = (`${match[2]}`).trim();
if (cmd) {
switch (cmd) {
case 'lA':
align = ALIGN.LEFT;
break;
case 'rA':
align = ALIGN.RIGHT;
break;
case 'cA':
align = ALIGN.CENTER;
break;
case '1B': // begin bon?
break;
case 'fP': // fP (einde bon of knip bon?)
break;
case '2C': //?
break;
case 'bC':
bold = true;
break;
}
}
if (text != '') {
let newLinePart = '';
if (align === ALIGN.CENTER) {
newLinePart = center(text, bonCharWidth);
} else if (align === ALIGN.RIGHT) {
newLinePart = rjust(text, bonCharWidth);
} else {
newLinePart = ljust(text, bonCharWidth);
}
line = mergeLine(line, newLinePart)
}
}
}
if (line) {
output.push(line); // push last line
}
console.log(output.join('\n'));
//return output;
};
const generateTestBon = () => {
const filiaaladres = 'Street';
const filiaalpostcode = '1234AB';
const filiaalplaats = 'Rotterdam';
const filiaaltelnr = '123-4567890';
const filiaalemail = 'test@test.com';
const input = [
'\x1B' + '|1B',
'\x1B' + '|lA ' + 'li' + '\n',
`\x1B|cA ${filiaaladres}\n`,
'\x1B' + '|rA ' + 're' + '\n',
`\x1B|N\x1B|cA ${filiaalpostcode} ${filiaalplaats}\n`,
`\x1B|N\x1B|cA Tel:${filiaaltelnr} ${filiaalemail}\n\n`
];
// const tmp = CCPrinterInfo.split("#");
//
// for( var i = 0; i < tmp.length; i++) {
// if( i == 0 ){
// input.push('\x1B' +'__________________________________________\n');
// } else {
// if( (tmp[i] != '') && (tmp[i].search("----") == -1 ) )
// input.push('\x1B'+'|N'+tmp[i]+'\n');
// }
// }
//
// if( tmp.length == 1 ) {
// input.push('\x1B' +'Bon niet beschikbaar!\n');
// }
input.push('\x1B' + '__________________________________________\n');
input.push('\x1B' + '|fP');
return input.join('');
};
emulatePrintBon(generateTestBon());
Output:
li Street re
1234AB Rotterdam
__________________________________________
73600cookie-checkJavascript Epson label printer emulator