Skip to main content

tauri_plugin_printer/
desktop.rs

1use std::{
2    marker::PhantomData,
3    path::{Path, PathBuf},
4};
5
6use serde::de::DeserializeOwned;
7use tauri::{plugin::PluginApi, AppHandle, Runtime};
8
9use crate::{PrintJob, PrintResult, PrinterCapabilities, PrinterItem, Result};
10
11#[cfg(target_os = "windows")]
12macro_rules! platform {
13    ($method:ident($($argument:expr),* $(,)?)) => {
14        crate::desktopapp::windows::$method($($argument),*)
15    };
16}
17
18#[cfg(not(target_os = "windows"))]
19macro_rules! platform {
20    ($method:ident($($argument:expr),* $(,)?)) => {{
21        $(let _ = &$argument;)*
22        Err(crate::Error::UnsupportedPlatform(std::env::consts::OS.into()))
23    }};
24}
25
26pub fn init<R: Runtime, C: DeserializeOwned>(
27    _app: &AppHandle<R>,
28    _api: PluginApi<R, C>,
29) -> Result<Printer<R>> {
30    #[cfg(target_os = "windows")]
31    {
32        Ok(Printer {
33            executable: crate::desktopapp::windows::init()?,
34            runtime: PhantomData,
35        })
36    }
37
38    #[cfg(not(target_os = "windows"))]
39    Err(crate::Error::UnsupportedPlatform(
40        std::env::consts::OS.into(),
41    ))
42}
43
44/// Rust-side access to the printer APIs.
45pub struct Printer<R: Runtime> {
46    executable: PathBuf,
47    runtime: PhantomData<fn() -> R>,
48}
49
50impl<R: Runtime> Printer<R> {
51    pub fn get_printers(&self) -> Result<Vec<PrinterItem>> {
52        platform!(get_printers())
53    }
54
55    pub fn get_printer(&self, id: &str) -> Result<PrinterItem> {
56        platform!(get_printer(id))
57    }
58
59    pub fn get_printer_by_name(&self, name: &str) -> Result<PrinterItem> {
60        platform!(get_printer_by_name(name))
61    }
62
63    pub fn get_default_printer(&self) -> Result<Option<PrinterItem>> {
64        platform!(get_default_printer())
65    }
66
67    pub fn set_default_printer(&self, name: &str) -> Result<PrinterItem> {
68        platform!(set_default_printer(name))
69    }
70
71    pub fn get_printer_capabilities(&self, name: &str) -> Result<PrinterCapabilities> {
72        platform!(get_printer_capabilities(name))
73    }
74
75    pub fn get_jobs(&self, printer_name: &str) -> Result<Vec<PrintJob>> {
76        platform!(get_jobs(printer_name))
77    }
78
79    pub fn get_job(&self, printer_name: &str, job_id: i32) -> Result<PrintJob> {
80        platform!(get_job(printer_name, job_id))
81    }
82
83    pub fn resume_job(&self, printer_name: &str, job_id: i32) -> Result<PrintResult> {
84        platform!(resume_job(printer_name, job_id))
85    }
86
87    pub fn restart_job(&self, printer_name: &str, job_id: i32) -> Result<PrintResult> {
88        platform!(restart_job(printer_name, job_id))
89    }
90
91    pub fn pause_job(&self, printer_name: &str, job_id: i32) -> Result<PrintResult> {
92        platform!(pause_job(printer_name, job_id))
93    }
94
95    pub fn remove_job(&self, printer_name: &str, job_id: i32) -> Result<PrintResult> {
96        platform!(remove_job(printer_name, job_id))
97    }
98
99    pub fn print_pdf(
100        &self,
101        printer_name: Option<&str>,
102        path: &Path,
103        settings: &str,
104        remove_after_print: bool,
105    ) -> Result<PrintResult> {
106        platform!(print_pdf(
107            &self.executable,
108            printer_name,
109            path,
110            settings,
111            remove_after_print
112        ))
113    }
114
115    pub fn print_pdf_data(
116        &self,
117        printer_name: Option<&str>,
118        data: &str,
119        settings: &str,
120    ) -> Result<PrintResult> {
121        platform!(print_pdf_data(
122            &self.executable,
123            printer_name,
124            data,
125            settings
126        ))
127    }
128}