nitro-print-service-sdk
v1.0.2
Published
JavaScript/TypeScript client for Nitro local printer service
Maintainers
Readme
Nitro Print Service SDK
A lightweight TypeScript/JavaScript SDK for communicating with the Nitro Print Service to discover printers and send print jobs.
Installation
npm install nitro-print-service-sdkRequirements
Before using this package, make sure your Nitro Print Service is running on the machine.
The SDK communicates with:
http://127.0.0.1:9191Important: Print Data
The print() function expects the print data as a Base64-encoded string.
You need to create the raw ESC/P commands for your printer, convert them into a Buffer, and then convert that buffer to Base64 before sending it to the SDK.
For example:
const label = new BrotherLabelBuilder()
// --- Item Number ---
.align("center")
.bold(true)
.fontSanSerif()
.fontSize(35)
.text(`#: ${data.itemNumber}`)
.lineBreak(1)
.bold(false)
// --- Details ---
.align("left")
.fontSize(22)
.lineSpacing(40)
.text(`Name: ${data.itemName}\n`)
.text(`Stream: ${data.streamName.toUpperCase()}\n`)
.text(`Date: ${formattedDate} ${formattedTime}`);
// Build the raw ESC/P binary payload
const payloadBuffer: Buffer = label.buildPayload();
// Convert the raw payload to Base64
const base64Data = payloadBuffer.toString("base64");You can then pass the Base64 data to print():
import { print } from "nitro-print-service-sdk";
const result = await print({
printerName: "Brother QL-820NWB",
base64Data,
});
console.log(result);If you don't specify printerName, the Nitro Print Service can use the default printer:
const result = await print({
base64Data,
});Get Available Printers
Use getPrinters() to retrieve the printers available on the machine:
import { getPrinters } from "nitro-print-service-sdk";
try {
const printers = await getPrinters();
console.log(printers);
} catch (error) {
console.error("Failed to get printers:", error);
}Example response:
[
{
name: "Brother QL-820NWB",
displayName: "Brother QL-820NWB",
description: "Brother Label Printer",
isDefault: true
}
]The print() function accepts a PrintRequest:
interface PrintRequest {
printerName?: string;
base64Data: string;
}Example:
import { print } from "nitro-print-service-sdk";
try {
const result = await print({
printerName: "Brother QL-820NWB",
base64Data,
});
console.log(result);
} catch (error) {
console.error("Print failed:", error);
}API
getPrinters()
Returns all available printers.
getPrinters(): Promise<Printer[]>print(request)
Sends the Base64-encoded ESC/P print data to the selected printer.
print(request: PrintRequest): Promise<PrintResponse>Types
Printer
interface Printer {
name: string;
displayName?: string;
description?: string;
isDefault: boolean;
}PrintRequest
interface PrintRequest {
printerName?: string;
base64Data: string;
}PrintResponse
interface PrintResponse {
status: "success" | "error";
message: string;
}PrintersResponse
interface PrintersResponse {
status: "success" | "error";
message?: string;
defaultPrinter: string | null;
printers: Printer[];
}Error Handling
Both getPrinters() and print() throw an Error when the request fails.
try {
const printers = await getPrinters();
const result = await print({
base64Data,
});
} catch (error) {
console.error(error);
}Local Nitro Print Service
Make sure the Nitro Print Service is running before using this SDK.
The SDK communicates with:
http://127.0.0.1:9191Available endpoints:
GET /printers
POST /printThe SDK does not start the Nitro Print Service. The service must already be running on the user's machine.
