escpos_rust/
lib.rs

1//! Library for controlling esc/pos printers with rust
2//!
3//! Not ready for production (yet, but soon!).
4//!
5//! ```rust,no_run
6//! use escpos_rs::{Printer, PrinterProfile};
7//!
8//! // We create a usb contest for the printer
9//! let printer_profile = PrinterProfile::usb_builder(0x0001, 0x0001).build();
10//! // We pass it to the printer
11//! let printer = match Printer::new(printer_profile) {
12//!     Ok(maybe_printer) => match maybe_printer {
13//!         Some(printer) => printer,
14//!         None => panic!("No printer was found :(")
15//!     },
16//!     Err(e) => panic!("Error: {}", e)
17//! };
18//! // We print simple text
19//! match printer.println("Hello, world!") {
20//!     Ok(_) => (),
21//!     Err(e) => println!("Error: {}", e)
22//! }
23//! ```
24//! 
25//! See the [Printer](crate::Printer) structure to see the rest of the implemented functions for interacting with the thermal printer (raw printing, images, etc.).
26//!
27//! ## Printer Profile
28//!
29//! In order to print, some data about the printer must be known. The [PrinterProfile](crate::PrinterProfile) structure fulfills this purpose.
30//!
31//! The strict minimum information needed to print, are the vendor id, the product id. Both vendor and product id should be found in the maker's website, or sometimes they get printed in test prints (which usually occur if you hold the feed button on the printer).
32//!
33//! If you are running linux, then one way to get these values is by executing the `lsusb` command.
34//!
35//! ### Instructions
36//!
37//! Because of the usual applications for thermal printers, the [Instruction](crate::Instruction) structure has been implemented, which allows you to define a sort of __template__, that you can use to print multiple documents with __certain__ data customized for each print.
38//!
39//! ```rust,no_run
40//! use escpos_rs::{
41//!     Printer, PrintData, PrinterProfile,
42//!     Instruction, Justification, command::Font
43//! };
44//! 
45//! // Printer details...
46//! let printer_profile = PrinterProfile::usb_builder(0x0001, 0x0001)
47//!     .with_font_width(Font::FontA, 32)
48//!     .build();
49//! // We pass it to the printer
50//! let printer = match Printer::new(printer_profile) {
51//!     Ok(maybe_printer) => match maybe_printer {
52//!         Some(printer) => printer,
53//!         None => panic!("No printer was found :(")
54//!     },
55//!     Err(e) => panic!("Error: {}", e)
56//! };
57//! // We create a simple instruction with a single substitution
58//! let instruction = Instruction::text(
59//!     "Hello, %name%!",
60//!     Font::FontA,
61//!     Justification::Center,
62//!     // Words that will be replaced in this specific instruction
63//!     Some(vec!["%name%".into()].into_iter().collect())
64//! );
65//! // We create custom information for the instruction
66//! let print_data_1 = PrintData::builder()
67//!     .replacement("%name%", "Carlos")
68//!     .build();
69//! // And a second set...
70//! let print_data_2 = PrintData::builder()
71//!     .replacement("%name%", "John")
72//!     .build();
73//! // We send the instruction to the printer, along with the custom data
74//! // for this particular print
75//! match printer.instruction(&instruction, Some(&print_data_1)) {
76//!     Ok(_) => (), // "Hello, Carlos!" should've been printed.
77//!     Err(e) => println!("Error: {}", e)
78//! }
79//! // Now we print the second data
80//! match printer.instruction(&instruction, Some(&print_data_2)) {
81//!     Ok(_) => (), // "Hello, John!" should've been printed.
82//!     Err(e) => println!("Error: {}", e)
83//! }
84//! ```
85//!
86//! This structure implements both Serialize, and Deserialize from [serde](https://docs.rs/serde), so it is possible to store these instructions to recover them from memory. You can serialize to a json, as pictures are encoded to base64 first to be utf-8 compatible.
87
88pub use printer::{Printer, PrinterProfile, PrinterProfileBuilder, PrinterModel, PrinterConnectionData};
89pub use instruction::{Instruction, Justification, PrintData, PrintDataBuilder, EscposImage};
90pub use error::{Error};
91pub use formatter::{Formatter, TableOptions};
92
93/// Contains raw esc/pos commands
94pub mod command;
95
96mod printer;
97mod instruction;
98mod error;
99mod formatter;
100mod utils;