Struct Printer

Source
pub struct Printer { /* private fields */ }
Expand description

Main escpos-rs structure

The printer represents the thermal printer connected to the computer.

use escpos_rs::{Printer, PrinterModel};

let printer = match Printer::new(PrinterModel::TMT20.usb_profile()) {
    Ok(maybe_printer) => match maybe_printer {
        Some(printer) => printer,
        None => panic!("No printer was found :(")
    },
    Err(e) => panic!("Error: {}", e)
};
// Now we have a printer

Implementations§

Source§

impl Printer

Source

pub fn new(printer_profile: PrinterProfile) -> Result<Option<Printer>, Error>

Creates a new printer

Creates the printer with the given details, from the printer details provided, and in the given USB context.

Examples found in repository?
examples/basic.rs (line 6)
3fn main() {
4    let printer_profile = PrinterProfile::terminal_builder().with_font_width(Font::FontA, 20).build();
5    // We pass it to the printer
6    let printer = match Printer::new(printer_profile) {
7        Ok(maybe_printer) => match maybe_printer {
8            Some(printer) => printer,
9            None => panic!("No printer was found :(")
10        },
11        Err(e) => panic!("Error: {}", e)
12    };
13    
14    // We print simple text
15    match printer.println("Hello, world!") {
16        Ok(_) => (),
17        Err(e) => println!("Error: {}", e)
18    }
19}
More examples
Hide additional examples
examples/space_split.rs (line 6)
3fn main() {
4    let printer_profile = PrinterProfile::terminal_builder().with_font_width(Font::FontA, 32).build();
5    // We pass it to the printer
6    let mut printer = match Printer::new(printer_profile) {
7        Ok(maybe_printer) => match maybe_printer {
8            Some(printer) => printer,
9            None => panic!("No printer was found :(")
10        },
11        Err(e) => panic!("Error: {}", e)
12    };
13    // We set word splitting
14    printer.set_space_split(true);
15
16    // We print simple text
17    match printer.println("Really long sentence that should be splitted into three components, yay!") {
18        Ok(_) => (),
19        Err(e) => println!("Error: {}", e)
20    }
21}
examples/tables.rs (line 6)
3fn main() {
4    let printer_profile = PrinterProfile::terminal_builder().with_font_width(Font::FontA, 20).build();
5    // We pass it to the printer
6    let mut printer = match Printer::new(printer_profile) {
7        Ok(maybe_printer) => match maybe_printer {
8            Some(printer) => printer,
9            None => panic!("No printer was found :(")
10        },
11        Err(e) => panic!("Error: {}", e)
12    };
13    // We set word splitting
14    printer.set_space_split(true);
15    
16    println!("Table with two columns:");
17    match printer.duo_table(("Product", "Price"), vec![
18        ("Milk", "5.00"),
19        ("Cereal", "10.00")
20    ]) {
21        Ok(_) => (),
22        Err(e) => println!("Error: {}", e)
23    }
24
25    println!("Table with three columns:");
26    match printer.trio_table(("Product", "Price", "Qty."), vec![
27        ("Milk", "5.00", "3"),
28        ("Cereal", "10.00", "1")
29    ]) {
30        Ok(_) => (),
31        Err(e) => println!("Error: {}", e)
32    }
33}
Source

pub fn with_context_feeling_lucky() -> Result<Option<Printer>, Error>

Guesses the printer, and connects to it (not meant for production)

Might help to find which printer you have if you have only one connected. The function will try to connect to a printer, based on the common ones recognized by this library.

Source

pub fn instruction( &self, instruction: &Instruction, print_data: Option<&PrintData>, ) -> Result<(), Error>

Print an instruction

You can pass optional printer data to the printer to fill in the dynamic parts of the instruction.

Source

pub fn print<T: Into<String>>(&self, content: T) -> Result<(), Error>

Print some text.

By default, lines will break when the text exceeds the current font’s width. If you want to break lines with whitespaces, according to the width, you can use the set_space_split function.

Source

pub fn print_gb18030(&self, content: String) -> Result<(), Error>

Print gb18030 text.

Use encoding and send raw data

Source

pub fn println<T: Into<String>>(&self, content: T) -> Result<(), Error>

Print some text, with a newline at the end.

By default, lines will break when the text exceeds the current font’s width. If you want to break lines with whitespaces, according to the width, you can use the set_space_split function.

Examples found in repository?
examples/basic.rs (line 15)
3fn main() {
4    let printer_profile = PrinterProfile::terminal_builder().with_font_width(Font::FontA, 20).build();
5    // We pass it to the printer
6    let printer = match Printer::new(printer_profile) {
7        Ok(maybe_printer) => match maybe_printer {
8            Some(printer) => printer,
9            None => panic!("No printer was found :(")
10        },
11        Err(e) => panic!("Error: {}", e)
12    };
13    
14    // We print simple text
15    match printer.println("Hello, world!") {
16        Ok(_) => (),
17        Err(e) => println!("Error: {}", e)
18    }
19}
More examples
Hide additional examples
examples/space_split.rs (line 17)
3fn main() {
4    let printer_profile = PrinterProfile::terminal_builder().with_font_width(Font::FontA, 32).build();
5    // We pass it to the printer
6    let mut printer = match Printer::new(printer_profile) {
7        Ok(maybe_printer) => match maybe_printer {
8            Some(printer) => printer,
9            None => panic!("No printer was found :(")
10        },
11        Err(e) => panic!("Error: {}", e)
12    };
13    // We set word splitting
14    printer.set_space_split(true);
15
16    // We print simple text
17    match printer.println("Really long sentence that should be splitted into three components, yay!") {
18        Ok(_) => (),
19        Err(e) => println!("Error: {}", e)
20    }
21}
Source

pub fn print_barcode( &self, content: String, position: BarcodePostion, ) -> Result<(), Error>

Print barcode use type code128

You also can set HRI code postion. None Top Bottom Both four postions.

Source

pub fn set_barcode_position( &self, position: BarcodePostion, ) -> Result<(), Error>

Source

pub fn set_font(&mut self, font: Font) -> Result<(), Error>

Sets the current printing font.

The function will return an error if the specified font does not exist in the printer profile.

Source

pub fn set_space_split(&mut self, state: bool)

Enables or disables space splitting for long text printing.

By default, the printer writes text in a single stream to the printer (which splits it wherever the maximum width is reached). To split by whitespaces, you can call this function with true as argument.

Examples found in repository?
examples/space_split.rs (line 14)
3fn main() {
4    let printer_profile = PrinterProfile::terminal_builder().with_font_width(Font::FontA, 32).build();
5    // We pass it to the printer
6    let mut printer = match Printer::new(printer_profile) {
7        Ok(maybe_printer) => match maybe_printer {
8            Some(printer) => printer,
9            None => panic!("No printer was found :(")
10        },
11        Err(e) => panic!("Error: {}", e)
12    };
13    // We set word splitting
14    printer.set_space_split(true);
15
16    // We print simple text
17    match printer.println("Really long sentence that should be splitted into three components, yay!") {
18        Ok(_) => (),
19        Err(e) => println!("Error: {}", e)
20    }
21}
More examples
Hide additional examples
examples/tables.rs (line 14)
3fn main() {
4    let printer_profile = PrinterProfile::terminal_builder().with_font_width(Font::FontA, 20).build();
5    // We pass it to the printer
6    let mut printer = match Printer::new(printer_profile) {
7        Ok(maybe_printer) => match maybe_printer {
8            Some(printer) => printer,
9            None => panic!("No printer was found :(")
10        },
11        Err(e) => panic!("Error: {}", e)
12    };
13    // We set word splitting
14    printer.set_space_split(true);
15    
16    println!("Table with two columns:");
17    match printer.duo_table(("Product", "Price"), vec![
18        ("Milk", "5.00"),
19        ("Cereal", "10.00")
20    ]) {
21        Ok(_) => (),
22        Err(e) => println!("Error: {}", e)
23    }
24
25    println!("Table with three columns:");
26    match printer.trio_table(("Product", "Price", "Qty."), vec![
27        ("Milk", "5.00", "3"),
28        ("Cereal", "10.00", "1")
29    ]) {
30        Ok(_) => (),
31        Err(e) => println!("Error: {}", e)
32    }
33}
Source

pub fn jump(&self, n: u8) -> Result<(), Error>

Jumps n number of lines (to leave whitespaces). Basically n * '\n' passed to print

Source

pub fn cut(&self) -> Result<(), Error>

Cuts the paper, in case the instruction is supported by the printer

Source

pub fn duo_table<A: Into<String>, B: Into<String>, C: IntoIterator<Item = (D, E)>, D: Into<String>, E: Into<String>>( &self, headers: (A, B), rows: C, ) -> Result<(), Error>

Prints a table with two columns.

For more details, check Formatter’s duo_table.

Examples found in repository?
examples/tables.rs (lines 17-20)
3fn main() {
4    let printer_profile = PrinterProfile::terminal_builder().with_font_width(Font::FontA, 20).build();
5    // We pass it to the printer
6    let mut printer = match Printer::new(printer_profile) {
7        Ok(maybe_printer) => match maybe_printer {
8            Some(printer) => printer,
9            None => panic!("No printer was found :(")
10        },
11        Err(e) => panic!("Error: {}", e)
12    };
13    // We set word splitting
14    printer.set_space_split(true);
15    
16    println!("Table with two columns:");
17    match printer.duo_table(("Product", "Price"), vec![
18        ("Milk", "5.00"),
19        ("Cereal", "10.00")
20    ]) {
21        Ok(_) => (),
22        Err(e) => println!("Error: {}", e)
23    }
24
25    println!("Table with three columns:");
26    match printer.trio_table(("Product", "Price", "Qty."), vec![
27        ("Milk", "5.00", "3"),
28        ("Cereal", "10.00", "1")
29    ]) {
30        Ok(_) => (),
31        Err(e) => println!("Error: {}", e)
32    }
33}
Source

pub fn trio_table<A: Into<String>, B: Into<String>, C: Into<String>, D: IntoIterator<Item = (E, F, G)>, E: Into<String>, F: Into<String>, G: Into<String>>( &self, headers: (A, B, C), rows: D, ) -> Result<(), Error>

Prints a table with three columns.

For more details, check Formatter’s trio_table.

Examples found in repository?
examples/tables.rs (lines 26-29)
3fn main() {
4    let printer_profile = PrinterProfile::terminal_builder().with_font_width(Font::FontA, 20).build();
5    // We pass it to the printer
6    let mut printer = match Printer::new(printer_profile) {
7        Ok(maybe_printer) => match maybe_printer {
8            Some(printer) => printer,
9            None => panic!("No printer was found :(")
10        },
11        Err(e) => panic!("Error: {}", e)
12    };
13    // We set word splitting
14    printer.set_space_split(true);
15    
16    println!("Table with two columns:");
17    match printer.duo_table(("Product", "Price"), vec![
18        ("Milk", "5.00"),
19        ("Cereal", "10.00")
20    ]) {
21        Ok(_) => (),
22        Err(e) => println!("Error: {}", e)
23    }
24
25    println!("Table with three columns:");
26    match printer.trio_table(("Product", "Price", "Qty."), vec![
27        ("Milk", "5.00", "3"),
28        ("Cereal", "10.00", "1")
29    ]) {
30        Ok(_) => (),
31        Err(e) => println!("Error: {}", e)
32    }
33}
Source

pub fn image(&self, escpos_image: EscposImage) -> Result<(), Error>

Source

pub fn raw<A: AsRef<[u8]>>(&self, bytes: A) -> Result<(), Error>

Sends raw information to the printer

As simple as it sounds

use escpos_rs::{Printer,PrinterProfile};
let printer_profile = PrinterProfile::usb_builder(0x0001, 0x0001).build();
let printer = Printer::new(printer_profile).unwrap().unwrap();
printer.raw(&[0x01, 0x02])?;

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.