Skip to content
zachzurn edited this page Nov 12, 2024 · 6 revisions

Thermal contains two main crates:

Quick Start

To get started, it's easiest to use the .thermal file format.

  1. Create a file called hello_world.thermal

  2. Add these lines to the file:

'// Initialize
ESC "@"

'// Print Hello World
"Hello World"
  1. Run the code to render the image
// Read string from thermal file
let text = read_to_string("hello_world.thermal");

//Parse into esc/pos commands
let bytes = parse_str( & text);

//Render to html
let renders = ImageRenderer::render(bytes, None);

// renders.output may contain multiple renders
// For this example we grab the first.
//
// Write the image to a file.
// Error handling is left out for brevity
if let Some(render) = renders.output.first() {
    save_image( & render.bytes, render.width, render.height, "hello_world.png");
}

// Error handling is left out for brevity
// png crate required for this example
fn save_image(bytes: &Vec<u8>, width: u32, height: u32, out_path: String) {
    let path = Path::new(&out_path);
    let file = File::create(path).unwrap();
    let ref mut writer = BufWriter::new(file);
    let mut encoder = png::Encoder::new(writer, width, height);

    encoder.set_color(png::ColorType::Rgb);
    encoder.set_depth(BitDepth::Eight);

    let mut writer = encoder.write_header().unwrap();
    writer.write_image_data(bytes).unwrap();
}

Thermal Parser

Handles parsing and emitting commands and contains the context that may change with each command.

Thermal Renderer

Uses the parser crate and allows for the creation of renderers.

  • HTML Renderer
  • Image Renderer
Clone this wiki locally