node-thermal-printer-js 1.0.8 → 1.2.0
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 +275 -24
- package/app.js +546 -6
- package/ble_server.py +345 -0
- package/package.json +7 -1
- package/scripts/install-deps.js +135 -0
package/README.md
CHANGED
|
@@ -2,52 +2,303 @@
|
|
|
2
2
|
|
|
3
3
|
Public npm package for sending ESC/POS print jobs to a PSF588 printer over BLE or classic Bluetooth COM port.
|
|
4
4
|
|
|
5
|
-
## Requirements (
|
|
5
|
+
## ⚠️ Installation Requirements (READ FIRST)
|
|
6
|
+
|
|
7
|
+
### Required Software
|
|
8
|
+
- **Node.js**: 16.0.0 or higher
|
|
9
|
+
- **Python**: 3.9 or higher (downloads: https://www.python.org/downloads/)
|
|
10
|
+
- **pip**: Python package manager (usually included with Python)
|
|
11
|
+
|
|
12
|
+
### Required Python Library
|
|
13
|
+
- **bleak**: BLE client library for Python
|
|
14
|
+
- Auto-installed during `npm install` (see postinstall)
|
|
15
|
+
- Or manually: `pip install bleak`
|
|
16
|
+
|
|
17
|
+
### Platform-Specific Requirements
|
|
18
|
+
|
|
19
|
+
#### Windows
|
|
20
|
+
- Python 3.9+ with pip
|
|
21
|
+
- Bluetooth drivers (usually included with Windows 10+)
|
|
22
|
+
- No special permissions needed
|
|
23
|
+
|
|
24
|
+
#### macOS
|
|
25
|
+
- Python 3.9+ with pip
|
|
26
|
+
- Ensure Bluetooth is enabled in System Preferences
|
|
27
|
+
- May require granting Terminal/Node.js permission to use Bluetooth
|
|
28
|
+
|
|
29
|
+
#### Linux
|
|
30
|
+
- Python 3.9+ with pip
|
|
31
|
+
- **Required group permissions**:
|
|
32
|
+
```bash
|
|
33
|
+
sudo usermod -a -G dialout,plugdev $USER
|
|
34
|
+
```
|
|
35
|
+
Then log out and back in for changes to take effect.
|
|
36
|
+
Without this, BLE operations will fail with permission errors.
|
|
37
|
+
|
|
38
|
+
## Quick Start
|
|
39
|
+
|
|
40
|
+
### 1. Install Package
|
|
41
|
+
```bash
|
|
42
|
+
npm install node-thermal-printer-js
|
|
43
|
+
```
|
|
6
44
|
|
|
7
|
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
45
|
+
The postinstall script will:
|
|
46
|
+
- ✅ Check for Python 3.9+
|
|
47
|
+
- ✅ Verify/install bleak library
|
|
48
|
+
- ✅ Display platform-specific setup instructions
|
|
10
49
|
|
|
11
|
-
|
|
50
|
+
### 2. Create `.env` Configuration (Optional)
|
|
51
|
+
```env
|
|
52
|
+
# Transport: "ble-server" (recommended), "ble", or "com"
|
|
53
|
+
PRINTER_TRANSPORT=ble-server
|
|
12
54
|
|
|
13
|
-
|
|
55
|
+
# BLE Configuration
|
|
56
|
+
PRINTER_BLE_NAME=PSF588
|
|
57
|
+
PRINTER_BLE_ADDRESS=C8:47:8C:1F:72:34
|
|
58
|
+
PRINTER_BLE_CHAR_UUID=49535343-8841-43f4-a8d4-ecbe34729bb3
|
|
14
59
|
|
|
15
|
-
|
|
60
|
+
# Server Configuration
|
|
61
|
+
PRINTER_BLE_SERVER_HOST=127.0.0.1
|
|
62
|
+
PRINTER_BLE_SERVER_PORT=5555
|
|
63
|
+
PRINTER_BLE_REQUEST_TIMEOUT_MS=40000
|
|
16
64
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
```
|
|
65
|
+
# COM Port (for Windows COM transport)
|
|
66
|
+
PRINTER_COM_PORT=COM3
|
|
20
67
|
|
|
21
|
-
|
|
68
|
+
# Python Command (if auto-detection fails)
|
|
69
|
+
PRINTER_PYTHON_CMD=python3
|
|
70
|
+
```
|
|
22
71
|
|
|
72
|
+
### 3. Use in Code
|
|
23
73
|
```js
|
|
24
|
-
import { printData } from "node-thermal-printer-js";
|
|
74
|
+
import { printData, startPrinterServer, stopPrinterServer } from "node-thermal-printer-js";
|
|
75
|
+
|
|
76
|
+
// Option 1: Fast persistent server (recommended)
|
|
77
|
+
await startPrinterServer({ bleName: "PSF588" });
|
|
78
|
+
await printData("Hello World");
|
|
79
|
+
await printData("Second print (faster)");
|
|
80
|
+
await stopPrinterServer();
|
|
25
81
|
|
|
26
|
-
|
|
27
|
-
|
|
82
|
+
// Option 2: Simple one-off print
|
|
83
|
+
await printData("Hello World", {
|
|
84
|
+
transport: "ble",
|
|
28
85
|
bleName: "PSF588",
|
|
29
|
-
connectTimeout: 15,
|
|
30
86
|
scanTimeout: 10,
|
|
31
|
-
|
|
32
|
-
|
|
87
|
+
connectTimeout: 15
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Option 3: COM port (Windows)
|
|
91
|
+
await printData("Hello World", {
|
|
92
|
+
transport: "com",
|
|
93
|
+
portPath: "COM3"
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## API Reference
|
|
98
|
+
|
|
99
|
+
### `printData(data, options?)`
|
|
100
|
+
Print ESC/POS data to thermal printer.
|
|
101
|
+
|
|
102
|
+
**Parameters:**
|
|
103
|
+
- `data` (string): ESC/POS data to print
|
|
104
|
+
- `options` (object, optional):
|
|
105
|
+
- `transport` (string): "ble-server", "ble", or "com" - default: "ble-server"
|
|
106
|
+
- `bleName` (string): Printer name for BLE discovery
|
|
107
|
+
- `bleAddress` (string): MAC address (e.g., "C8:47:8C:1F:72:34")
|
|
108
|
+
- `charUUID` (string): BLE characteristic UUID for write operations
|
|
109
|
+
- `portPath` (string): COM port path (e.g., "COM3") for COM transport
|
|
110
|
+
- `scanTimeout` (number): BLE scan timeout in seconds (default: 10)
|
|
111
|
+
- `connectTimeout` (number): BLE connection timeout in seconds (default: 15)
|
|
112
|
+
- `requestTimeoutMs` (number): Server request timeout in ms (default: 40000)
|
|
113
|
+
|
|
114
|
+
**Returns:** Promise<{ok: boolean, bytes_sent: number}>
|
|
115
|
+
|
|
116
|
+
**Example:**
|
|
117
|
+
```js
|
|
118
|
+
try {
|
|
119
|
+
const result = await printData("Test print", {
|
|
120
|
+
bleName: "PSF588",
|
|
121
|
+
scanTimeout: 15
|
|
122
|
+
});
|
|
123
|
+
console.log(`✓ Sent ${result.bytes_sent} bytes`);
|
|
124
|
+
} catch (err) {
|
|
125
|
+
console.error("Print failed:", err.message);
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### `startPrinterServer(options?)`
|
|
130
|
+
Start persistent BLE server for faster multi-print performance. The server stays running to eliminate connection overhead.
|
|
131
|
+
|
|
132
|
+
**Parameters:** Same as `printData` options
|
|
133
|
+
|
|
134
|
+
**Returns:** Promise<void>
|
|
135
|
+
|
|
136
|
+
**Example:**
|
|
137
|
+
```js
|
|
138
|
+
await startPrinterServer({
|
|
139
|
+
bleName: "PSF588",
|
|
140
|
+
connectTimeout: 20
|
|
33
141
|
});
|
|
142
|
+
console.log("Server ready for fast printing");
|
|
34
143
|
```
|
|
35
144
|
|
|
36
|
-
|
|
145
|
+
### `stopPrinterServer()`
|
|
146
|
+
Stop the running BLE server and clean up resources.
|
|
37
147
|
|
|
38
|
-
|
|
148
|
+
**Returns:** Promise<void>
|
|
39
149
|
|
|
40
|
-
|
|
41
|
-
|
|
150
|
+
### `getPrinterServerStatus()`
|
|
151
|
+
Get status of running BLE server.
|
|
42
152
|
|
|
43
|
-
|
|
153
|
+
**Returns:**
|
|
154
|
+
```js
|
|
155
|
+
{
|
|
156
|
+
running: boolean,
|
|
157
|
+
host: string,
|
|
158
|
+
port: number,
|
|
159
|
+
processInfo: ChildProcess | null
|
|
160
|
+
}
|
|
161
|
+
```
|
|
44
162
|
|
|
163
|
+
## Troubleshooting
|
|
164
|
+
|
|
165
|
+
### "Python 3.9+ not found"
|
|
166
|
+
**Solution:**
|
|
167
|
+
1. Install Python from https://www.python.org/downloads/
|
|
168
|
+
2. Ensure Python is in system PATH:
|
|
169
|
+
- Windows: Check "Add Python to PATH" during installation
|
|
170
|
+
- Mac/Linux: Verify `python3` works in terminal
|
|
171
|
+
3. Set environment variable:
|
|
172
|
+
```bash
|
|
173
|
+
export PRINTER_PYTHON_CMD=python3 # or python, py.exe, etc.
|
|
174
|
+
```
|
|
175
|
+
4. Run: `npm install` again
|
|
176
|
+
|
|
177
|
+
### "bleak module not found"
|
|
178
|
+
**Solution:**
|
|
45
179
|
```bash
|
|
180
|
+
pip install bleak
|
|
181
|
+
# or with Python 3 explicitly:
|
|
182
|
+
python3 -m pip install bleak
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### "Address already in use" (Port 5555)
|
|
186
|
+
**Solution:**
|
|
187
|
+
This happens if a previous BLE server crashed. The package auto-detects and cleans up stale processes, but manual cleanup:
|
|
188
|
+
```bash
|
|
189
|
+
# Windows
|
|
190
|
+
netstat -ano | findstr :5555
|
|
191
|
+
taskkill /PID <PID> /F
|
|
192
|
+
|
|
193
|
+
# Mac/Linux
|
|
194
|
+
lsof -ti:5555 | xargs kill -9
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### "Device not found" or connection timeouts
|
|
198
|
+
**Solutions:**
|
|
199
|
+
1. Ensure printer is powered on and in BLE discoverable mode
|
|
200
|
+
2. Increase timeouts:
|
|
201
|
+
```js
|
|
202
|
+
await printData(data, { scanTimeout: 20, connectTimeout: 25 });
|
|
203
|
+
```
|
|
204
|
+
3. Try specifying MAC address (faster than name discovery):
|
|
205
|
+
```js
|
|
206
|
+
await printData(data, { bleAddress: "C8:47:8C:1F:72:34" });
|
|
207
|
+
```
|
|
208
|
+
4. On Linux, verify group permissions: `id $USER` should show `dialout` and `plugdev` groups
|
|
209
|
+
|
|
210
|
+
### "Permission denied" (Linux)
|
|
211
|
+
**Solution:**
|
|
212
|
+
```bash
|
|
213
|
+
# Add user to required groups
|
|
214
|
+
sudo usermod -a -G dialout,plugdev $USER
|
|
215
|
+
|
|
216
|
+
# Apply immediately (or log out/in)
|
|
217
|
+
su - $USER
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Print job times out
|
|
221
|
+
**Solutions:**
|
|
222
|
+
1. Increase timeout:
|
|
223
|
+
```js
|
|
224
|
+
await printData(data, { requestTimeoutMs: 60000 });
|
|
225
|
+
```
|
|
226
|
+
2. Check printer is still connected and responsive
|
|
227
|
+
3. Try legacy BLE transport:
|
|
228
|
+
```js
|
|
229
|
+
await printData(data, { transport: "ble" }); // No persistent server
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Performance
|
|
233
|
+
|
|
234
|
+
### Transport Modes Comparison
|
|
235
|
+
|
|
236
|
+
| Mode | Speed | Resource Use | Recommended For |
|
|
237
|
+
|------|-------|--------------|-----------------|
|
|
238
|
+
| `ble-server` | **100-200ms** ✓ | Low (persistent) | Multiple prints, APIs |
|
|
239
|
+
| `ble` | 1-2s | Medium (spawn/connect each time) | Single prints, testing |
|
|
240
|
+
| `com` | Variable | Low | Windows paired COM port |
|
|
241
|
+
|
|
242
|
+
### Why `ble-server` is Fast
|
|
243
|
+
- Reuses single BLE connection for multiple prints
|
|
244
|
+
- Eliminates per-print connection overhead (1s)
|
|
245
|
+
- Persistent Python daemon handles I/O
|
|
246
|
+
|
|
247
|
+
## Development
|
|
248
|
+
|
|
249
|
+
### Local Development
|
|
250
|
+
```bash
|
|
251
|
+
# Clone and setup
|
|
252
|
+
git clone <repo>
|
|
253
|
+
cd Printer
|
|
46
254
|
npm install
|
|
47
|
-
|
|
255
|
+
|
|
256
|
+
# Run tests
|
|
257
|
+
npm test
|
|
258
|
+
|
|
259
|
+
# Watch mode
|
|
260
|
+
npm run dev
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Requirements for development
|
|
264
|
+
```bash
|
|
265
|
+
# Install Python dev dependencies
|
|
266
|
+
pip install -r requirements.txt
|
|
48
267
|
```
|
|
49
268
|
|
|
50
|
-
|
|
269
|
+
## Architecture
|
|
270
|
+
|
|
271
|
+
### Components
|
|
272
|
+
1. **app.js** (Node.js): Express-compatible export module, socket IPC client
|
|
273
|
+
2. **ble_server.py** (Python): Long-running BLE daemon, accepts print requests over TCP
|
|
274
|
+
3. **ble_print.py** (Python): Fallback legacy BLE bridge (process per print)
|
|
275
|
+
4. **scripts/install-deps.js** (Node.js): Postinstall hook for dependency verification
|
|
276
|
+
|
|
277
|
+
### Data Flow
|
|
278
|
+
```
|
|
279
|
+
printData(data)
|
|
280
|
+
↓
|
|
281
|
+
[ble-server mode]
|
|
282
|
+
↓
|
|
283
|
+
sendToBleServer() → TCP socket to ble_server.py
|
|
284
|
+
↓
|
|
285
|
+
ble_server.py connects to PSF588 printer (reused)
|
|
286
|
+
↓
|
|
287
|
+
Writes ESC/POS data via BLE characteristic write
|
|
288
|
+
↓
|
|
289
|
+
Response sent back over socket
|
|
290
|
+
|
|
291
|
+
[ble mode - fallback]
|
|
292
|
+
↓
|
|
293
|
+
Spawn new ble_print.py process
|
|
294
|
+
↓
|
|
295
|
+
Process connects to printer, prints, exits (slower)
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
## License
|
|
299
|
+
|
|
300
|
+
ISC
|
|
301
|
+
|
|
51
302
|
|
|
52
303
|
If you plan to use `transport: "ble"`, you need a Python 3.11+ environment with the `bleak` library (the repo includes `ble_print.py` and `ble_scan.py`). Follow the steps for your platform.
|
|
53
304
|
|