tauri-plugin-thermal-printer 1.0.2 → 1.2.2
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 +406 -59
- package/dist-js/index.cjs +263 -3
- package/dist-js/index.d.ts +222 -18
- package/dist-js/index.js +234 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -7,11 +7,9 @@ This plugin provides thermal printer functionality for Tauri applications, allow
|
|
|
7
7
|
| Linux | ✅ |
|
|
8
8
|
| macOS | ✅ |
|
|
9
9
|
| Windows | ✅ |
|
|
10
|
-
| Android |
|
|
10
|
+
| Android | ? |
|
|
11
11
|
| iOS | ❌ |
|
|
12
12
|
|
|
13
|
-
For mobile applications, this plugin is currently working on this...
|
|
14
|
-
|
|
15
13
|
## Table of Contents
|
|
16
14
|
|
|
17
15
|
- [How it Works](#how-it-works)
|
|
@@ -24,6 +22,8 @@ For mobile applications, this plugin is currently working on this...
|
|
|
24
22
|
- [List Printers](#list-printers)
|
|
25
23
|
- [Test Printer](#test-printer)
|
|
26
24
|
- [Print Document](#print-document)
|
|
25
|
+
- [Paper Size Helpers (TypeScript)](#paper-size-helpers-typescript)
|
|
26
|
+
- [Error Handling](#error-handling)
|
|
27
27
|
- [Section Types](#section-types)
|
|
28
28
|
- [Title](#title)
|
|
29
29
|
- [Subtitle](#subtitle)
|
|
@@ -41,6 +41,11 @@ For mobile applications, this plugin is currently working on this...
|
|
|
41
41
|
- [Logo](#logo)
|
|
42
42
|
- [Line](#line)
|
|
43
43
|
- [GlobalStyles](#globalstyles)
|
|
44
|
+
- [TypeScript Constants & Helpers](#typescript-constants--helpers)
|
|
45
|
+
- [CodePage](#codepage)
|
|
46
|
+
- [Style constants](#style-constants)
|
|
47
|
+
- [Section builder helpers](#section-builder-helpers)
|
|
48
|
+
- [Helper example (all builders)](#helper-example-all-builders)
|
|
44
49
|
- [Examples](#examples)
|
|
45
50
|
|
|
46
51
|
## How it Works
|
|
@@ -50,13 +55,17 @@ This plugin acts as a **translator** between a user-friendly JavaScript/TypeScri
|
|
|
50
55
|
### Architecture
|
|
51
56
|
|
|
52
57
|
```
|
|
53
|
-
Frontend (JavaScript/TypeScript)
|
|
58
|
+
Frontend (JavaScript/TypeScript)
|
|
54
59
|
↓ (IPC Commands)
|
|
55
|
-
Tauri Core (Rust)
|
|
60
|
+
Tauri Core (Rust) ←— ESC/POS generation (shared across all platforms)
|
|
56
61
|
↓ (Platform-specific implementations)
|
|
57
|
-
Operating System (Linux/macOS/Windows)
|
|
58
|
-
↓ (Raw binary data)
|
|
59
|
-
Thermal Printer (ESC/POS protocol)
|
|
62
|
+
├── Desktop: Operating System (Linux/macOS/Windows)
|
|
63
|
+
│ ↓ (Raw binary data)
|
|
64
|
+
│ Thermal Printer (ESC/POS protocol)
|
|
65
|
+
│
|
|
66
|
+
└── Android: Kotlin Plugin
|
|
67
|
+
↓ (Bluetooth SPP / RFCOMM)
|
|
68
|
+
Thermal Printer (ESC/POS protocol)
|
|
60
69
|
```
|
|
61
70
|
|
|
62
71
|
### Core Components
|
|
@@ -78,14 +87,14 @@ Converts data structures into ESC/POS binary commands:
|
|
|
78
87
|
pub fn generate_document(&mut self, print_job: &PrintJobRequest) -> Result<Vec<u8>, String>
|
|
79
88
|
```
|
|
80
89
|
|
|
81
|
-
#### 4. **OS Integration** (`src/desktop_printers/`)
|
|
90
|
+
#### 4. **OS Integration** (`src/desktop_printers/` and `android/`)
|
|
82
91
|
- **Linux/macOS**: Uses CUPS system (`lpstat`, `lp` commands)
|
|
83
92
|
- **Windows**: Uses WinAPI (Windows API) to directly access system printers via functions such as EnumPrintersW for listing printers, OpenPrinterW for opening printer handles, and WritePrinter for sending raw data
|
|
84
|
-
- **Android**:
|
|
93
|
+
- **Android**: Kotlin plugin with Bluetooth SPP and USB printer discovery and printing
|
|
85
94
|
|
|
86
95
|
### Workflow
|
|
87
96
|
|
|
88
|
-
#### Printing a Document:
|
|
97
|
+
#### Printing a Document (Desktop):
|
|
89
98
|
|
|
90
99
|
1. **Frontend** sends `PrintJobRequest` with sections and configuration
|
|
91
100
|
2. **Tauri** receives the command and processes it in Rust
|
|
@@ -93,6 +102,14 @@ pub fn generate_document(&mut self, print_job: &PrintJobRequest) -> Result<Vec<u
|
|
|
93
102
|
4. **Operating System** sends binary data to the printer
|
|
94
103
|
5. **Thermal Printer** interprets ESC/POS commands and prints
|
|
95
104
|
|
|
105
|
+
#### Printing a Document (Android):
|
|
106
|
+
|
|
107
|
+
1. **Frontend** sends `PrintJobRequest` with sections and configuration
|
|
108
|
+
2. **Rust** generates ESC/POS binary data using the same `ProcessPrint` pipeline
|
|
109
|
+
3. **Kotlin plugin** receives the binary data and the printer MAC address
|
|
110
|
+
4. **Bluetooth SPP** connection is established to the printer
|
|
111
|
+
5. **Thermal Printer** interprets ESC/POS commands and prints
|
|
112
|
+
|
|
96
113
|
#### Print Structure Example:
|
|
97
114
|
```json
|
|
98
115
|
{
|
|
@@ -133,15 +150,18 @@ The plugin translates all sections into **ESC/POS** (Escape Sequence for Point o
|
|
|
133
150
|
- ✅ **Linux**: Fully functional (CUPS)
|
|
134
151
|
- ✅ **macOS**: Fully functional (CUPS)
|
|
135
152
|
- ✅ **Windows**: Fully functional (WinAPI)
|
|
136
|
-
-
|
|
153
|
+
- ✅ **Android**: Bluetooth and USB printer discovery and printing
|
|
137
154
|
- ❌ **iOS**: Not implemented
|
|
138
155
|
|
|
139
156
|
### Supported Connections
|
|
140
157
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
158
|
+
| Connection | Linux | macOS | Windows | Android |
|
|
159
|
+
| ---------- | ----- | ----- | ------- | ------- |
|
|
160
|
+
| USB | ✅ | ✅ | ✅ | ✅ (discovery only) |
|
|
161
|
+
| Network | ✅ | ✅ | ✅ | ❌ |
|
|
162
|
+
| Bluetooth | ❌ | ❌ | ❌ | ✅ |
|
|
163
|
+
|
|
164
|
+
> **Android note**: The `printer` field in `PrintJobRequest` must be the Bluetooth MAC address of the printer (e.g. `"AA:BB:CC:DD:EE:FF"`). The printer must be previously paired in the Android Bluetooth settings. Bluetooth permissions are requested automatically at runtime.
|
|
145
165
|
|
|
146
166
|
## Installation
|
|
147
167
|
|
|
@@ -201,7 +221,7 @@ on package.json
|
|
|
201
221
|
|
|
202
222
|
```json
|
|
203
223
|
"dependencies": {
|
|
204
|
-
"tauri-plugin-thermal-printer
|
|
224
|
+
"tauri-plugin-thermal-printer": "file:../tauri-plugin-thermal-printer"
|
|
205
225
|
}
|
|
206
226
|
```
|
|
207
227
|
|
|
@@ -213,9 +233,13 @@ Get all printers available in the system. It just lists the configured printers.
|
|
|
213
233
|
|
|
214
234
|
#### Request:
|
|
215
235
|
```typescript
|
|
216
|
-
import { list_thermal_printers } from "tauri-plugin-thermal-printer
|
|
236
|
+
import { list_thermal_printers } from "tauri-plugin-thermal-printer";
|
|
217
237
|
|
|
218
|
-
|
|
238
|
+
try {
|
|
239
|
+
const response = await list_thermal_printers();
|
|
240
|
+
} catch (error) {
|
|
241
|
+
console.log("List printers failed: " + error)
|
|
242
|
+
}
|
|
219
243
|
```
|
|
220
244
|
|
|
221
245
|
#### Response
|
|
@@ -250,9 +274,9 @@ Send a print test to a specific printer to verify functionality.
|
|
|
250
274
|
|
|
251
275
|
#### Request:
|
|
252
276
|
```typescript
|
|
253
|
-
import { test_thermal_printer, type TestPrintRequest } from "tauri-plugin-thermal-printer
|
|
277
|
+
import { test_thermal_printer, type TestPrintRequest } from "tauri-plugin-thermal-printer";
|
|
254
278
|
|
|
255
|
-
|
|
279
|
+
try { await test_thermal_printer({
|
|
256
280
|
"printer_info": {
|
|
257
281
|
"printer": "TM-T20II",
|
|
258
282
|
"paper_size": "Mm80",
|
|
@@ -280,7 +304,7 @@ const response = await test_thermal_printer({
|
|
|
280
304
|
"test_all_fonts": false,
|
|
281
305
|
"test_invert": false,
|
|
282
306
|
"test_rotate": false
|
|
283
|
-
} as TestPrintRequest)
|
|
307
|
+
} as TestPrintRequest) } catch (error) { console.error("Test print failed:", error); }
|
|
284
308
|
```
|
|
285
309
|
|
|
286
310
|
#### Request parameters (TestPrintRequest):
|
|
@@ -307,9 +331,7 @@ const response = await test_thermal_printer({
|
|
|
307
331
|
| `test_rotate` | boolean | ❌ No | Text rotation test (default: `false`) |
|
|
308
332
|
|
|
309
333
|
#### Response:
|
|
310
|
-
Returns `
|
|
311
|
-
- `true`: Test completed successfully
|
|
312
|
-
- `false`: Test failed
|
|
334
|
+
Returns `Promise<void>`. Resolves when the test print completes successfully. **Throws a `string`** with the error message if it fails. Use `try/catch` to handle errors — see [Error Handling](#error-handling).
|
|
313
335
|
|
|
314
336
|
---
|
|
315
337
|
|
|
@@ -319,9 +341,9 @@ Print a personalized document with the specified sections.
|
|
|
319
341
|
|
|
320
342
|
#### Request:
|
|
321
343
|
```typescript
|
|
322
|
-
import { print_thermal_printer, type PrintJobRequest } from "tauri-plugin-thermal-printer
|
|
344
|
+
import { print_thermal_printer, type PrintJobRequest } from "tauri-plugin-thermal-printer";
|
|
323
345
|
|
|
324
|
-
|
|
346
|
+
try { await print_thermal_printer({
|
|
325
347
|
"printer": "TM-T20II",
|
|
326
348
|
"paper_size": "Mm80",
|
|
327
349
|
"options": {
|
|
@@ -338,34 +360,116 @@ const response = await print_thermal_printer({
|
|
|
338
360
|
{"Beep": {"times": 1, "duration": 100}},
|
|
339
361
|
{"Drawer": {"pin": 2, "pulse_time": 100}},
|
|
340
362
|
{"Qr": {"data": "https://example.com", "size": 5, "error_correction": "M", "model": 2}},
|
|
341
|
-
{"Barcode": {"data": "
|
|
342
|
-
{"Table": {"columns": 3, "column_widths": [
|
|
363
|
+
{"Barcode": {"data": "123456789012", "barcode_type": "CODE128", "width": 2, "height": 100, "text_position": "below"}},
|
|
364
|
+
{"Table": {"columns": 3, "column_widths": [16, 16, 16], "header": [{"text": "Col1"}, {"text": "Col2"}, {"text": "Col3"}], "body": [[{"text": "Data1"}, {"text": "Data2"}, {"text": "Data3"}]], "truncate": false}},
|
|
343
365
|
{"DataMatrix": {"data": "DataMatrix data", "size": 5}},
|
|
344
366
|
{"Pdf417": {"data": "PDF417 data", "columns": 2, "rows": 5, "width": 3, "height": 5, "error_correction": 2}},
|
|
345
|
-
{"Image": {"data": "{
|
|
367
|
+
{"Image": {"data": "{base64 image data}", "max_width": 384, "align": "center", "dithering": true, "size": "normal"}},
|
|
346
368
|
{"Logo": {"key_code": 1, "mode": "normal"}},
|
|
347
369
|
{"Line": {"character": "="}}
|
|
348
370
|
]
|
|
349
|
-
} as PrintJobRequest)
|
|
371
|
+
} as PrintJobRequest) } catch (error) { console.error("Print failed:", error); }
|
|
350
372
|
```
|
|
351
373
|
|
|
352
374
|
#### Response:
|
|
353
|
-
Returns `
|
|
354
|
-
- `true`: Print completed successfully
|
|
355
|
-
- `false`: Print failed
|
|
375
|
+
Returns `Promise<void>`. Resolves when printing completes successfully. **Throws a `string`** with the error message if the job fails (e.g., printer not found, invalid barcode data, QR data too long). Use `try/catch` to handle errors — see [Error Handling](#error-handling).
|
|
356
376
|
|
|
357
377
|
#### Main parameters (PrintJobRequest):
|
|
358
378
|
|
|
359
379
|
| Parameter | Type | Required | Description |
|
|
360
380
|
|-----------|------|----------|-------------|
|
|
361
381
|
| `printer` | string | ✅ Yes | Printer name |
|
|
362
|
-
| `paper_size` |
|
|
382
|
+
| `paper_size` | PaperSize | ❌ No | Paper size (default: `"Mm80"`) — see [Paper Sizes](#paper-sizes) |
|
|
363
383
|
| `options` | PrinterOptions | ❌ No | Configuration options |
|
|
364
384
|
| `options.cut_paper` | boolean | ❌ No | Cut paper after printing (default: `true`) |
|
|
365
385
|
| `options.beep` | boolean | ❌ No | Beep after printing (default: `false`) |
|
|
366
386
|
| `options.open_cash_drawer` | boolean | ❌ No | Open cash drawer after printing (default: `false`) |
|
|
387
|
+
| `options.code_page` | CodePage | ❌ No | Character encoding for special characters (default: `"Default"`) — see [CodePage](#codepage) |
|
|
367
388
|
| `sections` | array | ✅ Yes | Array of sections to print (see [Section Types](#section-types)) |
|
|
368
389
|
|
|
390
|
+
#### Paper Sizes
|
|
391
|
+
|
|
392
|
+
| Value | Paper width | Chars/line | Typical use |
|
|
393
|
+
|-------|-------------|------------|-------------|
|
|
394
|
+
| `"Mm40"` | 40mm | 21 | Handheld ticket printers |
|
|
395
|
+
| `"Mm44"` | 44mm | 24 | Compact POS |
|
|
396
|
+
| `"Mm58"` | 58mm | 32 | Small format (most common portable) |
|
|
397
|
+
| `"Mm72"` | 72mm | 42 | Mid-range printers |
|
|
398
|
+
| `"Mm80"` | 80mm | 48 | Standard large format (default) |
|
|
399
|
+
| `"Mm104"` | 104mm | 62 | Wide format |
|
|
400
|
+
|
|
401
|
+
#### Paper Size Helpers (TypeScript)
|
|
402
|
+
|
|
403
|
+
The TypeScript package exports constants and helper functions with the same values used by the Rust backend.
|
|
404
|
+
|
|
405
|
+
```typescript
|
|
406
|
+
import {
|
|
407
|
+
PAPER_SIZE_CHARS_PER_LINE,
|
|
408
|
+
PAPER_SIZE_PIXELS_WIDTH,
|
|
409
|
+
DEFAULT_PAPER_SIZE,
|
|
410
|
+
getPaperSizeCharsPerLine,
|
|
411
|
+
getPaperSizePixelsWidth,
|
|
412
|
+
type PaperSize,
|
|
413
|
+
} from "tauri-plugin-thermal-printer";
|
|
414
|
+
|
|
415
|
+
const size: PaperSize = "Mm58";
|
|
416
|
+
|
|
417
|
+
console.log(DEFAULT_PAPER_SIZE); // "Mm80"
|
|
418
|
+
console.log(PAPER_SIZE_CHARS_PER_LINE[size]); // 32
|
|
419
|
+
console.log(PAPER_SIZE_PIXELS_WIDTH[size]); // 384
|
|
420
|
+
|
|
421
|
+
// Equivalent helper functions:
|
|
422
|
+
console.log(getPaperSizeCharsPerLine(size)); // 32
|
|
423
|
+
console.log(getPaperSizePixelsWidth(size)); // 384
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
Values per paper size:
|
|
427
|
+
|
|
428
|
+
| PaperSize | Chars/line | Pixels width |
|
|
429
|
+
|-----------|------------|--------------|
|
|
430
|
+
| `"Mm40"` | 21 | 256 |
|
|
431
|
+
| `"Mm44"` | 24 | 288 |
|
|
432
|
+
| `"Mm58"` | 32 | 384 |
|
|
433
|
+
| `"Mm72"` | 42 | 512 |
|
|
434
|
+
| `"Mm80"` | 48 | 576 |
|
|
435
|
+
| `"Mm104"` | 62 | 752 |
|
|
436
|
+
|
|
437
|
+
---
|
|
438
|
+
|
|
439
|
+
### Error Handling
|
|
440
|
+
|
|
441
|
+
`print_thermal_printer` and `test_thermal_printer` now return `Promise<void>` and **throw** a descriptive `string` when something fails. Always wrap calls in `try/catch`:
|
|
442
|
+
|
|
443
|
+
```typescript
|
|
444
|
+
import { print_thermal_printer, type PrintJobRequest } from "tauri-plugin-thermal-printer";
|
|
445
|
+
|
|
446
|
+
try {
|
|
447
|
+
await print_thermal_printer(job);
|
|
448
|
+
} catch (error) {
|
|
449
|
+
// `error` is a string describing what went wrong, e.g.:
|
|
450
|
+
// "Printer not specified"
|
|
451
|
+
// "Barcode data cannot be empty"
|
|
452
|
+
// "Barcode type 'EAN13' only accepts numeric digits"
|
|
453
|
+
// "QR data length 5000 exceeds maximum 4296 for error correction level 'M'"
|
|
454
|
+
// "Table row 2 has 2 cells but 3 columns declared"
|
|
455
|
+
// "column_widths sum (45) must equal paper chars_per_line (48)"
|
|
456
|
+
// "Image data cannot be empty"
|
|
457
|
+
console.error("Print failed:", error);
|
|
458
|
+
}
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
`list_thermal_printers` also throws on failure (e.g., CUPS not available):
|
|
462
|
+
|
|
463
|
+
```typescript
|
|
464
|
+
try {
|
|
465
|
+
const printers = await list_thermal_printers();
|
|
466
|
+
} catch (error) {
|
|
467
|
+
console.error("Could not list printers:", error);
|
|
468
|
+
}
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
---
|
|
472
|
+
|
|
369
473
|
#### Section Types
|
|
370
474
|
|
|
371
475
|
Sections are defined as objects in the `sections` array. Each section is an enum variant with its data. Below are all supported section types:
|
|
@@ -503,8 +607,11 @@ Advances the paper by a specific number of lines.
|
|
|
503
607
|
}
|
|
504
608
|
```
|
|
505
609
|
|
|
506
|
-
- `feed_type` (string, required): Feed type
|
|
507
|
-
- `
|
|
610
|
+
- `feed_type` (string, required): Feed type:
|
|
611
|
+
- `"lines"` — advance N lines (`ESC d n`)
|
|
612
|
+
- `"dots"` — advance N dot rows (`ESC J n`)
|
|
613
|
+
- `"line_feed"` — send N raw LF characters
|
|
614
|
+
- `value` (number, required): Amount to advance
|
|
508
615
|
|
|
509
616
|
##### Cut
|
|
510
617
|
Cuts the paper.
|
|
@@ -518,7 +625,11 @@ Cuts the paper.
|
|
|
518
625
|
}
|
|
519
626
|
```
|
|
520
627
|
|
|
521
|
-
- `mode` (string, required): Cut mode
|
|
628
|
+
- `mode` (string, required): Cut mode:
|
|
629
|
+
- `"full"` — full cut
|
|
630
|
+
- `"partial"` — partial cut (default fallback)
|
|
631
|
+
- `"partial_alt"` — partial cut (alternate)
|
|
632
|
+
- `"partial_alt2"` — full cut (alternate)
|
|
522
633
|
- `feed` (number, required): Lines to advance before cutting
|
|
523
634
|
|
|
524
635
|
##### Beep
|
|
@@ -566,11 +677,11 @@ Prints a QR code.
|
|
|
566
677
|
}
|
|
567
678
|
```
|
|
568
679
|
|
|
569
|
-
- `data` (string, required): QR data
|
|
570
|
-
- `size` (number, required): Module size (1
|
|
571
|
-
- `error_correction` (string, required):
|
|
572
|
-
- `model` (number, required): QR model (1 or 2)
|
|
573
|
-
- `align` (string, optional):
|
|
680
|
+
- `data` (string, required): QR data. **Must not be empty.** Maximum length depends on error correction level — the backend will throw if exceeded.
|
|
681
|
+
- `size` (number, required): Module size (1–16)
|
|
682
|
+
- `error_correction` (string, required): `"L"` (7089 chars max) | `"M"` (4296, default) | `"Q"` (2953) | `"H"` (1817)
|
|
683
|
+
- `model` (number, required): QR model (`1` or `2`)
|
|
684
|
+
- `align` (string, optional): `"left"` | `"center"` | `"right"`
|
|
574
685
|
|
|
575
686
|
##### Barcode
|
|
576
687
|
Prints a barcode.
|
|
@@ -588,12 +699,12 @@ Prints a barcode.
|
|
|
588
699
|
}
|
|
589
700
|
```
|
|
590
701
|
|
|
591
|
-
- `data` (string, required): Barcode data
|
|
592
|
-
- `barcode_type` (string, required):
|
|
593
|
-
- `width` (number, required): Module width
|
|
594
|
-
- `height` (number, required): Height in dots
|
|
595
|
-
- `text_position` (string, required):
|
|
596
|
-
- `align` (string, optional):
|
|
702
|
+
- `data` (string, required): Barcode data. **Must not be empty.** Numeric-only types (`UPC-A`, `UPC-E`, `EAN13`, `EAN8`, `ITF`) only accept digit characters — the backend will throw an error otherwise.
|
|
703
|
+
- `barcode_type` (string, required): `"UPC-A"` | `"UPC-E"` | `"EAN13"` | `"EAN8"` | `"CODE39"` | `"ITF"` | `"CODABAR"` | `"CODE93"` | `"CODE128"`
|
|
704
|
+
- `width` (number, required): Module width (1–6)
|
|
705
|
+
- `height` (number, required): Height in dots (must be > 0)
|
|
706
|
+
- `text_position` (string, required): `"none"` | `"above"` | `"below"` | `"both"`
|
|
707
|
+
- `align` (string, optional): `"left"` | `"center"` | `"right"` (default: current global alignment)
|
|
597
708
|
|
|
598
709
|
##### Table
|
|
599
710
|
Prints a table.
|
|
@@ -638,10 +749,10 @@ Or simply:
|
|
|
638
749
|
```
|
|
639
750
|
|
|
640
751
|
- `columns` (number, required): Number of columns
|
|
641
|
-
- `column_widths` (array, optional): Widths of each column.
|
|
642
|
-
- `header` (array, optional): Column headers
|
|
643
|
-
- `body` (array, required): Data rows
|
|
644
|
-
- `truncate` (boolean, optional): Truncate long text (default: false)
|
|
752
|
+
- `column_widths` (array, optional): Widths of each column in characters. **When provided: length must equal `columns` and the sum must equal the paper's chars/line** (e.g., 48 for Mm80). If omitted, columns are distributed evenly.
|
|
753
|
+
- `header` (array, optional): Column headers — must have exactly `columns` elements if provided
|
|
754
|
+
- `body` (array, required): Data rows — each row must have exactly `columns` cells
|
|
755
|
+
- `truncate` (boolean, optional): Truncate long text instead of wrapping (default: `false`)
|
|
645
756
|
|
|
646
757
|
##### DataMatrix
|
|
647
758
|
Prints a DataMatrix code.
|
|
@@ -696,11 +807,11 @@ Prints an image.
|
|
|
696
807
|
}
|
|
697
808
|
```
|
|
698
809
|
|
|
699
|
-
- `data` (string, required): Base64 encoded image
|
|
700
|
-
- `max_width` (number, required): Maximum width in pixels
|
|
701
|
-
- `align` (string, required):
|
|
702
|
-
- `dithering` (boolean, required): Apply dithering
|
|
703
|
-
- `size` (string, required):
|
|
810
|
+
- `data` (string, required): Base64 encoded image. **Must not be empty.**
|
|
811
|
+
- `max_width` (number, required): Maximum width in pixels (0 or values larger than the paper width are clamped to the paper width automatically)
|
|
812
|
+
- `align` (string, required): `"left"` | `"center"` | `"right"`
|
|
813
|
+
- `dithering` (boolean, required): Apply Floyd-Steinberg dithering for better quality on monochrome printers
|
|
814
|
+
- `size` (string, required): `"normal"` | `"double_width"` | `"double_height"` | `"quadruple"`
|
|
704
815
|
|
|
705
816
|
##### Logo
|
|
706
817
|
Prints a logo stored in the printer.
|
|
@@ -762,6 +873,240 @@ Changes the current global styles that will be applied to subsequent text sectio
|
|
|
762
873
|
|
|
763
874
|
---
|
|
764
875
|
|
|
876
|
+
## TypeScript Constants & Helpers
|
|
877
|
+
|
|
878
|
+
The plugin exports typed constants and builder functions so you never have to type raw strings.
|
|
879
|
+
|
|
880
|
+
### CodePage
|
|
881
|
+
|
|
882
|
+
Set the character encoding once in `PrinterOptions.code_page` and all text sections (`Title`, `Subtitle`, `Text`, `Table`) will use it automatically.
|
|
883
|
+
|
|
884
|
+
```typescript
|
|
885
|
+
import { CODE_PAGE, type CodePage } from "tauri-plugin-thermal-printer";
|
|
886
|
+
|
|
887
|
+
// In your PrintJobRequest:
|
|
888
|
+
const options = {
|
|
889
|
+
cut_paper: true,
|
|
890
|
+
beep: false,
|
|
891
|
+
open_cash_drawer: false,
|
|
892
|
+
code_page: CODE_PAGE.SPANISH, // enables á é í ó ú ñ ü ¿ ¡
|
|
893
|
+
};
|
|
894
|
+
```
|
|
895
|
+
|
|
896
|
+
| Constant | Value | Code Page | Languages |
|
|
897
|
+
|---|---|---|---|
|
|
898
|
+
| `CODE_PAGE.DEFAULT` | `"Default"` | CP437 | ASCII only — no accented characters |
|
|
899
|
+
| `CODE_PAGE.SPANISH` | `"Spanish"` | CP850 | **Spanish**, French, Italian, German, Portuguese |
|
|
900
|
+
| `CODE_PAGE.FRENCH` | `"French"` | CP850 | Alias of Spanish |
|
|
901
|
+
| `CODE_PAGE.PORTUGUESE` | `"Portuguese"` | CP860 | **Portuguese** (ã, õ) |
|
|
902
|
+
| `CODE_PAGE.CANADIAN_FRENCH` | `"CanadianFrench"` | CP863 | Canadian French |
|
|
903
|
+
| `CODE_PAGE.NORDIC` | `"Nordic"` | CP865 | Swedish, Norwegian, Danish, Finnish (å, ø, æ) |
|
|
904
|
+
| `CODE_PAGE.WINDOWS_LATIN` | `"WindowsLatin"` | CP1252 | Wide Western European — includes € |
|
|
905
|
+
| `CODE_PAGE.RUSSIAN` | `"Russian"` | CP866 | **Russian** / Cyrillic |
|
|
906
|
+
| `CODE_PAGE.EASTERN_EUROPE` | `"EasternEurope"` | CP852 | **Polish**, Czech, Slovak, Hungarian |
|
|
907
|
+
|
|
908
|
+
> **Note**: Without a `code_page`, accented characters (á, ñ, ü, etc.) will print as `?`. Set it once in `options` and it applies to the entire document.
|
|
909
|
+
|
|
910
|
+
---
|
|
911
|
+
|
|
912
|
+
### Style constants
|
|
913
|
+
|
|
914
|
+
Instead of typing raw strings you can import typed constant objects:
|
|
915
|
+
|
|
916
|
+
```typescript
|
|
917
|
+
import {
|
|
918
|
+
TEXT_ALIGN,
|
|
919
|
+
TEXT_SIZE,
|
|
920
|
+
TEXT_FONT,
|
|
921
|
+
BARCODE_TYPE,
|
|
922
|
+
BARCODE_TEXT_POSITION,
|
|
923
|
+
QR_ERROR_CORRECTION,
|
|
924
|
+
IMAGE_MODE,
|
|
925
|
+
CUT_MODE,
|
|
926
|
+
} from "tauri-plugin-thermal-printer";
|
|
927
|
+
|
|
928
|
+
// Examples:
|
|
929
|
+
const styles = {
|
|
930
|
+
align: TEXT_ALIGN.CENTER, // "center"
|
|
931
|
+
size: TEXT_SIZE.DOUBLE, // "double"
|
|
932
|
+
font: TEXT_FONT.B, // "B"
|
|
933
|
+
bold: true,
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
const barcode = {
|
|
937
|
+
barcode_type: BARCODE_TYPE.EAN13, // "EAN13"
|
|
938
|
+
text_position: BARCODE_TEXT_POSITION.BELOW, // "below"
|
|
939
|
+
};
|
|
940
|
+
|
|
941
|
+
const qr = {
|
|
942
|
+
error_correction: QR_ERROR_CORRECTION.M, // "M"
|
|
943
|
+
};
|
|
944
|
+
```
|
|
945
|
+
|
|
946
|
+
| Export | Values |
|
|
947
|
+
|---|---|
|
|
948
|
+
| `TEXT_ALIGN` | `LEFT` `CENTER` `RIGHT` |
|
|
949
|
+
| `TEXT_SIZE` | `NORMAL` `HEIGHT` `WIDTH` `DOUBLE` |
|
|
950
|
+
| `TEXT_FONT` | `A` `B` `C` |
|
|
951
|
+
| `BARCODE_TYPE` | `UPC_A` `UPC_E` `EAN13` `EAN8` `CODE39` `ITF` `CODABAR` `CODE93` `CODE128` |
|
|
952
|
+
| `BARCODE_TEXT_POSITION` | `NONE` `ABOVE` `BELOW` `BOTH` |
|
|
953
|
+
| `QR_ERROR_CORRECTION` | `L` `M` `Q` `H` |
|
|
954
|
+
| `IMAGE_MODE` | `NORMAL` `DOUBLE_WIDTH` `DOUBLE_HEIGHT` `QUADRUPLE` |
|
|
955
|
+
| `CUT_MODE` | `FULL` `PARTIAL` |
|
|
956
|
+
|
|
957
|
+
---
|
|
958
|
+
|
|
959
|
+
### Section builder helpers
|
|
960
|
+
|
|
961
|
+
Short helper functions to build section types without enum wrapper boilerplate:
|
|
962
|
+
|
|
963
|
+
```typescript
|
|
964
|
+
import {
|
|
965
|
+
title, subtitle, text, line, feed, cut, globalStyles,
|
|
966
|
+
beep, drawer, table, qr, barcode, dataMatrix, pdf417, image, logo,
|
|
967
|
+
TEXT_ALIGN, TEXT_SIZE, BARCODE_TYPE, QR_ERROR_CORRECTION,
|
|
968
|
+
} from "tauri-plugin-thermal-printer";
|
|
969
|
+
|
|
970
|
+
const sections = [
|
|
971
|
+
title("My Business"),
|
|
972
|
+
subtitle("Receipt #001"),
|
|
973
|
+
text("Thank you for your purchase!", { align: TEXT_ALIGN.CENTER }),
|
|
974
|
+
line("="),
|
|
975
|
+
qr("https://example.com/order/123", {
|
|
976
|
+
size: 6,
|
|
977
|
+
error_correction: QR_ERROR_CORRECTION.M,
|
|
978
|
+
}),
|
|
979
|
+
barcode("123456789012", BARCODE_TYPE.EAN13),
|
|
980
|
+
beep(),
|
|
981
|
+
text("Total: $50.00", { bold: true, size: TEXT_SIZE.DOUBLE }),
|
|
982
|
+
line("-"),
|
|
983
|
+
feed(3),
|
|
984
|
+
cut(),
|
|
985
|
+
];
|
|
986
|
+
```
|
|
987
|
+
|
|
988
|
+
| Helper | Description |
|
|
989
|
+
|---|---|
|
|
990
|
+
| `title(text, styles?)` | Creates a `{ Title: ... }` section |
|
|
991
|
+
| `subtitle(text, styles?)` | Creates a `{ Subtitle: ... }` section |
|
|
992
|
+
| `text(text, styles?)` | Creates a `{ Text: ... }` section |
|
|
993
|
+
| `line(character?)` | Creates a `{ Line: ... }` section (default `"-"`) |
|
|
994
|
+
| `feed(value, type?)` | Creates a `{ Feed: ... }` section (default `"lines"`) |
|
|
995
|
+
| `cut(mode?, feedLines?)` | Creates a `{ Cut: ... }` section (default `"partial"`, 4 lines) |
|
|
996
|
+
| `globalStyles(styles)` | Creates a `{ GlobalStyles: ... }` section |
|
|
997
|
+
| `beep(times?, duration?)` | Creates a `{ Beep: ... }` section (default `1`, `3`) |
|
|
998
|
+
| `drawer(pin?, pulse_time?)` | Creates a `{ Drawer: ... }` section (default `2`, `120`) |
|
|
999
|
+
| `table(columns, body, options?)` | Creates a `{ Table: ... }` section (`truncate` default `true`) |
|
|
1000
|
+
| `qr(data, options?)` | Creates a `{ Qr: ... }` section (`size=6`, `error_correction="M"`, `model=2`) |
|
|
1001
|
+
| `barcode(data, barcode_type?, options?)` | Creates a `{ Barcode: ... }` section (`CODE128`, `width=3`, `height=80`, `text_position="below"`) |
|
|
1002
|
+
| `dataMatrix(data, size?)` | Creates a `{ DataMatrix: ... }` section (default `size=6`) |
|
|
1003
|
+
| `pdf417(data, options?)` | Creates a `{ Pdf417: ... }` section (`columns=0`, `rows=0`, `width=2`, `height=3`, `error_correction=2`) |
|
|
1004
|
+
| `image(data, options?)` | Creates a `{ Image: ... }` section (`max_width=0`, `align="center"`, `dithering=true`, `size="normal"`) |
|
|
1005
|
+
| `logo(key_code, mode?)` | Creates a `{ Logo: ... }` section (default `mode="normal"`) |
|
|
1006
|
+
|
|
1007
|
+
### Helper example (all builders)
|
|
1008
|
+
|
|
1009
|
+
```typescript
|
|
1010
|
+
import {
|
|
1011
|
+
print_thermal_printer,
|
|
1012
|
+
type PrintJobRequest,
|
|
1013
|
+
title,
|
|
1014
|
+
subtitle,
|
|
1015
|
+
text,
|
|
1016
|
+
line,
|
|
1017
|
+
feed,
|
|
1018
|
+
cut,
|
|
1019
|
+
globalStyles,
|
|
1020
|
+
beep,
|
|
1021
|
+
drawer,
|
|
1022
|
+
table,
|
|
1023
|
+
qr,
|
|
1024
|
+
barcode,
|
|
1025
|
+
dataMatrix,
|
|
1026
|
+
pdf417,
|
|
1027
|
+
image,
|
|
1028
|
+
logo,
|
|
1029
|
+
TEXT_ALIGN,
|
|
1030
|
+
TEXT_SIZE,
|
|
1031
|
+
BARCODE_TYPE,
|
|
1032
|
+
BARCODE_TEXT_POSITION,
|
|
1033
|
+
QR_ERROR_CORRECTION,
|
|
1034
|
+
IMAGE_MODE,
|
|
1035
|
+
CODE_PAGE,
|
|
1036
|
+
} from "tauri-plugin-thermal-printer";
|
|
1037
|
+
|
|
1038
|
+
const job: PrintJobRequest = {
|
|
1039
|
+
printer: "TM-T20II",
|
|
1040
|
+
paper_size: "Mm80",
|
|
1041
|
+
options: {
|
|
1042
|
+
cut_paper: true,
|
|
1043
|
+
beep: false,
|
|
1044
|
+
open_cash_drawer: false,
|
|
1045
|
+
code_page: CODE_PAGE.WINDOWS_LATIN,
|
|
1046
|
+
},
|
|
1047
|
+
sections: [
|
|
1048
|
+
globalStyles({ align: TEXT_ALIGN.LEFT }),
|
|
1049
|
+
title("DEMO STORE"),
|
|
1050
|
+
subtitle("Receipt #A-1001"),
|
|
1051
|
+
text("Date: 2026-03-30 14:22"),
|
|
1052
|
+
line("="),
|
|
1053
|
+
table(
|
|
1054
|
+
3,
|
|
1055
|
+
[
|
|
1056
|
+
[text("1"), text("Americano"), text("$2.50", { align: TEXT_ALIGN.RIGHT })],
|
|
1057
|
+
[text("2"), text("Croissant"), text("$7.00", { align: TEXT_ALIGN.RIGHT })],
|
|
1058
|
+
],
|
|
1059
|
+
{
|
|
1060
|
+
column_widths: [6, 28, 14],
|
|
1061
|
+
header: [
|
|
1062
|
+
text("QTY", { bold: true }),
|
|
1063
|
+
text("ITEM", { bold: true }),
|
|
1064
|
+
text("TOTAL", { bold: true, align: TEXT_ALIGN.RIGHT }),
|
|
1065
|
+
],
|
|
1066
|
+
truncate: true,
|
|
1067
|
+
},
|
|
1068
|
+
),
|
|
1069
|
+
line("-"),
|
|
1070
|
+
text("Grand total: $9.50", { bold: true, size: TEXT_SIZE.DOUBLE, align: TEXT_ALIGN.RIGHT }),
|
|
1071
|
+
qr("https://example.com/r/A-1001", {
|
|
1072
|
+
size: 6,
|
|
1073
|
+
error_correction: QR_ERROR_CORRECTION.M,
|
|
1074
|
+
model: 2,
|
|
1075
|
+
align: TEXT_ALIGN.CENTER,
|
|
1076
|
+
}),
|
|
1077
|
+
barcode("123456789012", BARCODE_TYPE.EAN13, {
|
|
1078
|
+
width: 3,
|
|
1079
|
+
height: 70,
|
|
1080
|
+
text_position: BARCODE_TEXT_POSITION.BELOW,
|
|
1081
|
+
align: TEXT_ALIGN.CENTER,
|
|
1082
|
+
}),
|
|
1083
|
+
dataMatrix("A-1001", 6),
|
|
1084
|
+
pdf417("A-1001|TOTAL=9.50|PAID", {
|
|
1085
|
+
columns: 0,
|
|
1086
|
+
rows: 0,
|
|
1087
|
+
width: 2,
|
|
1088
|
+
height: 3,
|
|
1089
|
+
error_correction: 2,
|
|
1090
|
+
}),
|
|
1091
|
+
image("<BASE64_IMAGE>", {
|
|
1092
|
+
max_width: 0,
|
|
1093
|
+
align: TEXT_ALIGN.CENTER,
|
|
1094
|
+
dithering: true,
|
|
1095
|
+
size: IMAGE_MODE.NORMAL,
|
|
1096
|
+
}),
|
|
1097
|
+
logo(1, IMAGE_MODE.NORMAL),
|
|
1098
|
+
drawer(2, 120),
|
|
1099
|
+
beep(1, 3),
|
|
1100
|
+
feed(3),
|
|
1101
|
+
cut(),
|
|
1102
|
+
],
|
|
1103
|
+
};
|
|
1104
|
+
|
|
1105
|
+
await print_thermal_printer(job);
|
|
1106
|
+
```
|
|
1107
|
+
|
|
1108
|
+
---
|
|
1109
|
+
|
|
765
1110
|
## Examples
|
|
766
1111
|
|
|
767
1112
|
This section contains practical examples for different use cases. Each example demonstrates how to structure print jobs for various business scenarios.
|
|
@@ -771,7 +1116,7 @@ This section contains practical examples for different use cases. Each example d
|
|
|
771
1116
|
### 🛒 Long Receipt (Supermarket - 80mm)
|
|
772
1117
|
|
|
773
1118
|
```typescript
|
|
774
|
-
import { print_thermal_printer, type PrintJobRequest } from "tauri-plugin-thermal-printer
|
|
1119
|
+
import { print_thermal_printer, type PrintJobRequest } from "tauri-plugin-thermal-printer";
|
|
775
1120
|
|
|
776
1121
|
const receipt: PrintJobRequest = {
|
|
777
1122
|
"printer": "TM-T20II",
|
|
@@ -1221,6 +1566,8 @@ const paymentReceipt: PrintJobRequest = {
|
|
|
1221
1566
|
};
|
|
1222
1567
|
```
|
|
1223
1568
|
|
|
1569
|
+
|
|
1570
|
+
|
|
1224
1571
|
---
|
|
1225
1572
|
|
|
1226
1573
|
### 📋 Summary
|