visit
Label printers can support programming languages like TSPL, ZPL, EPL, and so on. Today we are going to overview the TSPL language. We can build labels using TSPL commands like TEXT
, BARCODE
and QRCODE
. For instance, if we want to print a label with a text and barcode, we use these commands with their properties like position or size, and send these commands to the label printer over Bluetooth or Serial connection.
On the left side, you can see the TSPL commands and the printed label on the right side. You can find all the available commands , but let’s look at some of them to understand how to use the TSPL.
Whether it is a TEXT
, BARCODE
or BITMAP
, generally, the coordinates and size are in dots. The number of dots per inch depends on the printer's DPI.
According to this, if we want to add a barcode with a height of 10mm, and the printer is 203DPI, then we should set the height as 80 (10mm x 8 = 80 dots).
SIZE 4,1
SIZE 50 mm,25 mm
We can set the gap which is the space between labels (GAP m,n
).
GAP 0,0
We can use TEXT
command to print a text on the label. We can give the position, font size, rotation, and so on:
TEXT x,y,“font”,rotation,x-multiplication,y-multiplication,[alignment,] “content”
Parameter | Description |
---|---|
x, y | x and y-coordinate |
font | Generally, we can set 1-8 (1-small, 2-bigger… 8-biggest) |
rotation | 0, 90, 180, 270 in clockwise direction |
x and y-multiplication | Scale factor 1-10 |
alignment | 1-left, 2-center, 3-right (optional) |
content | Text content |
Sample commands | Result |
---|---|
|
|
We can add a barcode to the label with the BARCODE
command:
BARCODE X,Y,”code type”,height,human-readable,rotation,narrow,wide,[alignment,]”content”
Parameter | Description |
---|---|
x, y | x and y-coordinate |
code type | 128, EAN128, EAN13… |
height | Height in dots |
human-readable |
0 - barcode value (text) is not visible |
rotation | 0, 90, 180, 270 in clockwise direction |
narrow | Width of the narrow element in dots |
wide | Width of the wide element in dots |
alignment | 1-left, 2-center, 3-right (optional) |
content | Content of barcode |
Sample commands:
TEXT 10,10, "2",0,1,1, "Human readable alignment"
BARCODE 10,50, "128",100,1,0,2,2,"left"
BARCODE 310,50, "128",100,2,0,2,2,"center"
BARCODE 610,50, "128",100,3,0,2,2,"right"
Result:
After building the label we need to tell the printer that the label is ready to print. We use PRINT m[,n]
command to do this:
Commands | Description |
---|---|
|
- Set the size of the label |
We add END
command at the end, to tell the printer that we’ve finished printing. Without this command, the printer may not print the last image in the buffer.
const usb = require('usb');
const cmds = [
'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',
];
// you can get all available devices with usb.getDeviceList()
let device = usb.findByIds(/*vid*/8137, /*pid*/8214);
device.open();
device.interfaces[0].claim();
const outEndpoint = device.interfaces[0].endpoints.find(e => e.direction === 'out');
outEndpoint.transferType = 2;
outEndpoint.transfer(Buffer.from(cmds.join('\r\n')), (err) => {
device.close();
});