@cutos/device-tspl-printer
v3.4.1
Published
@cutos/device-tspl-printer is a JavaScript library that provides a unified interface for accessing and controlling printers that support the TSPL command set.
Readme
Introduction
@cutos/device-tspl-printer is a JavaScript library that provides a unified interface for accessing and controlling printers that support the TSPL command set. Developers can use this interface to send and receive data, configure printer parameters, and handle printer events.
TSPL Printer SDK
Installation
npm install @cutos/core
npm install @cutos/device-tspl-printer
Import dependencies
import {CoreAPI} from '@cutos/core';
import {DeviceTSPLPrinter} from '@cutos/device-tspl-printer';DeviceTSPLPrinter
Constructor, create TSPL printer device instance
let devTSPLPrinter = new DeviceTSPLPrinter(name);- name: TSPL Printer device name
Example:
devTSPLPrinter = new DeviceTSPLPrinter('device-tspl-printer');DeviceTSPLPrinter.init
TSPL Printer device initialization
devTSPLPrinter.init(callback);- callback: callback function
Example:
devTSPLPrinter.init((result, error) => {
if (!error) {
console.log('onDeviceCreate', result)
} else {
console.log(error)
}
});DeviceTSPLPrinter.connect
Connect TSPL Printer
devTSPLPrinter.connect(path, callback);- path: device port
- callback: callback function
Example:
devTSPLPrinter.connect('/ttyS1', (response) => {
console.log(response)
})- Return response example:
{
"status": true,
"msg": "already connected"
}DeviceTSPLPrinter.setLabelSize
Set the label size (width and height) for printing
devTSPLPrinter.setLabelSize(width, height);- width: label width
- height: label height
DeviceTSPLPrinter.setLabelGap
Configures the gap between consecutive labels (for gap-sensing printers)
devTSPLPrinter.setLabelGap(gap);- gap: label gap,unit:mm
DeviceTSPLPrinter.setDirection
Defines the printout direction and mirror image.
devTSPLPrinter.setDirection(direction, mirror)- direction: Feed Direction 0 or 1
- mirror: 0:Print normal image;1: Print mirror image
Example:
devTSPLPrinter.setDirection(0, 0)DeviceTSPLPrinter.setClearBuff
Clears the printer's command buffer
devTSPLPrinter.setClearBuff()DeviceTSPLPrinter.addBox
Draws a rectangle/box on the label.
devTSPLPrinter.addBox(x, y, width, height, thick)- x:Specify x-coordinate of upper left corner (in dots)
- y:Specify y-coordinate of upper left corner (in dots)
- width:Specify x-coordinate of lower right corner (in dots)
- height:Specify y-coordinate of lower right corner (in dots)
- thick:Line thickness (in dots)
Example:
devTSPLPrinter.addBox(0, 0, 46 * 8, 37 * 8, 8)DeviceTSPLPrinter.addText
devTSPLPrinter.addText(text, x, y)- text: prints text on label
- x:The x-coordinate of the text
- y:The y-coordinate of the text
Example:
devTSPLPrinter.addText("hello world", 64, 224)DeviceTSPLPrinter.addQRCode
devTSPLPrinter.addQRCode(content, x, y)- content: QR code content
- x:The upper left corner x-coordinate of the QR code
- y:The upper left corner y-coordinate of the QR code
Example:
devTSPLPrinter.addQRCode('POWERED BY CUTOS', 136, 50)DeviceTSPLPrinter.print
Print content
devTSPLPrinter.print(callback)- callback: callback function
Example:
devTSPLPrinter.print((result, error) => {
console.log(result, error)
})