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
impl Printer
Sourcepub fn new(printer_profile: PrinterProfile) -> Result<Option<Printer>, Error>
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?
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_context_feeling_lucky() -> Result<Option<Printer>, Error>
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.
Sourcepub fn instruction(
&self,
instruction: &Instruction,
print_data: Option<&PrintData>,
) -> Result<(), Error>
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.
Sourcepub fn print<T: Into<String>>(&self, content: T) -> Result<(), Error>
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.
Sourcepub fn print_gb18030(&self, content: String) -> Result<(), Error>
pub fn print_gb18030(&self, content: String) -> Result<(), Error>
Print gb18030 text.
Use encoding and send raw data
Sourcepub fn println<T: Into<String>>(&self, content: T) -> Result<(), Error>
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?
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}
Sourcepub fn print_barcode(
&self,
content: String,
position: BarcodePostion,
) -> Result<(), Error>
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.
pub fn set_barcode_position( &self, position: BarcodePostion, ) -> Result<(), Error>
Sourcepub fn set_font(&mut self, font: Font) -> Result<(), Error>
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.
Sourcepub fn set_space_split(&mut self, state: bool)
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?
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
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 jump(&self, n: u8) -> Result<(), Error>
pub fn jump(&self, n: u8) -> Result<(), Error>
Jumps n number of lines (to leave whitespaces). Basically n * '\n'
passed to print
Sourcepub fn cut(&self) -> Result<(), Error>
pub fn cut(&self) -> Result<(), Error>
Cuts the paper, in case the instruction is supported by the printer
Sourcepub 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>
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>
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 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 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>
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?
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}
pub fn image(&self, escpos_image: EscposImage) -> Result<(), Error>
Sourcepub fn raw<A: AsRef<[u8]>>(&self, bytes: A) -> Result<(), Error>
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§
impl !Freeze for Printer
impl RefUnwindSafe for Printer
impl Send for Printer
impl Sync for Printer
impl Unpin for Printer
impl UnwindSafe for Printer
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