pub struct PrinterProfileBuilder { /* private fields */ }
Expand description
Helper structure to create a PrinterProfile
Builder pattern for the PrinterProfile structure.
Implementations§
Source§impl PrinterProfileBuilder
impl PrinterProfileBuilder
Sourcepub fn new_usb(vendor_id: u16, product_id: u16) -> PrinterProfileBuilder
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.
Sourcepub fn new_terminal() -> PrinterProfileBuilder
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).
Sourcepub fn with_endpoint(self, endpoint: u8) -> Result<PrinterProfileBuilder, Error>
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();
Sourcepub fn with_width(self, width: u16) -> PrinterProfileBuilder
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();
Sourcepub fn with_font_width(self, font: Font, width: u8) -> PrinterProfileBuilder
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?
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
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}
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}
Sourcepub fn with_timeout(
self,
timeout: Duration,
) -> Result<PrinterProfileBuilder, Error>
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();
Sourcepub fn build(self) -> PrinterProfile
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?
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
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}
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§
impl Freeze for PrinterProfileBuilder
impl RefUnwindSafe for PrinterProfileBuilder
impl Send for PrinterProfileBuilder
impl Sync for PrinterProfileBuilder
impl Unpin for PrinterProfileBuilder
impl UnwindSafe for PrinterProfileBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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