escpos 0.19.0

ESC/POS printer driver
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# ESC/POS Rust implementation

[![Build status](https://github.com/fabienbellanger/escpos-rs/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/fabienbellanger/escpos-rs/actions/workflows/CI.yml)
[![Crates.io](https://img.shields.io/crates/v/escpos)](https://crates.io/crates/escpos)
[![Documentation](https://docs.rs/escpos/badge.svg)](https://docs.rs/escpos)

This crate implements a subset of Epson's ESC/POS protocol for thermal receipt printers. It allows you to generate and
print documents with basic text formatting, cutting, barcodes, QR codes and raster images on a compatible printer. It
also provides a way to check the printer status.

![Ticket](resources/documentation/ticket.jpg) ![Receipt](resources/documentation/receipt.jpg)  
_Printed on Aures ODP 333_

This project is strongly inspired by [recibo](https://github.com/jamhall/recibo/tree/main) _(Rust)_,
[escposify](https://github.com/local-group/rust-escposify) _(Rust)_
and [escpos](https://github.com/hennedo/escpos) _(Go)_.

## Installation

For standard functionalities (e.g., printing text), no additional dependencies are required:

```toml
[dependencies]
escpos = "0.19.0"
```

If you need all [features](#Features-list), you can use the `full` feature:

```toml
[dependencies]
escpos = { version = "0.19.0", features = ["full"] }
```

Or you can use `cargo add` command:

```bash
cargo add escpos
cargo add escpos -F full
```

## Code coverage

Tool used: [tarpaulin](https://github.com/xd009642/tarpaulin)

```shell
cargo install cargo-tarpaulin
```

```shell
cargo tarpaulin --all-features --tests

```

_Results:_

- [2025-09-15] `57.62% coverage, 1172/2034 lines covered`

## MSRV

Tool used: [cargo-msrv](https://github.com/foresterre/cargo-msrv)

```shell
cargo install cargo-msrv
```

```shell
cargo msrv find
cargo msrv verify
```

## Features list

| Name          | Description                                                            | Default |
|---------------|------------------------------------------------------------------------|:-------:|
| `std`         | Enable `std` support (disable for `no_std` + `alloc` environments)     ||
| `barcodes`    | Print barcodes (UPC-A, UPC-E, EAN8, EAN13, CODE39, ITF or CODABAR)     ||
| `codes_2d`    | Print 2D codes (QR Code, PDF417, GS1 DataBar, DataMatrix, Aztec, etc.) ||
| `graphics`    | Print raster images (requires `std`)                                   ||
| `usb`         | Enable USB feature (requires `std`)                                    ||
| `native_usb`  | Enable native USB feature (requires `std`)                             ||
| `hidapi`      | Enable HidApi feature (requires `std`)                                 ||
| `serial_port` | Enable Serial port feature (requires `std`)                            ||
| `usbprint`    | Enable Windows USB print driver (`usbprint.sys` via Win32 API)         ||
| `ui`          | Enable ui feature (UI components)                                      ||
| `full`        | Enable all features                                                    ||

## `no_std` support

The crate can be used on bare-metal targets (microcontrollers, kernels…) by disabling default features. Only `alloc` is
required:

```toml
[dependencies]
escpos = { version = "0.18", default-features = false, features = ["barcodes", "codes_2d"] }
```

The built-in `Console`, `Network` and `File` drivers as well as the `graphics` feature require
`std`. In `no_std` mode you implement the `Driver` trait for your peripheral (UART, SPI, USB endpoint, …) and pass it to
`Printer::new`. The `Printer::driver` accessor lets you recover the driver from a `Printer`.

See [`examples/no_std_codes.rs`](examples/no_std_codes.rs) for a minimal example with a custom in-memory driver and
barcodes / 2D codes — all the protocol-level APIs (barcodes, QR Code, PDF417, DataMatrix, Aztec, MaxiCode, page codes,
status…) work in `no_std`. The codes themselves are rendered by the printer's firmware: the crate only serializes the
ESC/POS commands, so the embedded-side cost is minimal.

## Examples

The `examples` folder contains various examples of how to use `escpos`.  
The [docs](https://docs.rs/escpos) also provide code snippets and examples.

To launch an example, use the following command:

```shell
RUST_LOG=debug cargo run --example full --features full
```

The list of all the examples can be found [here](examples/EXAMPLES.md).

### Simple text formatting

```rust
use escpos::printer::Printer;
use escpos::printer_options::PrinterOptions;
use escpos::utils::*;
use escpos::{driver::*, errors::Result};

fn main() -> Result<()> {
    env_logger::init();

    let driver = NetworkDriver::open("192.168.1.248", 9100, None)?;
    Printer::new(driver, Protocol::default(), Some(PrinterOptions::default()))
        .debug_mode(Some(DebugMode::Dec))
        .init()?
        .smoothing(true)?
        .bold(true)?
        .underline(UnderlineMode::Single)?
        .writeln("Bold underline")?
        .justify(JustifyMode::CENTER)?
        .reverse(true)?
        .bold(false)?
        .writeln("Hello world - Reverse")?
        .feed()?
        .justify(JustifyMode::RIGHT)?
        .reverse(false)?
        .underline(UnderlineMode::None)?
        .size(2, 3)?
        .writeln("Hello world - Normal")?
        .print_cut()?; // print() or print_cut() is mandatory to send the data to the printer

    Ok(())
}
```

### EAN13

```rust
use escpos::printer::Printer;
use escpos::utils::*;
use escpos::{driver::*, errors::Result};

fn main() -> Result<()> {
    env_logger::init();

    let driver = ConsoleDriver::open(true);
    Printer::new(driver, Protocol::default(), None)
        .debug_mode(Some(DebugMode::Hex))
        .init()?
        .ean13_option(
            "1234567890265",
            BarcodeOption::new(
                BarcodeWidth::M,
                BarcodeHeight::S,
                BarcodeFont::A,
                BarcodePosition::Below,
            )
        )?
        .feed()?
        .print()?; // print() or print_cut() is mandatory to send the data to the printer
    Ok(())
}
```

### QR Code

```rust
use escpos::printer::Printer;
use escpos::utils::*;
use escpos::{driver::*, errors::Result};

fn main() -> Result<()> {
    env_logger::init();

    let driver = ConsoleDriver::open(true);
    Printer::new(driver, Protocol::default(), None)
        .debug_mode(Some(DebugMode::Hex))
        .init()?
        .qrcode_option(
            "https://www.google.com",
            QRCodeOption::new(QRCodeModel::Model1, 6, QRCodeCorrectionLevel::M),
        )?
        .feed()?
        .print_cut()?; // print() or print_cut() is mandatory to send the data to the printer

    Ok(())
}
```

### Bit image (with `graphics` feature enabled)

```rust
use escpos::printer::Printer;
use escpos::utils::*;
use escpos::{driver::*, errors::Result};

fn main() -> Result<()> {
    env_logger::init();

    let driver = ConsoleDriver::open(true);
    let mut printer = Printer::new(driver, Protocol::default(), None);
    printer.debug_mode(Some(DebugMode::Hex))
        .init()?
        .bit_image_option(
            "./resources/images/rust-logo-small.png",
            BitImageOption::new(Some(128), None, BitImageSize::Normal)?,
        )?
        .feed()?
        .print_cut()?; // print() or print_cut() is mandatory to send the data to the printer

    Ok(())
}
```

### Windows USB print driver (with `usbprint` feature enabled, Windows only)

Drives a POS printer through the standard Windows `usbprint.sys` kernel driver using the Win32 API (`CreateFile` /
`ReadFile` / `WriteFile`). No Zadig / WinUSB / libusb swap required — the device keeps working with the regular Windows
print spooler at the same time.

```rust,ignore
use escpos::printer::Printer;
use escpos::utils::*;
use escpos::{driver::*, errors::Result};

fn main() -> Result<()> {
    env_logger::init();

    // List USB printers exposed by usbprint.sys
    for info in WindowsUsbPrintDriver::list()? {
        println!("{} (VID={:?} PID={:?})", info.device_path, info.vendor_id, info.product_id);
    }

    // Option 1: open by USB vendor/product id
    let driver = WindowsUsbPrintDriver::open_by_vid_pid(0x0525, 0xa700)?;

    // Option 2: open by full device interface path
    // let driver = WindowsUsbPrintDriver::open(r"\\?\USB#VID_0525&PID_A700#...")?;

    Printer::new(driver, Protocol::default(), None)
        .init()?
        .writeln("Hello from Windows usbprint!")?
        .print_cut()?;

    Ok(())
}
```

### Check printer status

```rust
use escpos::printer::Printer;
use escpos::utils::*;
use escpos::{driver::*, errors::Result};

fn main() -> Result<()> {
    env_logger::init();

    let driver = ConsoleDriver::open(true);
    Printer::new(driver.clone(), Protocol::default(), None)
        .debug_mode(Some(DebugMode::Dec))
        .real_time_status(RealTimeStatusRequest::Printer)?
        .real_time_status(RealTimeStatusRequest::RollPaperSensor)?
        .send_status()?;

    let mut buf = [0; 1];
    driver.read(&mut buf)?;

    let status = RealTimeStatusResponse::parse(RealTimeStatusRequest::Printer, buf[0])?;
    println!(
        "Printer online: {}",
        status.get(&RealTimeStatusResponse::Online).unwrap_or(&false)
    );

    Ok(())
}
```

## Commands list

| Status | Command                         | Description                                           | Feature    |
|:------:|---------------------------------|-------------------------------------------------------|------------|
|| `init()`                        | Initialize printer (`ESC @`)                          |            |
|| `print()`                       | Print document                                        |            |
|| `reset()`                       | Hardware reset (`ESC ? LF 0`)                         |            |
|| `cut()`                         | Paper cut (`GS V A 0`)                                |            |
|| `partial_cut()`                 | Partial paper cut (`GS V A 1`)                        |            |
|| `print_cut()`                   | Print and paper cut                                   |            |
|| `page_code()`                   | Select character code table (`ESC t`)                 |            |
|| `character_set()`               | Select an international character set (`ESC R`)       |            |
|| `bold()`                        | Text bold (`ESC E`)                                   |            |
|| `underline()`                   | Text underline (`ESC -`)                              |            |
|| `double_strike()`               | Text double strike (`ESC G`)                          |            |
|| `font()`                        | Text font (`ESC M`)                                   |            |
|| `flip()`                        | Text flip (`ESC V`)                                   |            |
|| `justify()`                     | Text justify (`ESC a`)                                |            |
|| `reverse()`                     | Text reverse color (`GS B`)                           |            |
|| `size()`                        | Text size (`GS !`)                                    |            |
|| `reset_size()`                  | Reset text size (`GS !`)                              |            |
|| `smoothing()`                   | Smoothing mode (`GS b`)                               |            |
|| `feed()`                        | Line feed (`ESC d`)                                   |            |
|| `feeds()`                       | Multiple lines feed (`ESC d`)                         |            |
|| `line_spacing()`                | Line spacing (`ESC 3`)                                |            |
|| `reset_line_spacing()`          | Reset line spacing (`ESC 2`)                          |            |
|| `upside_down()`                 | Upside-down mode (`ESC {`)                            |            |
|| `cash_drawer()`                 | Generate pulse (`ESC p`)                              |            |
|| `write()`                       | Write text                                            |            |
|| `writeln()`                     | Write text and line feed                              |            |
|| `custom()`                      | Custom command                                        |            |
|| `custom_with_page_code()`       | Custom command with page code                         |            |
|| `motion_units()`                | Set horizontal and vertical motion units (`GS P`)     |            |
|| `ean13()`                       | Print EAN13 with default option                       | `barcode`  |
|| `ean13_option()`                | Print EAN13 with custom option                        | `barcode`  |
|| `ean8()`                        | Print EAN8 with default option                        | `barcode`  |
|| `ean8_option()`                 | Print EAN8 with custom option                         | `barcode`  |
|| `upca()`                        | Print UPC-A with default option                       | `barcode`  |
|| `upca_option()`                 | Print UPC-A with custom option                        | `barcode`  |
|| `upce()`                        | Print UPC-E with default option                       | `barcode`  |
|| `upce_option()`                 | Print UPC-E with custom option                        | `barcode`  |
|| `code39()`                      | Print CODE 39 with default option                     | `barcode`  |
|| `code39_option()`               | Print CODE 39 with custom option                      | `barcode`  |
|| `codabar()`                     | Print CODABAR with default option                     | `barcode`  |
|| `codabar_option()`              | Print CODABAR with custom option                      | `barcode`  |
|| `itf()`                         | Print ITF with default option                         | `barcode`  |
|| `itf_option()`                  | Print ITF with custom option                          | `barcode`  |
|| `qrcode()`                      | Print QR code with default option                     | `codes_2d` |
|| `qrcode_option()`               | Print QR code with custom option                      | `codes_2d` |
|| `bit_image()`                   | Print raster bit image with default option            | `graphics` |
|| `bit_image_option()`            | Print raster bit image with custom option             | `graphics` |
|| `bit_image_from_bytes()`        | Print raster bit image from bytes with default option | `graphics` |
|| `bit_image_from_bytes_option()` | Print raster bit image from bytes with custom option  | `graphics` |
|| `gs1_databar_2d`                | Print 2D GS1 DataBar with default option              | `codes_2d` |
|| `gs1_databar_2d_option`         | Print 2D GS1 DataBar with custom option               | `codes_2d` |
|| `pdf417`                        | Print PDF417 with default option                      | `codes_2d` |
|| `pdf417_option`                 | Print PDF417 with custom option                       | `codes_2d` |
|| `maxi_code`                     | Print MaxiCode with default option                    | `codes_2d` |
|| `maxi_code_option`              | Print MaxiCode with custom option                     | `codes_2d` |
|| `data_matrix`                   | Print DataMatrix with default option                  | `codes_2d` |
|| `data_matrix_option`            | Print DataMatrix with custom option                   | `codes_2d` |
|| `aztec`                         | Print Aztec code with default option                  | `codes_2d` |
|| `aztec_option`                  | Print Aztec code with custom option                   | `codes_2d` |
|   🚧   | `graphic()`                     | Print raster graphic with default option              | `graphics` |
|   🚧   | `graphic_option()`              | Print raster graphic with custom option               | `graphics` |
|| `draw_line()`                   | Print a line                                          | `ui`       |

- ✅ Done
- 🚧 In progress
- ❌ To do

## Page codes list

| Code       | Implemented? |
|:-----------|:------------:|
| PC437      ||
| Katakana   ||
| PC850      ||
| PC860      ||
| PC863      ||
| PC865      ||
| Hiragana   ||
| PC851      ||
| PC853      ||
| PC857      ||
| PC737      ||
| ISO8859_7  ||
| WPC1252    ||
| PC866      ||
| PC852      ||
| PC858      ||
| PC720      ||
| WPC775     ||
| PC855      ||
| PC861      ||
| PC862      ||
| PC864      ||
| PC869      ||
| ISO8859_2  ||
| ISO8859_15 ||
| PC1098     ||
| PC1118     ||
| PC1119     ||
| PC1125     ||
| WPC1250    ||
| WPC1251    ||
| WPC1253    ||
| WPC1254    ||
| WPC1255    ||
| WPC1256    ||
| WPC1257    ||
| WPC1258    ||
| KZ1048     ||

## External resources

- [Epson documentation]https://download4.epson.biz/sec_pubs/pos/reference_en/escpos