escpos/document
A high-level API for building ESC/POS printer documents.
Documents are built by composing commands and modifiers, then compiled into a binary command buffer that can be sent to a printer.
Example
import escpos/document.{bold, cut, justify, line, styled, text, Center}
document.render([
styled([justify(Center), bold()], [
line([text("Receipt")]),
]),
line([text("Item 1 ... $5.00")]),
cut(),
])
Types
Printer font. Re-exported from escpos/protocol.
pub type Font =
protocol.Font
Text justification. Re-exported from escpos/protocol.
pub type Justify =
protocol.Justify
Values
pub fn image(image: image.PrintableImage) -> Command
Prints an image.
Example
let assert Ok(pgm) = simplifile.read_bits(from: "./lucy.pgm")
let assert Ok(img) = image.from_pgm(raw_pgm)
let img = image.dither_bayer4x4(img, 0)
document.render([
image(img),
cut(),
])
pub fn image_raster(image: image.PrintableImage) -> Command
Prints a monochrome image prepared by the image module. This is using an older
method to print images, useful for some printers that do not understand graphic buffer
commands.
Example
let assert Ok(pgm) = simplifile.read_bits(from: "./lucy.pgm")
let assert Ok(img) = image.from_pgm(raw_pgm)
let img = image.dither_bayer4x4(img, 0)
document.render([
raster_image(img),
cut(),
])
pub fn line(commands: List(Command)) -> Command
Groups commands into a single line, with a newline at the end. Useful for combining styled and unstyled text on one line.
Example
line([
styled([bold()], [text("Total:")]),
text(" $12.99"),
])
pub fn line_feed(lines: Int) -> Command
Advances the paper by the specified number of lines.
pub fn partial_cut() -> Command
Performs a partial paper cut, leaving a small portion connected.
pub fn render(document: List(Command)) -> printer.CommandBuffer
Compiles a list of commands into a binary command buffer ready for printing.
pub fn styled(
modifiers: List(Modifier),
commands: List(Command),
) -> Command
Applies style modifiers to a group of commands. Styles are automatically reverted after the nested commands.
pub fn text_size(size: Int) -> Modifier
Sets both text width and height scaling (0-7).
pub fn upside_down() -> Modifier
Prints text upside down (requires newline before and after).