Expand description
A library for building ZPL II label strings to be sent to Zebra thermal printers.
§Overview
ZPL (Zebra Programming Language) is a label description language interpreted
by Zebra printers. This library lets you build valid ZPL strings in Rust
without constructing raw strings by hand. All field values are validated
against the ZPL II specification at construction time — an invalid value
returns a ZplError rather than silently producing a malformed command.
§Quick start
use zpl_builder::{LabelBuilder, BarcodeType, Color, Orientation};
let zpl = LabelBuilder::new()
.set_home_position(10, 10).unwrap()
.add_text("Hello, printer!", 50, 50, 'A', 30, Orientation::Normal).unwrap()
.add_graphical_box(10, 10, 500, 300, 3, Color::Black, 0).unwrap()
.add_barcode(BarcodeType::Code128, "ABC-123", 50, 120, 2, 2.5, 80, Orientation::Normal).unwrap()
.build();
assert!(zpl.starts_with("^XA"));
assert!(zpl.ends_with("^XZ"));§Elements
The builder supports the following ZPL elements:
| Method | ZPL command | Description |
|---|---|---|
LabelBuilder::add_text | ^A / ^FD | Text field |
LabelBuilder::add_barcode | ^BY / ^B* | Barcode (27 types) |
LabelBuilder::add_graphical_box | ^GB | Rectangle |
LabelBuilder::add_graphical_circle | ^GC | Circle |
LabelBuilder::add_graphical_ellipse | ^GE | Ellipse |
LabelBuilder::add_graphical_diagonal_line | ^GD | Diagonal line |
§Error handling
Every builder method returns Result<LabelBuilder, ZplError>. This means
you can chain calls with ? in a function that returns Result:
use zpl_builder::{LabelBuilder, Color, Orientation, ZplError};
fn build_shipping_label(tracking: &str) -> Result<String, ZplError> {
Ok(LabelBuilder::new()
.set_home_position(0, 0)?
.add_text("SHIP TO:", 10, 10, 'B', 20, Orientation::Normal)?
.add_graphical_box(5, 5, 400, 200, 2, Color::Black, 0)?
.build())
}Or with .unwrap() / .expect() when the values are known to be valid:
use zpl_builder::{LabelBuilder, Orientation};
let zpl = LabelBuilder::new()
.add_text("Fixed label", 10, 10, 'A', 20, Orientation::Normal).unwrap()
.build();See ZplError for the full list of validation errors and their ranges.
Structs§
- Label
Builder - Builder for ZPL II label strings.
Enums§
- Barcode
Type - Barcode symbology.
- Color
- Line and fill color for graphical elements.
- Diagonal
Orientation - Orientation of a diagonal line.
- Orientation
- Orientation of a text or barcode field.
- ZplError
- Errors that can occur when building a ZPL label.