node-thermal-printer-js 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -2
- package/app.js +198 -200
- package/package.json +3 -4
- package/.env.example +0 -16
package/README.md
CHANGED
|
@@ -23,12 +23,20 @@ npm install node-thermal-printer-js
|
|
|
23
23
|
```js
|
|
24
24
|
import { printData } from "node-thermal-printer-js";
|
|
25
25
|
|
|
26
|
-
await printData("Hello
|
|
26
|
+
await printData("Hello World", {
|
|
27
|
+
transport: "ble", // or "com"
|
|
28
|
+
bleName: "PSF588",
|
|
29
|
+
connectTimeout: 15,
|
|
30
|
+
scanTimeout: 10,
|
|
31
|
+
portPath: "COM3", // for "com" transport
|
|
32
|
+
charUUID: "0000ffe1-0000-1000-8000-00805f9b34fb", // for "ble" transport
|
|
33
|
+
});
|
|
27
34
|
```
|
|
28
35
|
|
|
29
36
|
## Local Development
|
|
30
37
|
|
|
31
38
|
This project can print ESC/POS data to PSF588 using either:
|
|
39
|
+
|
|
32
40
|
- BLE via Python bridge (`ble_print.py` + `bleak`)
|
|
33
41
|
- Classic Bluetooth Serial (COM port) via `serialport`
|
|
34
42
|
|
|
@@ -62,6 +70,7 @@ node test-print.js ble ORDER123 PSF588 "AA:BB:CC:DD:EE:FF" "49535343-8841-43f4-a
|
|
|
62
70
|
```
|
|
63
71
|
|
|
64
72
|
Args order for BLE:
|
|
73
|
+
|
|
65
74
|
1. `ble`
|
|
66
75
|
2. `orderId` (optional)
|
|
67
76
|
3. `bleName` (optional, default: `PSF588`)
|
|
@@ -115,4 +124,3 @@ npm publish --access public
|
|
|
115
124
|
- **"Access Denied" on write:** Characteristic doesn't support writes. Use `node test-print.js scan PSF588` to find a writable one.
|
|
116
125
|
- **Connection timeout:** Keep printer in pairing mode, disconnect from other devices, ensure Bluetooth is enabled on Windows.
|
|
117
126
|
- **Python not found:** Install Python 3.11+ and ensure it's on PATH, or set `PRINTER_PYTHON_CMD=py -3.11`.
|
|
118
|
-
|
package/app.js
CHANGED
|
@@ -1,200 +1,198 @@
|
|
|
1
|
-
import "dotenv/config";
|
|
2
|
-
import { spawn } from "node:child_process";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
|
|
6
|
-
const getEscPosPayload = (data) =>
|
|
7
|
-
Buffer.concat([
|
|
8
|
-
Buffer.from([0x1b, 0x40, 0x0a]),
|
|
9
|
-
Buffer.from(`${data}\n\n\n\n\n`, "utf-8"),
|
|
10
|
-
]);
|
|
11
|
-
|
|
12
|
-
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
|
13
|
-
const bleScriptPath = path.join(scriptDir, "ble_print.py");
|
|
14
|
-
|
|
15
|
-
const runPythonProcess = ({ cmd, cmdArgs, scriptArgs }) =>
|
|
16
|
-
new Promise((resolve, reject) => {
|
|
17
|
-
const child = spawn(cmd, [...cmdArgs, ...scriptArgs], {
|
|
18
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
let stdout = "";
|
|
22
|
-
let stderr = "";
|
|
23
|
-
|
|
24
|
-
child.stdout.on("data", (chunk) => {
|
|
25
|
-
stdout += chunk.toString();
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
child.stderr.on("data", (chunk) => {
|
|
29
|
-
stderr += chunk.toString();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
child.on("error", (err) => {
|
|
33
|
-
reject(new Error(`Launch error for ${cmd}: ${err.message}`));
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
child.on("close", (code) => {
|
|
37
|
-
if (code === 0) {
|
|
38
|
-
resolve({
|
|
39
|
-
ok: true,
|
|
40
|
-
stdout: stdout.trim(),
|
|
41
|
-
stderr: stderr.trim(),
|
|
42
|
-
code,
|
|
43
|
-
cmd,
|
|
44
|
-
cmdArgs,
|
|
45
|
-
});
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
resolve({
|
|
50
|
-
ok: false,
|
|
51
|
-
stdout: stdout.trim(),
|
|
52
|
-
stderr: stderr.trim(),
|
|
53
|
-
code,
|
|
54
|
-
cmd,
|
|
55
|
-
cmdArgs,
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
const printViaComPort = async (data, options = {}) => {
|
|
61
|
-
const portPath = options.portPath || process.env.PRINTER_COM_PORT
|
|
62
|
-
const baudRate = options.baudRate || 9600;
|
|
63
|
-
|
|
64
|
-
// Dynamically import `serialport` only when COM transport is requested.
|
|
65
|
-
let
|
|
66
|
-
try {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
"--
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
//
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
return printViaComPort(data, options);
|
|
200
|
-
};
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
const getEscPosPayload = (data) =>
|
|
7
|
+
Buffer.concat([
|
|
8
|
+
Buffer.from([0x1b, 0x40, 0x0a]),
|
|
9
|
+
Buffer.from(`${data}\n\n\n\n\n`, "utf-8"),
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
const bleScriptPath = path.join(scriptDir, "ble_print.py");
|
|
14
|
+
|
|
15
|
+
const runPythonProcess = ({ cmd, cmdArgs, scriptArgs }) =>
|
|
16
|
+
new Promise((resolve, reject) => {
|
|
17
|
+
const child = spawn(cmd, [...cmdArgs, ...scriptArgs], {
|
|
18
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
let stdout = "";
|
|
22
|
+
let stderr = "";
|
|
23
|
+
|
|
24
|
+
child.stdout.on("data", (chunk) => {
|
|
25
|
+
stdout += chunk.toString();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
child.stderr.on("data", (chunk) => {
|
|
29
|
+
stderr += chunk.toString();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
child.on("error", (err) => {
|
|
33
|
+
reject(new Error(`Launch error for ${cmd}: ${err.message}`));
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
child.on("close", (code) => {
|
|
37
|
+
if (code === 0) {
|
|
38
|
+
resolve({
|
|
39
|
+
ok: true,
|
|
40
|
+
stdout: stdout.trim(),
|
|
41
|
+
stderr: stderr.trim(),
|
|
42
|
+
code,
|
|
43
|
+
cmd,
|
|
44
|
+
cmdArgs,
|
|
45
|
+
});
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
resolve({
|
|
50
|
+
ok: false,
|
|
51
|
+
stdout: stdout.trim(),
|
|
52
|
+
stderr: stderr.trim(),
|
|
53
|
+
code,
|
|
54
|
+
cmd,
|
|
55
|
+
cmdArgs,
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const printViaComPort = async (data, options = {}) => {
|
|
61
|
+
const portPath = options.portPath || process.env.PRINTER_COM_PORT;
|
|
62
|
+
const baudRate = options.baudRate || 9600;
|
|
63
|
+
|
|
64
|
+
// Dynamically import `serialport` only when COM transport is requested.
|
|
65
|
+
let SerialPortModule;
|
|
66
|
+
try {
|
|
67
|
+
SerialPortModule = await import("serialport");
|
|
68
|
+
} catch (e) {
|
|
69
|
+
return Promise.reject(
|
|
70
|
+
new Error(`serialport module not available: ${e.message}`),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const SerialPortCtor =
|
|
75
|
+
SerialPortModule.default || SerialPortModule.SerialPort || SerialPortModule;
|
|
76
|
+
|
|
77
|
+
const port = new SerialPortCtor({
|
|
78
|
+
path: portPath,
|
|
79
|
+
baudRate,
|
|
80
|
+
autoOpen: false,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
port.open((err) => {
|
|
85
|
+
if (err)
|
|
86
|
+
return reject(new Error(`Failed to open ${portPath}: ${err.message}`));
|
|
87
|
+
|
|
88
|
+
const payload = getEscPosPayload(data);
|
|
89
|
+
|
|
90
|
+
port.write(payload, (writeErr) => {
|
|
91
|
+
if (writeErr) {
|
|
92
|
+
port.close(() => reject(writeErr));
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
port.drain((drainErr) => {
|
|
97
|
+
port.close(() => {
|
|
98
|
+
if (drainErr) return reject(drainErr);
|
|
99
|
+
resolve();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const printViaBleBridge = (data, options = {}) => {
|
|
108
|
+
const payload = getEscPosPayload(data).toString("base64");
|
|
109
|
+
const args = [
|
|
110
|
+
bleScriptPath,
|
|
111
|
+
"--data-b64",
|
|
112
|
+
payload,
|
|
113
|
+
"--name",
|
|
114
|
+
options.bleName || process.env.PRINTER_BLE_NAME,
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
if (options.bleAddress || process.env.PRINTER_BLE_ADDRESS) {
|
|
118
|
+
args.push(
|
|
119
|
+
"--address",
|
|
120
|
+
options.bleAddress || process.env.PRINTER_BLE_ADDRESS,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (options.charUUID || process.env.PRINTER_BLE_CHAR_UUID) {
|
|
125
|
+
args.push(
|
|
126
|
+
"--char-uuid",
|
|
127
|
+
options.charUUID || process.env.PRINTER_BLE_CHAR_UUID,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (options.connectTimeout || process.env.PRINTER_BLE_CONNECT_TIMEOUT) {
|
|
132
|
+
args.push(
|
|
133
|
+
"--connect-timeout",
|
|
134
|
+
String(options.connectTimeout || process.env.PRINTER_BLE_CONNECT_TIMEOUT),
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (options.scanTimeout || process.env.PRINTER_BLE_SCAN_TIMEOUT) {
|
|
139
|
+
args.push(
|
|
140
|
+
"--scan-timeout",
|
|
141
|
+
String(options.scanTimeout || process.env.PRINTER_BLE_SCAN_TIMEOUT),
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (options.pair || process.env.PRINTER_BLE_PAIR === "1") {
|
|
146
|
+
args.push("--pair");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const envCmd = options.pythonCmd || process.env.PRINTER_PYTHON_CMD;
|
|
150
|
+
const candidates = envCmd
|
|
151
|
+
? [{ cmd: envCmd, cmdArgs: [] }]
|
|
152
|
+
: [
|
|
153
|
+
{ cmd: "py", cmdArgs: ["-3.11"] },
|
|
154
|
+
{ cmd: "py", cmdArgs: ["-3"] },
|
|
155
|
+
{ cmd: "python", cmdArgs: [] },
|
|
156
|
+
];
|
|
157
|
+
|
|
158
|
+
return new Promise(async (resolve, reject) => {
|
|
159
|
+
const failures = [];
|
|
160
|
+
|
|
161
|
+
for (const candidate of candidates) {
|
|
162
|
+
// Try multiple launchers because Windows py default can point to a missing runtime.
|
|
163
|
+
const result = await runPythonProcess({
|
|
164
|
+
cmd: candidate.cmd,
|
|
165
|
+
cmdArgs: candidate.cmdArgs,
|
|
166
|
+
scriptArgs: args,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
if (result.ok) {
|
|
170
|
+
resolve(result.stdout);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
failures.push(
|
|
175
|
+
`${result.cmd} ${result.cmdArgs.join(" ")} -> code ${result.code}: ${result.stderr || result.stdout || "no output"}`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
reject(
|
|
180
|
+
new Error(
|
|
181
|
+
`BLE bridge failed for all Python launchers. ${failures.join(" | ")}`,
|
|
182
|
+
),
|
|
183
|
+
);
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// Print to a paired Bluetooth printer exposed as a Windows COM (RFCOMM) port.
|
|
188
|
+
// Preferable on Windows: pair the PSF588 printer in OS Bluetooth settings
|
|
189
|
+
// and note the outgoing COM port (e.g. COM5). Then call `printToPSF588(data, { portPath: 'COM5' })`.
|
|
190
|
+
export const printData = (data, options = {}) => {
|
|
191
|
+
const transport = options.transport || process.env.PRINTER_TRANSPORT;
|
|
192
|
+
|
|
193
|
+
if (transport === "ble") {
|
|
194
|
+
return printViaBleBridge(data, options);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return printViaComPort(data, options);
|
|
198
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-thermal-printer-js",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "ESC/POS printer helper for PSF588 Bluetooth and COM printing.",
|
|
5
5
|
"main": "app.js",
|
|
6
6
|
"exports": {
|
|
@@ -10,8 +10,7 @@
|
|
|
10
10
|
"app.js",
|
|
11
11
|
"ble_print.py",
|
|
12
12
|
"ble_scan.py",
|
|
13
|
-
"README.md"
|
|
14
|
-
".env.example"
|
|
13
|
+
"README.md"
|
|
15
14
|
],
|
|
16
15
|
"scripts": {
|
|
17
16
|
"test": "node test-api.js",
|
|
@@ -35,4 +34,4 @@
|
|
|
35
34
|
"dotenv": "^16.6.1",
|
|
36
35
|
"serialport": "^13.0.0"
|
|
37
36
|
}
|
|
38
|
-
}
|
|
37
|
+
}
|
package/.env.example
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# Default: COM5 for Windows serial port, or use BLE bridge
|
|
2
|
-
PRINTER_TRANSPORT=com
|
|
3
|
-
|
|
4
|
-
# COM mode settings
|
|
5
|
-
PRINTER_COM_PORT=COM5
|
|
6
|
-
|
|
7
|
-
# BLE mode settings (if using BLE bridge)
|
|
8
|
-
PRINTER_BLE_NAME=PSF588
|
|
9
|
-
# PRINTER_BLE_ADDRESS=AA:BB:CC:DD:EE:FF
|
|
10
|
-
PRINTER_BLE_CHAR_UUID=49535343-8841-43f4-a8d4-ecbe34729bb3
|
|
11
|
-
PRINTER_BLE_CONNECT_TIMEOUT=15
|
|
12
|
-
PRINTER_BLE_SCAN_TIMEOUT=10
|
|
13
|
-
PRINTER_BLE_PAIR=0
|
|
14
|
-
|
|
15
|
-
# Python command for BLE bridge (only needed for BLE mode)
|
|
16
|
-
# PRINTER_PYTHON_CMD=python
|