Struct PrinterProfileBuilder

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

Helper structure to create a PrinterProfile

Builder pattern for the PrinterProfile structure.

Implementations§

Source§

impl PrinterProfileBuilder

Source

pub fn new_usb(vendor_id: u16, product_id: u16) -> PrinterProfileBuilder

Creates a new PrinterProfileBuilder set for usb printing

use escpos_rs::PrinterProfileBuilder;
// Creates a minimum data structure to connect to a printer
let printer_profile_builder = PrinterProfileBuilder::new_usb(0x0001, 0x0001);

The data structure will be properly built just with the vendor id and the product id. The Printer’s new method will try to locate a bulk write endpoint, but it might fail to do so. See with_endpoint for manual setup.

By default, a width of 384 dots and the FontA with 32 columns of width will be loaded with the profile.

Source

pub fn new_terminal() -> PrinterProfileBuilder

Creates a new PrinterProfileBuilder set for terminal printing

use escpos_rs::PrinterProfileBuilder;
// Creates a minimum (probably non-working) data structure to connect to a printer
let printer_profile_builder = PrinterProfileBuilder::new_terminal();

The printer will have a 32-char width for printing text, and a default with of 384 (but it cannot be used, as pictures can’t be printed to the terminal).

Source

pub fn with_endpoint(self, endpoint: u8) -> Result<PrinterProfileBuilder, Error>

Sets the usb endpoint to which the data will be written.

use escpos_rs::PrinterProfileBuilder;
// Creates the printer details with the endpoint 0x02
let printer_profile = PrinterProfileBuilder::new_usb(0x0001, 0x0001)
    .with_endpoint(0x02).unwrap()
    .build();
Source

pub fn with_width(self, width: u16) -> PrinterProfileBuilder

Adds a specific pixel width for the printer (required for printing images)

Defaults to 384, usually for 58mm printers.

use escpos_rs::PrinterProfileBuilder;
let printer_profile = PrinterProfileBuilder::new_usb(0x0001, 0x0001)
    .with_width(384)
    .build();
Source

pub fn with_font_width(self, font: Font, width: u8) -> PrinterProfileBuilder

Adds a specific width per font

This allows the justification, and proper word splitting to work. If you feel insecure about what value to use, the default font (FontA) usually has 32 characters of width for 58mm paper printers, and 48 for 80mm paper. You can also look for the specsheet, or do trial and error.

use escpos_rs::{PrinterProfileBuilder, command::Font};
let printer_profile = PrinterProfileBuilder::new_usb(0x0001, 0x0001)
    .with_font_width(Font::FontA, 32)
    .build();
Examples found in repository?
examples/basic.rs (line 4)
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 4)
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 4)
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_timeout( self, timeout: Duration, ) -> Result<PrinterProfileBuilder, Error>

Adds a bulk write timeout (usb only)

USB devices might fail to write to the bulk endpoint. In such a case, a timeout must be provided to know when to stop waiting for the buffer to flush to the printer. The default value is 2 seconds.

use escpos_rs::PrinterProfileBuilder;
let printer_profile = PrinterProfileBuilder::new_usb(0x0001, 0x0001)
    .with_timeout(std::time::Duration::from_secs(3)).unwrap()
    .build();
Source

pub fn build(self) -> PrinterProfile

Build the PrinterProfile that lies beneath the builder

let printer_profile = PrinterProfileBuilder::new_usb(0x0001, 0x0001).build();
Examples found in repository?
examples/basic.rs (line 4)
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 4)
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 4)
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}

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.