diff options
Diffstat (limited to 'src/core/print/plugins')
| -rw-r--r-- | src/core/print/plugins/mod.rs | 2 | ||||
| -rw-r--r-- | src/core/print/plugins/pdf.rs | 27 | ||||
| -rw-r--r-- | src/core/print/plugins/system.rs | 49 |
3 files changed, 78 insertions, 0 deletions
diff --git a/src/core/print/plugins/mod.rs b/src/core/print/plugins/mod.rs new file mode 100644 index 0000000..8decf3b --- /dev/null +++ b/src/core/print/plugins/mod.rs @@ -0,0 +1,2 @@ +pub mod pdf; +pub mod system; diff --git a/src/core/print/plugins/pdf.rs b/src/core/print/plugins/pdf.rs new file mode 100644 index 0000000..2456edb --- /dev/null +++ b/src/core/print/plugins/pdf.rs @@ -0,0 +1,27 @@ +use anyhow::{Context, Result}; +use printpdf::PdfDocumentReference; +use std::fs::File; +use std::io::BufWriter; +use std::path::PathBuf; + +pub struct PdfPlugin; + +impl PdfPlugin { + pub fn new() -> Self { + Self + } + + pub fn export_pdf(&self, doc: PdfDocumentReference, path: &PathBuf) -> Result<()> { + let file = File::create(path).context("Failed to create PDF file for export")?; + let mut writer = BufWriter::new(file); + doc.save(&mut writer) + .context("Failed to save PDF to specified path")?; + Ok(()) + } +} + +impl Default for PdfPlugin { + fn default() -> Self { + Self::new() + } +} 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") + } +} |
