visit
In the beginning, we can set the size and print multiple labels. This won’t be an issue because every label at the label roll has the same size. But the situation is different when printing receipts because every receipt can have a different height.
Generally, receipt printers use language like ESC/POS. One of the differences between TSPL and ESC/POS is we need to set the size of the receipt while printing. So when we build a receipt with the commands like TEXT
, BARCODE
, and BITMAP
we need to calculate the height of the receipt according to the items that we have.
TEXT x,y,“font”,rotation,x-multiplication,y-multiplication,[alignment,] “content”
1: 8 x 12 fixed pitch dot font 2: 12 x 20 fixed pitch dot font 3: 16 x 24 fixed pitch dot font 4: 24 x 32 fixed pitch dot font 5: 32 x 48 dot fixed pitch font 6: 14 x 19 dot fixed pitch font OCR-B 7: 21 x 27 dot fixed pitch font OCR-B 8: 14 x25 dot fixed pitch font OCR-A
TEXT 10,10,"2",0,1,1,"FONT 2"
y + [font height]*[y-multiplication]
10 + 20*1 = 30dots
To prevent this, we can reset the printer with the command <ESC>!R
, and it will use our new size:
<ESC>!R
SIZE 48 mm,25 mm
CLS
TEXT 10,10,"4",0,1,1,"HackerNoon"
BARCODE 10,60,"128",90,1,0,2,2,"altospos.com"
PRINT 1
END
I created a simple example that prints two receipts with different heights. The first receipt’s height is about 90 dots (including paddings), and with the 203 DPI printer, it will be around 11 mm (learn more). And the second receipt’s height is 21 mm. If we do not reset the printer (<ESC>!R
), the second one will also have a height of 11 mm and the remaining part won’t be printed.
const usb = require('usb');
(async function() {
await print([
'<ESC>!R',
'SIZE 80 mm,11 mm',
'CLS',
'TEXT 10,10,"4",0,1,1,"HackerNoon"',
'TEXT 10,47,"1",0,1,1,"Top Stories"',
'TEXT 10,64,"1",0,1,1,"Programming"',
'PRINT 1',
'END',
]);
await print([
'<ESC>!R',
'SIZE 80 mm,21 mm',
'CLS',
`TEXT 10,10,"4",0,1,1,"Alto's POS & Inventory"`,
'TEXT 10,50,"2",0,1,1,"Restaurants & Cafes"',
'TEXT 10,75,"2",0,1,1,"Retail Stores"',
'TEXT 10,100,"2",0,1,1,"Services"',
'TEXT 10,125,"1",0,1,1,"//altospos.com"',
'PRINT 1',
'END',
]);
})();
function print(cmds) {
return new Promise(resolve => {
// you can get all available devices with usb.getDeviceList()
let device = usb.findByIds(/*vid*/8137, /*pid*/8214);
device.open();
const interface = device.interfaces[0];
interface.claim();
const outEndpoint = interface.endpoints.find(e => e.direction === 'out');
outEndpoint.transferType = 2;
outEndpoint.transfer(Buffer.from(cmds.join('\r\n')), (err) => {
device.close();
resolve();
});
});
}