I've been struggling with this for a while.
I'm trying to print using ESC / POS commands directly to a thermal printer.
I'm sending the data from node, using either the node-printer module:
https://www.npmjs.com/package/printer
or the ipp module
https://www.npmjs.com/package/ipp
Both give me the exact same behaviour, which is that the first few commands that I send seem to be completely ignored.
For the sake of testing, I used commands from this tutorial:
https://www.neodynamic.com/articles/How-to-print-raw-ESC-POS-commands-from-Javascript/
So for example, in my node program I could have:
let esc = '\x1B'; //ESC byte in hex notation
let newLine = '\x0A'; //LF byte in hex notation
let cmds = esc + "@"; //Initializes the printer (ESC @)
cmds += esc + '!' + '\x38'; //Emphasized + Double-height + Double-width mode selected (ESC ! (8 + 16 + 32)) 56 dec => 38 hex
cmds += 'BEST DEAL STORES'; //text to print
cmds += newLine + newLine;
cmds += esc + '!' + '\x00'; //Character font A selected (ESC ! 0)
cmds += 'COOKIES 5.00';
cmds += newLine;
cmds += 'MILK 65 Fl oz 3.78';
cmds += newLine + newLine;
cmds += 'SUBTOTAL 8.78';
cmds += newLine;
cmds += 'TAX 5% 0.44';
cmds += newLine;
cmds += 'TOTAL 9.22';
cmds += newLine;
cmds += 'CASH TEND 10.00';
cmds += newLine;
cmds += 'CASH DUE 0.78';
cmds += newLine + newLine;
cmds += esc + '!' + '\x18'; //Emphasized + Double-height mode selected (ESC ! (16 + 8)) 24 dec => 18 hex
cmds += '# ITEMS SOLD 2';
cmds += esc + '!' + '\x00'; //Character font A selected (ESC ! 0)
cmds += newLine + newLine;
cmds += '11/03/13 19:53:17';
Then send those commands directly to the printer. Using node-printer (taken straight from their print_raw example):
let printer = require("printer");
this.printer.printDirect({data: cmds
, type: 'RAW' // type: RAW, TEXT, PDF, JPEG, .. depends on platform
, success:function(jobID: any){
console.log("sent to printer with ID: "+jobID);
}
, error:function(err: any){console.log(err);}
});
Or using ipp:
const ipp = require('ipp');
const uri = "ipp://localhost/printers/myPrinter";
let msg = {
"operation-attributes-tag": {
"requesting-user-name": "William",
"job-name": "My Test Job",
"document-format": "text/plain"
},
data: new Buffer(cmds)
};
const rawPrinter = ipp.Printer(uri);
rawPrinter.execute("Print-Job", msg, function(err: any, res: any){
console.log(res);
});
With both of these techniques, for some reason printing only begins at 'FL oz' (the milk item on the fake receipt). Any commands before that (including alignment or font selection) are completely ignored.
Now if I add a bunch of commands before that, using something like
let stupid = 40;
while (stupid > 0) {
cmds += esc;
stupid--;
}
Then I do get the whole receipt printed. But this feels reeeeeeeaaaaaally hacky and will almost definitely come back to haunt me. Is there some other message I need to send to the printer for it to read the commands from the beginning? Maybe I need to format the commands in a better way?
I'm running this on Ubuntu, and the printer is a Bixolon thermal printer 350III, whose commands can be found here:
http://www.bixolon.com/upload/download/unified%20command%20manual_rev_1_01.pdf
Obviously I'm pretty new to this, and I'm desperately hoping that I'm missing something obvious.