tauri_plugin_printer_v2/
lib.rs1mod declare;
2mod fsys;
3mod windows;
4
5use tauri::{
6 plugin::{Builder, TauriPlugin},
7 Manager, Runtime,
8};
9
10use std::env;
11
12pub use crate::models::*;
13use crate::declare::{PrintHtmlOptions, PrintPdfUrlOptions};
14
15#[cfg(desktop)]
16mod desktop;
17#[cfg(mobile)]
18mod mobile;
19
20mod error;
21mod models;
22
23pub use error::{Error, Result};
24
25#[cfg(desktop)]
26use desktop::Printer;
27#[cfg(mobile)]
28use mobile::Printer;
29
30#[tauri::command]
34async fn ping<R: Runtime>(app: tauri::AppHandle<R>, payload: PingRequest) -> Result<PingResponse> {
35 app.printer().ping(payload)
36}
37
38#[tauri::command(rename_all = "snake_case")]
42async fn print_html<R: Runtime>(app: tauri::AppHandle<R>, options: PrintHtmlOptions) -> Result<String> {
43 println!("print_html: {:?}", options.print_settings);
44 app.printer().print_html(options)
45}
46
47
48pub trait PrinterExt<R: Runtime> {
50 fn printer(&self) -> &Printer<R>;
51}
52
53impl<R: Runtime, T: Manager<R>> crate::PrinterExt<R> for T {
54 fn printer(&self) -> &Printer<R> {
55 self.state::<Printer<R>>().inner()
56 }
57}
58
59#[tauri::command(rename_all = "snake_case")]
66fn create_temp_file(buffer_data: String, filename: String) -> String {
68 let dir = env::temp_dir();
69 let result = fsys::create_file_from_base64(
70 buffer_data.as_str(),
71 format!("{}{}", dir.display(), filename).as_str(),
72 );
73 if result.is_ok() {
74 return format!("{}{}", dir.display(), filename);
75 }
76 return "".to_owned();
77}
78
79#[tauri::command(rename_all = "snake_case")]
85fn remove_temp_file(filename: String) -> bool {
87 let dir = env::temp_dir();
88 let result = fsys::remove_file(format!("{}{}", dir.display(), filename).as_str());
89 if result.is_ok() {
90 return true;
91 }
92 return false;
93}
94
95#[tauri::command]
99fn get_printers() -> String {
101 if cfg!(windows) {
102 return windows::get_printers();
103 }
104
105 return "Unsupported OS".to_string();
106}
107
108#[tauri::command(rename_all = "snake_case")]
114fn get_printers_by_name(printername: String) -> String {
116 println!("获取打印机列表: {}", printername);
117 if cfg!(windows) {
118 return windows::get_printers_by_name(printername);
119 }
120
121 return "Unsupported OS".to_string();
122}
123
124#[tauri::command(rename_all = "snake_case")]
133fn print_pdf(
135 id: String,
136 path: String,
137 printer: String,
138 print_settings: String,
139 remove_after_print: Option<bool>,
140) -> String {
141
142 if cfg!(windows) {
143 let options = declare::PrintOptions {
144 id,
145 path,
146 printer,
147 print_settings,
148 remove_after_print,
149 };
150 return windows::print_pdf(options);
151 }
152
153 return "Unsupported OS".to_string();
154}
155
156#[tauri::command(rename_all = "snake_case")]
168fn print_pdf_from_url(
170 id: String,
171 url: String,
172 printer: String,
173 print_settings: String,
174 remove_after_print: Option<bool>,
175 timeout_seconds: Option<u64>,
176 temp_dir: Option<String>,
177) -> String {
178
179 if cfg!(windows) {
180 let options = PrintPdfUrlOptions {
181 id,
182 url,
183 printer,
184 print_settings,
185 remove_after_print,
186 timeout_seconds,
187 temp_dir,
188 };
189 return windows::print_pdf_from_url(options);
190 }
191
192 return "Unsupported OS".to_string();
193}
194
195#[tauri::command(rename_all = "snake_case")]
196fn get_jobs(printername: String) -> String {
198 if cfg!(windows) {
199 return windows::get_jobs(printername);
200 }
201 return "Unsupported OS".to_string();
202}
203
204#[tauri::command(rename_all = "snake_case")]
211fn get_jobs_by_id(printername: String, jobid: String) -> String {
213 if cfg!(windows) {
214 return windows::get_jobs_by_id(printername, jobid);
215 }
216 return "Unsupported OS".to_string();
217}
218
219#[tauri::command(rename_all = "snake_case")]
226fn resume_job(printername: String, jobid: String) -> String {
228 if cfg!(windows) {
229 return windows::resume_job(printername, jobid);
230 }
231 return "Unsupported OS".to_string();
232}
233
234#[tauri::command(rename_all = "snake_case")]
241fn restart_job(printername: String, jobid: String) -> String {
243 if cfg!(windows) {
244 return windows::restart_job(printername, jobid);
245 }
246 return "Unsupported OS".to_string();
247}
248
249#[tauri::command(rename_all = "snake_case")]
256fn pause_job(printername: String, jobid: String) -> String {
258 if cfg!(windows) {
259 return windows::pause_job(printername, jobid);
260 }
261 return "Unsupported OS".to_string();
262}
263
264#[tauri::command(rename_all = "snake_case")]
271fn remove_job(printername: String, jobid: String) -> String {
273 if cfg!(windows) {
274 return windows::remove_job(printername, jobid);
275 }
276 return "Unsupported OS".to_string();
277}
278
279pub fn custom_get_printers_by_name(printername: String) -> String {
285 if cfg!(windows) {
286 return windows::get_printers_by_name(printername);
287 }
288
289 return "Unsupported OS".to_string();
290}
291
292pub fn custom_print_pdf(
301 id: String,
302 path: String,
303 printer: String,
304 print_settings: String,
305 remove_after_print: Option<bool>,
306) -> String {
307 if cfg!(windows) {
308 let options = declare::PrintOptions {
309 id,
310 path,
311 printer,
312 print_settings,
313 remove_after_print,
314 };
315 return windows::print_pdf(options);
316 }
317
318 return "Unsupported OS".to_string();
319}
320
321pub fn init<R: Runtime>() -> TauriPlugin<R> {
327 if cfg!(windows) {
328 windows::init_windows();
329 }
330 Builder::new("printer")
331 .invoke_handler(tauri::generate_handler![
332 ping,
333 print_html,
334 create_temp_file,
335 remove_temp_file,
336 get_printers,
337 get_printers_by_name,
338 print_pdf,
339 print_pdf_from_url,
340 get_jobs,
341 get_jobs_by_id,
342 resume_job,
343 restart_job,
344 pause_job,
345 remove_job
346 ])
347 .setup(|app, api| {
348 #[cfg(mobile)]
349 let printer = mobile::init(app, api)?;
350 #[cfg(desktop)]
351 let printer = desktop::init(app, api)?;
352 app.manage(printer);
353 Ok(())
354 })
355 .build()
356}