24 个版本 (12 个重大变更)
| 0.13.0 | 2024年8月8日 |
|---|---|
| 0.12.2 | 2024年4月23日 |
| 0.11.0 | 2024年4月16日 |
| 0.9.0 | 2024年3月29日 |
| 0.6.0 | 2023年11月17日 |
#66 在 硬件支持
每月下载 182 次
310KB
5K SLoC
ESC/POS Rust 实现
本crate实现了Epson ESC/POS协议的一部分,用于热敏打印机。它允许您在兼容的打印机上生成和打印具有基本文本格式化、裁剪、条形码、二维码和位图图像的文档。它还提供了一种检查打印机状态的方法。

打印在 Aures ODP 333 上
该项目深受 recibo (Rust)、escposify (Rust) 和 escpos (Go) 的启发。
安装
对于标准功能(例如打印文本),无需额外的依赖项
[dependencies]
escpos = "0.13.0"
如果您需要所有 功能,可以使用 full 功能
[dependencies]
escpos = { version = "0.13.0", features = ["full"] }
或者您可以使用 cargo add 命令
cargo add escpos
cargo add escpos -F full
代码覆盖率
使用的工具: tarpaulin
cargo install cargo-tarpaulin
cargo tarpaulin --all-features
结果
- [2024-08-08]
57.75% 覆盖率, 1121/1941 行被覆盖
功能列表
| 名称 | 描述 | 默认 |
|---|---|---|
barcodes |
打印条形码(UPC-A、UPC-E、EAN8、EAN13、CODE39、ITF 或 CODABAR) | ✅ |
codes_2d |
打印 2D 码(QR 码、PDF417、GS1 DataBar、DataMatrix、Aztec 等。) | ✅ |
graphics |
打印位图图像 | ❌ |
usb |
启用 USB 功能 | ❌ |
native_usb |
启用原生 USB 功能 | ❌ |
hidapi |
启用 HidApi 功能 | ❌ |
serial_port |
启用串行端口功能 | ❌ |
full |
启用所有功能 | ❌ |
示例
“examples”文件夹包含各种使用escpos的示例。文档(将)也将提供大量的代码片段和示例。
要启动示例,请使用以下命令
RUST_LOG=debug cargo run --example full --features full
RUST_LOG=debug cargo run --example receipt -F full
RUST_LOG=debug cargo run --example codes
RUST_LOG=debug cargo run --example debug
RUST_LOG=debug cargo run --example page_codes
RUST_LOG=debug cargo run --example usb --features usb
RUST_LOG=debug cargo run --example native_usb --features native_usb
RUST_LOG=debug cargo run --example hidapi --features hidapi
RUST_LOG=debug cargo run --example serial_port --features serial_port
RUST_LOG=debug cargo run --example status --all-features
简单的文本格式化
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()?;
Ok(())
}
EAN13
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_cut()?;
Ok(())
}
QR码
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()?;
Ok(())
}
位图像(启用graphics功能)
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()?;
Ok(())
}
检查打印机状态
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(())
}
命令列表
| 状态 | 命令 | 描述 | 功能 |
|---|---|---|---|
| ✅ | init() |
初始化打印机(ESC @) |
|
| ✅ | print() |
打印文档 | |
| ✅ | reset() |
硬件重置(ESC ? LF 0) |
|
| ✅ | cut() |
纸张切割(GS V A 0) |
|
| ✅ | partial_cut() |
部分纸张切割(GS V A 1) |
|
| ✅ | print_cut() |
打印和纸张切割 | |
| ✅ | page_code() |
选择字符码表(ESC t) |
|
| ✅ | character_set() |
选择国际字符集(ESC R) |
|
| ✅ | bold() |
文本加粗(ESC E) |
|
| ✅ | underline() |
文本下划线(ESC -) |
|
| ✅ | double_strike() |
文本双击(ESC G) |
|
| ✅ | font() |
文本字体(ESC M) |
|
| ✅ | flip() |
文本翻转(ESC V) |
|
| ✅ | justify() |
文本对齐(ESC a) |
|
| ✅ | reserve() |
文本保留颜色(GS B) |
|
| ✅ | size() |
文本大小(GS !) |
|
| ✅ | reset_size() |
重置文本大小(GS !) |
|
| ✅ | smoothing() |
平滑模式(GS b) |
|
| ✅ | feed() |
换行(ESC d) |
|
| ✅ | feeds() |
多行换行(ESC d) |
|
| ✅ | line_spacing() |
行间距(ESC 3) |
|
| ✅ | reset_line_spacing() |
重置行间距(ESC 2) |
|
| ✅ | upside_down() |
倒置模式(ESC {) |
|
| ✅ | cash_drawer() |
生成脉冲(ESC p) |
|
| ✅ | write() |
写入文本 | |
| ✅ | writeln() |
写入文本和换行 | |
| ✅ | custom() |
自定义命令 | |
| ✅ | custom_with_page_code() |
带有页面代码的自定义命令 | |
| ✅ | motion_units() |
设置水平和垂直运动单位(GS P) |
|
| ✅ | ean13() |
使用默认选项打印EAN13 | barcode |
| ✅ | ean13_option() |
使用自定义选项打印EAN13 | barcode |
| ✅ | ean8() |
使用默认选项打印EAN8 | barcode |
| ✅ | ean8_option() |
使用自定义选项打印EAN8 | barcode |
| ✅ | upca() |
使用默认选项打印UPC-A | barcode |
| ✅ | upca_option() |
使用自定义选项打印UPC-A | barcode |
| ✅ | upce() |
使用默认选项打印UPC-E | barcode |
| ✅ | upce_option() |
使用自定义选项打印UPC-E | barcode |
| ✅ | code39() |
使用默认选项打印CODE 39 | barcode |
| ✅ | code39_option() |
使用自定义选项打印CODE 39 | barcode |
| ✅ | codabar() |
使用默认选项打印CODABAR | barcode |
| ✅ | codabar_option() |
使用自定义选项打印CODABAR | barcode |
| ✅ | itf() |
使用默认选项打印ITF | barcode |
| ✅ | itf_option() |
使用自定义选项打印ITF | barcode |
| ✅ | qrcode() |
使用默认选项打印QR码 | codes_2d |
| ✅ | qrcode_option() |
使用自定义选项打印QR码 | codes_2d |
| ✅ | bit_image() |
使用默认选项打印光栅位图 | graphics |
| ✅ | bit_image_option() |
使用自定义选项打印光栅位图 | graphics |
| ✅ | bit_image_from_bytes() |
使用默认选项从字节打印光栅位图 | graphics |
| ✅ | bit_image_from_bytes_option() |
使用自定义选项从字节打印光栅位图 | graphics |
| ✅ | gs1_databar_2d |
使用默认选项打印2D GS1 DataBar | codes_2d |
| ✅ | gs1_databar_2d_option |
使用自定义选项打印2D GS1 DataBar | codes_2d |
| ✅ | pdf417 |
使用默认选项打印PDF417 | codes_2d |
| ✅ | pdf417_option |
使用自定义选项打印PDF417 | codes_2d |
| ✅ | maxi_code |
使用默认选项打印MaxiCode | codes_2d |
| ✅ | maxi_code_option |
使用自定义选项打印MaxiCode | codes_2d |
| ✅ | data_matrix |
使用默认选项打印DataMatrix | codes_2d |
| ✅ | data_matrix_option |
使用自定义选项打印DataMatrix | codes_2d |
| ✅ | aztec |
使用默认选项打印Aztec码 | codes_2d |
| ✅ | aztec_option |
使用自定义选项打印Aztec码 | codes_2d |
| 🚧 | graphic() |
使用默认选项打印光栅图形 | graphics |
| 🚧 | graphic_option() |
使用自定义选项打印光栅图形 | graphics |
- ✅ 完成
- 🚧 进行中
- ❌ 待办
页面代码列表
| 代码 | 已实现? |
|---|---|
| PC437 | ✅ |
| 片假名 | ✅ |
| PC850 | ✅ |
| PC860 | ✅ |
| PC863 | ✅ |
| PC865 | ✅ |
| 平假名 | ❌ |
| 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 | ✅ |
外部资源
依赖关系
~3–17MB
~233K SLoC