| 1 | //! Render a .pos file as TSPL labels without a printer. |
| 2 | //! |
| 3 | //! cargo run --example tspl_preview -- file.pos out-prefix [width_mm height_mm gap_mm dpi] |
| 4 | //! Writes out-prefix-N.png (one per label page) and out-prefix.prn (TSPL job). |
| 5 | |
| 6 | use std::collections::HashMap; |
| 7 | use std::path::Path; |
| 8 | |
| 9 | use posprint::io::PosFile; |
| 10 | use posprint::label_format::LabelFormat; |
| 11 | use posprint::pipeline::{encode_pos_file, encode_pos_file_tspl}; |
| 12 | use posprint::tspl::{page_scale, render_escpos, TsplOptions}; |
| 13 | |
| 14 | fn main() { |
| 15 | let args: Vec<String> = std::env::args().collect(); |
| 16 | if args.len() < 3 { |
| 17 | eprintln!("usage: tspl_preview file.pos out-prefix [width_mm height_mm gap_mm dpi]"); |
| 18 | std::process::exit(2); |
| 19 | } |
| 20 | let path = Path::new(&args[1]); |
| 21 | let file = PosFile::load_from_path(path).expect("load"); |
| 22 | let num = |i: usize, d: f32| args.get(i).and_then(|v| v.parse().ok()).unwrap_or(d); |
| 23 | let format = LabelFormat { |
| 24 | width_mm: num(3, 100.0), |
| 25 | height_mm: num(4, 50.0), |
| 26 | gap_mm: num(5, 3.0), |
| 27 | dpi: num(6, 300.0) as u16, |
| 28 | ..Default::default() |
| 29 | }; |
| 30 | let params: HashMap<String, String> = std::env::var("POSPRINT_PARAMS") |
| 31 | .ok() |
| 32 | .and_then(|p| serde_json::from_str(&std::fs::read_to_string(p).ok()?).ok()) |
| 33 | .unwrap_or_default(); |
| 34 | let esc = encode_pos_file(&file, path.parent(), ¶ms).expect("encode"); |
| 35 | let src_w = file.metadata.printable_dots() as u32; |
| 36 | for (i, page) in render_escpos(&esc, src_w, page_scale(src_w, &format)).iter().enumerate() { |
| 37 | page.save(format!("{}-{}.png", args[2], i + 1)).expect("png"); |
| 38 | } |
| 39 | let job = encode_pos_file_tspl(&file, path.parent(), ¶ms, &format, TsplOptions::default()) |
| 40 | .expect("tspl"); |
| 41 | std::fs::write(format!("{}.prn", args[2]), job).expect("prn"); |
| 42 | } |