aboutsummaryrefslogtreecommitdiff
path: root/src/core/print/plugins/system.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/print/plugins/system.rs')
-rw-r--r--src/core/print/plugins/system.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/core/print/plugins/system.rs b/src/core/print/plugins/system.rs
new file mode 100644
index 0000000..7525a03
--- /dev/null
+++ b/src/core/print/plugins/system.rs
@@ -0,0 +1,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")
+ }
+}