@capvision/printer-pos
v1.3.0
Published
Library that offers easy API to communicate with point-of-sales printers, by serial way (usb), TCP/IP (wifi) and bluetooth.
Readme
Node POS
Library that offers easy API to communicate with point-of-sales printers, by serial way (usb), TCP/IP (wifi) and bluetooth.
Usage
import { AbstractAdapter, SerialAdapter, TcpAdapter, USBAdapter, listPrinters, Printer } from "@capvision/printer-pos";
const printers = await listPrinters();
const selectedPrinter = printers[0];
let adapter: AbstractAdapter;
if(selectedPrinter.type === "USB") {
adapter = new USBAdapter({
vendorId: selectedPrinter.vendorId,
productId: selectedPrinter.productId,
});
}
else if(selectedPrinter.type === "Network") {
adapter = new TcpAdapter({
host: selectedPrinter.host,
port: selectedPrinter.port, // optional, defaults to 9100
});
}
else {
adapter = new SerialAdapter({
path: selectedPrinter.path,
});
}
// or directly adapter = new *Adapter(selectedPrinter);
const printer = new Printer(adapter);
await printer.open();
printer.setDefaultStyle({
align: "left",
bold: false,
underline: false,
double: false,
font: "a",
});
await printer
.text("=== Printing Test ===\n", { bold: true, underline: true })
.text("Hello, this is a printing test.\n", { align: "center" })
.text("We thank you for using our POS solution !\n\n", { font: "b" })
.cut()
.flush();
await printer.close();Even easier :
import { AdapterFactory, listPrinters, Printer } from "@capvision/printer-pos";
const printers = await listPrinters();
const selectedPrinter = printers[0];
const adapter = AdapterFactory.create(selectedPrinter);
const printer = new Printer(adapter);Network (TCP/IP) configuration
Ethernet / Wi-Fi printers are driven over a RAW ESC/POS socket — the de-facto standard "port 9100" transport (a.k.a. Epson RAW / JetDirect). Provide the host and, optionally, the port (default 9100):
const adapter = new TcpAdapter({ host: "192.168.1.50", port: 9100 });AdapterFactory.create(printerInfo) builds a TcpAdapter when
printerInfo.type === "Network", forwarding printerInfo.host and
printerInfo.port. Network printers are not auto-discovered by
listPrinters() (they require an IP/host known to the operator); the host app is
expected to let the user enter the host/port and store it on PrinterInfo.
Status read-back (DLE EOT) is best-effort: many network printers do not answer,
in which case Printer.getStatus() resolves to null.
Text size
Character magnification uses GS ! n (ESC/POS), supporting independent
width/height multipliers from 1× to 8×:
printer.text("TITLE", { widthScale: 2, heightScale: 2 }); // double size
printer.text("HUGE", { widthScale: 3, heightScale: 4 }); // 3× wide, 4× tallThe legacy boolean double flag still works (equivalent to 2×2) but explicit
widthScale/heightScale take precedence when set. To map a logical point size
(e.g. coming from a layout) onto the discrete POS paliers, use
fontSizeToScale(fontSize, baseFontSize?) (baseline POS_BASE_FONT_SIZE = 10pt
→ 1×).
Serial configuration
Serial / Bluetooth printers default to 9600 8N1. Override (and persist) the link parameters per printer:
const adapter = new SerialAdapter({ path: "COM5", baudRate: 19200, parity: "even" });AdapterFactory.create(printerInfo) forwards printerInfo.serial to the
adapter, so storing a known-good configuration on PrinterInfo.serial avoids
re-discovering it on every print.
Best-effort auto-detection (for printers that answer DLE EOT):
import { detectPrinterSerialConfig } from "@capvision/printer-pos";
const params = await detectPrinterSerialConfig("COM5"); // SerialParams | nullText is encoded with the printer's code page (default cp1252, covering
French accents and the € sign); change it via new Printer(adapter, { codePage: "cp858" }).
Notes
USB devices can work with Serial Port, or native USB.
Bluetooth devices almost always work Serial Port.
