tauri-plugin-thermal-printer 1.3.2

Plugin for Tauri to send esc/pos commands to thermal_printer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
use crate::commands_esc_pos::codes::barcode::{Barcode, BarcodeTextPosition, BarcodeType};
use crate::commands_esc_pos::codes::qr::{QRErrorCorrection, QRModel, QRSize, QR};
use crate::commands_esc_pos::control::printer_control::PrinterControl;
use crate::commands_esc_pos::image_escpos::{Image, ImageAlignment, ImageMode};
use crate::commands_esc_pos::text::encoder::TextEncoder;
use crate::commands_esc_pos::text::table;
use crate::commands_esc_pos::text::text_type::TextType;
use crate::models::print_job_request::PrintJobRequest;
use crate::models::print_sections::{Table, Text};
use crate::TestPrintRequest;

pub struct TestPrinter {
    print_job_context: TestPrintRequest,
}

impl TestPrinter {
    pub fn new() -> Self {
        Self {
            print_job_context: TestPrintRequest {
                printer_info: PrintJobRequest {
                    printer: "".to_string(),
                    sections: vec![],
                    options: Default::default(),
                    paper_size: crate::PaperSize::DEFAULT,
                },
                include_text: true,
                include_custom_text: false,
                custom_text: None,
                include_text_styles: true,
                include_alignment: true,
                include_columns: true,
                include_separators: true,
                include_barcode: true,
                include_barcode_types: false,
                include_qr: true,
                include_image: false,
                image_base64: None,
                test_all_fonts: true,
                test_invert: true,
                test_rotate: true,
                test_feed: true,
                test_cash_drawer: true,
                include_beep: true,
                cut_paper: true,
            },
        }
    }

    /// Genera el documento de prueba completo
    pub fn generate_test_document(
        &mut self,
        request: &TestPrintRequest,
    ) -> Result<Vec<u8>, String> {
        let mut document: Vec<u8> = Vec::new();

        self.print_job_context = request.clone();
        let encoder = TextEncoder::from_code_page(&request.printer_info.options.code_page);

        // Inicializar impresora y seleccionar página de código
        document.extend(PrinterControl::initialize());
        document.extend(request.printer_info.options.code_page.escpos_command());
        document.extend(PrinterControl::line_feed());

        // ==================== ENCABEZADO ====================
        if request.include_text {
            self.add_header(&mut document)?;
        }

        if request.include_custom_text {
            if let Some(custom_text) = request
                .custom_text
                .as_deref()
                .map(str::trim)
                .filter(|text| !text.is_empty())
            {
                self.add_custom_text_section(&mut document, custom_text, &encoder)?;
            }
        }

        if request.include_separators {
            self.add_double_line(&mut document);
        }

        // ==================== ESTILOS DE TEXTO ====================
        if request.include_text_styles {
            self.add_text_styles_section(&mut document)?;
        }

        // ==================== FUENTES ====================
        if request.test_all_fonts {
            self.add_fonts_section(&mut document)?;
        }

        // ==================== ALINEACIÓN ====================
        if request.include_alignment {
            self.add_alignment_section(&mut document)?;
        }

        // ==================== INVERSIÓN Y ROTACIÓN ====================
        if request.test_invert {
            self.add_invert_section(&mut document)?;
        }

        if request.test_rotate {
            self.add_rotate_section(&mut document)?;
        }

        // ==================== SEPARADORES ====================
        if request.include_separators {
            self.add_separators_section(&mut document)?;
        }

        // ==================== COLUMNAS ====================
        if request.include_columns {
            self.add_columns_section(&mut document, &encoder)?;
        }

        // ==================== CÓDIGOS DE BARRAS ====================
        if request.include_barcode {
            self.add_barcode_section(&mut document)?;
        }

        if request.include_barcode_types {
            self.add_barcode_types_section(&mut document)?;
        }

        // ==================== CÓDIGO QR ====================
        if request.include_qr {
            self.add_qr_section(&mut document)?;
        }

        // ==================== IMAGEN ====================
        if request.include_image {
            if let Some(ref image_base64) = request.image_base64 {
                self.add_image_section(&mut document, image_base64)?;
            }
        }

        // ==================== CONTROL DE IMPRESORA ====================
        if request.test_feed {
            self.add_feed_section(&mut document)?;
        }

        if request.test_cash_drawer {
            self.add_cash_drawer_section(&mut document)?;
        }

        // ==================== MENSAJE FINAL ====================
        self.add_footer(&mut document)?;

        // Beep al final si está habilitado
        if request.include_beep {
            document.extend(PrinterControl::beep_custom(3, 100));
        }

        // Avanzar papel antes de cortar
        let feed_lines = if request.cut_paper { 4 } else { 2 };
        document.extend(PrinterControl::feed_paper(feed_lines));

        // Cortar papel si está habilitado
        if request.cut_paper {
            document.extend(PrinterControl::cut_paper_with_feed(0, 4));
        } else {
            document.extend(PrinterControl::feed_paper(8));
        }

        Ok(document)
    }

    // ==================== SECCIONES INDIVIDUALES ====================

    fn add_header(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(TextType::AlignCenter.command());
        document.extend(TextType::DoubleSize.command());
        document.extend(b"PRUEBA DE IMPRESORA\n");
        document.extend(TextType::Normal.command());
        document.extend(TextType::AlignLeft.command());
        document.extend(b"\n");
        Ok(())
    }

    fn add_custom_text_section(
        &self,
        document: &mut Vec<u8>,
        custom_text: &str,
        encoder: &TextEncoder,
    ) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> TEXTO PERSONALIZADO <<<\n");
        document.extend(TextType::BoldOff.command());
        document.extend(encoder.encode_text(custom_text)?);
        document.extend(b"\n\n");
        Ok(())
    }

    fn add_text_styles_section(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> ESTILOS DE TEXTO <<<\n");
        document.extend(TextType::BoldOff.command());
        document.extend(b"\n");

        // Negrita
        document.extend(TextType::BoldOn.command());
        document.extend(b"1. Texto en Negrita\n");
        document.extend(TextType::BoldOff.command());

        // Subrayado
        document.extend(TextType::UnderlineOn.command());
        document.extend(b"2. Texto Subrayado\n");
        document.extend(TextType::UnderlineOff.command());

        // Doble altura
        document.extend(TextType::DoubleHeight.command());
        document.extend(b"3. Doble Altura\n");
        document.extend(TextType::Normal.command());

        // Doble ancho
        document.extend(TextType::DoubleWidth.command());
        document.extend(b"4. Doble Ancho\n");
        document.extend(TextType::Normal.command());

        // Doble tamaño
        document.extend(TextType::DoubleSize.command());
        document.extend(b"5. Doble Tamano\n");
        document.extend(TextType::Normal.command());

        document.extend(b"\n");
        Ok(())
    }

    fn add_fonts_section(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> TIPOS DE FUENTE <<<\n");
        document.extend(TextType::BoldOff.command());

        document.extend(TextType::FontA.command());
        document.extend(b"Fuente A (por defecto)\n");

        document.extend(TextType::FontB.command());
        document.extend(b"Fuente B (mas pequena)\n");

        document.extend(TextType::FontC.command());
        document.extend(b"Fuente C (condensada)\n");

        document.extend(TextType::FontA.command());
        document.extend(b"\n");
        Ok(())
    }

    fn add_alignment_section(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> ALINEACION <<<\n");
        document.extend(TextType::BoldOff.command());

        document.extend(TextType::AlignLeft.command());
        document.extend(b"Izquierda\n");

        document.extend(TextType::AlignCenter.command());
        document.extend(b"Centrado\n");

        document.extend(TextType::AlignRight.command());
        document.extend(b"Derecha\n");

        document.extend(TextType::AlignLeft.command());
        document.extend(b"\n");
        Ok(())
    }

    fn add_invert_section(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> INVERSION <<<\n");
        document.extend(TextType::BoldOff.command());

        document.extend(TextType::InvertOn.command());
        document.extend(b"Texto Invertido (fondo negro)\n");
        document.extend(TextType::InvertOff.command());
        document.extend(b"\n");
        Ok(())
    }

    fn add_rotate_section(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> ROTACION <<<\n");
        document.extend(TextType::BoldOff.command());

        document.extend(TextType::RotateOn.command());
        document.extend(b"Texto Rotado 90 grados\n");
        document.extend(TextType::RotateOff.command());
        document.extend(b"\n");
        Ok(())
    }

    fn add_separators_section(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> LINEAS SEPARADORAS <<<\n");
        document.extend(TextType::BoldOff.command());

        document.extend(b"Guiones:\n");
        self.add_dashed_line(document);

        document.extend(b"Igual:\n");
        self.add_double_line(document);

        document.extend(b"Asteriscos:\n");
        self.add_star_line(document);

        document.extend(b"\n");
        Ok(())
    }

    fn add_columns_section(
        &self,
        document: &mut Vec<u8>,
        encoder: &TextEncoder,
    ) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> TABLA CON COLUMNAS <<<\n");
        document.extend(TextType::BoldOff.command());

        let table = Table {
            columns: 3,
            truncate: true,
            column_widths: Some(vec![25, 8, 15]),
            header: Some(vec![
                Text {
                    text: "Producto".to_string(),
                    styles: None,
                },
                Text {
                    text: "Cant".to_string(),
                    styles: None,
                },
                Text {
                    text: "Precio".to_string(),
                    styles: None,
                },
            ]),
            body: vec![
                vec![
                    Text {
                        text: "Producto A".to_string(),
                        styles: None,
                    },
                    Text {
                        text: "2".to_string(),
                        styles: None,
                    },
                    Text {
                        text: "$10.50".to_string(),
                        styles: None,
                    },
                ],
                vec![
                    Text {
                        text: "Producto B".to_string(),
                        styles: None,
                    },
                    Text {
                        text: "1".to_string(),
                        styles: None,
                    },
                    Text {
                        text: "$25.00".to_string(),
                        styles: None,
                    },
                ],
                vec![
                    Text {
                        text: "Producto C Largo".to_string(),
                        styles: None,
                    },
                    Text {
                        text: "5".to_string(),
                        styles: None,
                    },
                    Text {
                        text: "$8.99".to_string(),
                        styles: None,
                    },
                ],
            ],
        };

        document.extend(table::process_table(
            &table,
            self.print_job_context
                .printer_info
                .paper_size
                .chars_per_line(),
            table.truncate,
            encoder,
        )?);
        self.add_dashed_line(document);

        // Totales
        let subtotal = self.create_receipt_line("SUBTOTAL:", "$44.49");
        document.extend(subtotal.as_bytes());
        document.extend(b"\n");

        let tax = self.create_receipt_line("IVA (16%):", "$7.12");
        document.extend(tax.as_bytes());
        document.extend(b"\n");

        self.add_double_line(document);

        let total = self.create_receipt_line("TOTAL:", "$51.61");
        document.extend(TextType::BoldOn.command());
        document.extend(TextType::DoubleHeight.command());
        document.extend(total.as_bytes());
        document.extend(b"\n");
        document.extend(TextType::Normal.command());

        document.extend(b"\n");
        Ok(())
    }

    fn add_barcode_section(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> CODIGOS DE BARRAS <<<\n");
        document.extend(TextType::BoldOff.command());
        document.extend(b"\n");

        document.extend(TextType::AlignLeft.command());
        document.extend(b"EAN13 Left:\n");
        let barcode = Barcode::new(BarcodeType::Ean13, "123456789012".to_string())
            .set_height(60)
            .set_width(2)
            .set_text_position(BarcodeTextPosition::Below);
        document.extend(barcode.get_command());

        document.extend(b"\n");

        document.extend(TextType::AlignCenter.command());
        document.extend(b"CODE128 Center:\n");

        let barcode = Barcode::new(BarcodeType::Code128, "123456789012".to_string())
            .set_height(60)
            .set_width(2)
            .set_text_position(BarcodeTextPosition::Below);
        document.extend(barcode.get_command());

        document.extend(b"\n");

        document.extend(TextType::AlignRight.command());
        document.extend(b"UPCA Right:\n");
        let barcode = Barcode::new(BarcodeType::UpcA, "123456789012".to_string())
            .set_height(60)
            .set_width(2)
            .set_text_position(BarcodeTextPosition::Below);
        document.extend(barcode.get_command());

        document.extend(b"\n\n");
        document.extend(TextType::AlignLeft.command());
        Ok(())
    }

    fn add_barcode_types_section(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(TextType::AlignCenter.command());

        // EAN13
        document.extend(b"EAN13:\n");
        let ean13 = Barcode::new(BarcodeType::Ean13, "1234567890128".to_string())
            .set_height(50)
            .set_width(2)
            .set_text_position(BarcodeTextPosition::Below);
        document.extend(ean13.get_command());
        document.extend(b"\n\n");

        // CODE39
        document.extend(b"CODE39:\n");
        let code39 = Barcode::new(BarcodeType::Code39, "ABC123".to_string())
            .set_height(50)
            .set_width(2)
            .set_text_position(BarcodeTextPosition::Below);
        document.extend(code39.get_command());
        document.extend(b"\n\n");

        document.extend(TextType::AlignLeft.command());
        Ok(())
    }

    fn add_qr_section(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> CODIGO QR <<<\n");
        document.extend(TextType::BoldOff.command());
        document.extend(b"\n");

        document.extend(TextType::AlignCenter.command());
        document.extend(b"Escanea el QR:\n");

        let qr = QR::new("https://github.com/luis3132/tauri-plugin-thermal-printer".to_string())
            .set_size(QRSize::Size6)
            .set_error_correction(QRErrorCorrection::M)
            .set_model(QRModel::Model2);
        document.extend(qr.get_command());

        document.extend(b"\n");
        document.extend(TextType::AlignLeft.command());
        Ok(())
    }

    fn add_image_section(&self, document: &mut Vec<u8>, image_base64: &str) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> IMAGEN <<<\n");
        document.extend(TextType::BoldOff.command());
        document.extend(b"\n");

        match Image::new(image_base64, 384) {
            Ok(img) => {
                let img = img
                    .set_alignment(ImageAlignment::Center)
                    .set_mode(ImageMode::Normal)
                    .set_use_dithering(true);

                match img.get_command() {
                    Ok(cmd) => {
                        document.extend(cmd);
                        document.extend(b"\n");
                    }
                    Err(_) => {
                        document.extend(b"Error al procesar imagen\n");
                    }
                }
            }
            Err(_) => {
                document.extend(b"Error al crear imagen\n");
            }
        }

        Ok(())
    }

    fn add_feed_section(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> CONTROL DE PAPEL <<<\n");
        document.extend(TextType::BoldOff.command());

        document.extend(b"Avance de 2 lineas...\n");
        document.extend(PrinterControl::feed_paper(2));

        document.extend(b"Avance de papel en puntos...\n");
        document.extend(PrinterControl::feed_paper_dots(50));
        document.extend(b"\n");
        Ok(())
    }

    fn add_cash_drawer_section(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(TextType::BoldOn.command());
        document.extend(b">>> CAJON DE DINERO <<<\n");
        document.extend(TextType::BoldOff.command());

        document.extend(b"Abriendo cajon (Pin 2)...\n");
        document.extend(PrinterControl::open_cash_drawer_pin2(100));
        document.extend(b"\n");
        Ok(())
    }

    fn add_footer(&self, document: &mut Vec<u8>) -> Result<(), String> {
        document.extend(b"\n");
        document.extend(TextType::AlignCenter.command());
        self.add_star_line(document);

        document.extend(TextType::DoubleHeight.command());
        document.extend(b"PRUEBA COMPLETADA\n");
        document.extend(TextType::Normal.command());

        self.add_star_line(document);

        let datetime = format_utc_datetime();
        document.extend(datetime.as_bytes());
        document.extend(b"\n");

        document.extend(TextType::AlignLeft.command());
        Ok(())
    }

    // ==================== UTILIDADES ====================

    fn add_dashed_line(&self, document: &mut Vec<u8>) {
        let line = "-".repeat(
            self.print_job_context
                .printer_info
                .paper_size
                .chars_per_line() as usize,
        );
        document.extend(line.as_bytes());
        document.extend(b"\n");
    }

    fn add_double_line(&self, document: &mut Vec<u8>) {
        let line = "=".repeat(
            self.print_job_context
                .printer_info
                .paper_size
                .chars_per_line() as usize,
        );
        document.extend(line.as_bytes());
        document.extend(b"\n");
    }

    fn add_star_line(&self, document: &mut Vec<u8>) {
        let line = "*".repeat(
            self.print_job_context
                .printer_info
                .paper_size
                .chars_per_line() as usize,
        );
        document.extend(line.as_bytes());
        document.extend(b"\n");
    }

    fn create_receipt_line(&self, label: &str, value: &str) -> String {
        let total_width = self
            .print_job_context
            .printer_info
            .paper_size
            .chars_per_line() as usize;
        let available = total_width.saturating_sub(label.len() + value.len());
        let dots = ".".repeat(available.max(1));
        format!("{}{}{}", label, dots, value)
    }
}

/// Formats the current UTC time as "dd/mm/yyyy HH:MM:SS" using only std::time.
/// Works on all platforms supported by Rust (Linux, macOS, Windows, Android, iOS).
fn format_utc_datetime() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};

    let total_secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);

    let time_of_day = total_secs % 86400;
    let days = total_secs / 86400;
    let hour = (time_of_day / 3600) as u32;
    let min = ((time_of_day % 3600) / 60) as u32;
    let sec = (time_of_day % 60) as u32;

    let (year, month, day) = days_to_ymd(days);
    format!(
        "{:02}/{:02}/{} {:02}:{:02}:{:02}",
        day, month, year, hour, min, sec
    )
}

/// Converts days since Unix epoch (1970-01-01) to (year, month, day) via the
/// Proleptic Gregorian calendar algorithm by Howard Hinnant (civil_from_days).
fn days_to_ymd(days: u64) -> (u32, u32, u32) {
    let z = days + 719468;
    let era = z / 146097;
    let doe = z % 146097;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = (yoe + era * 400) as u32;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = (doy - (153 * mp + 2) / 5 + 1) as u32;
    let m = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
    let y = if m <= 2 { y + 1 } else { y };
    (y, m, d)
}