blob: 7525a03200e0953aac3582476545a3735f5aad1c (
plain)
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
|
use anyhow::{Context, Result};
use printpdf::PdfDocumentReference;
use std::fs::File;
use std::io::BufWriter;
use std::path::PathBuf;
pub struct SystemPrintPlugin {
temp_dir: PathBuf,
}
impl SystemPrintPlugin {
pub fn new() -> Result<Self> {
let temp_dir = std::env::temp_dir().join("beepzone_labels");
std::fs::create_dir_all(&temp_dir)?;
Ok(Self { temp_dir })
}
#[allow(dead_code)]
pub fn print_label(&self, doc: PdfDocumentReference) -> Result<()> {
let pdf_path = self.save_pdf_to_temp(doc)?;
log::info!("Generated temporary PDF at: {:?}", pdf_path);
self.open_print_dialog(&pdf_path)?;
Ok(())
}
pub fn save_pdf_to_temp(&self, doc: PdfDocumentReference) -> Result<PathBuf> {
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();
let pdf_path = self.temp_dir.join(format!("label_{}.pdf", timestamp));
let file = File::create(&pdf_path).context("Failed to create temp PDF file")?;
let mut writer = BufWriter::new(file);
doc.save(&mut writer).context("Failed to save temp PDF")?;
Ok(pdf_path)
}
pub fn open_print_dialog(&self, pdf_path: &PathBuf) -> Result<()> {
open::that(pdf_path).context("Failed to open PDF with system default application")?;
log::info!("PDF opened successfully. User can print from the PDF viewer.");
Ok(())
}
}
impl Default for SystemPrintPlugin {
fn default() -> Self {
Self::new().expect("Failed to initialize SystemPrintPlugin")
}
}
|