tauri_plugin_printer_v2/
lib.rs

1mod 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;
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/**
31 * 测试打印机连接
32 */
33#[tauri::command]
34async fn ping<R: Runtime>(app: tauri::AppHandle<R>, payload: PingRequest) -> Result<PingResponse> {
35    app.printer().ping(payload)
36}
37
38/**
39 * 打印 HTML 内容
40 */
41#[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
48/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the printer APIs.
49pub 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/**
60 * 创建临时文件
61 * @param buffer_data base64字符串
62 * @param filename 文件名
63 * @returns 临时文件路径
64 */
65#[tauri::command(rename_all = "snake_case")]
66// this will be accessible with `invoke('plugin:printer|create_temp_file')`.
67fn 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/**
80 * 删除临时文件
81 * @param filename 文件名
82 * @returns 删除结果
83 */
84#[tauri::command(rename_all = "snake_case")]
85// this will be accessible with `invoke('plugin:printer|create_temp_file')`.
86fn 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/**
96 * 获取打印机列表
97 */
98#[tauri::command]
99// this will be accessible with `invoke('plugin:printer|get_printers')`.
100fn get_printers() -> String {
101    if cfg!(windows) {
102        return windows::get_printers();
103    }
104
105    return "Unsupported OS".to_string();
106}
107
108/**
109 * 获取打印机列表
110 * @param printername 打印机名称
111 * @returns 打印机列表
112 */
113#[tauri::command(rename_all = "snake_case")]
114// this will be accessible with `invoke('plugin:printer|get_printer_by_name')`.
115fn 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/**
125 * 打印PDF
126 * @param id 打印机ID
127 * @param path PDF文件路径
128 * @param printer_setting 打印机设置
129 * @param remove_after_print 打印完成后删除文件
130 * @returns 打印结果
131 */
132#[tauri::command(rename_all = "snake_case")]    
133// this will be accessible with `invoke('plugin:printer|print_pdf')`.
134fn 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")]
157// this will be accessible with `invoke('plugin:printer|get_jobs')`.
158fn get_jobs(printername: String) -> String {
159    if cfg!(windows) {
160        return windows::get_jobs(printername);
161    }
162    return "Unsupported OS".to_string();
163}
164
165/**
166 * 获取打印任务列表
167 * @param printername 打印机名称
168 * @param jobid 打印任务ID
169 * @returns 打印任务列表
170 */
171#[tauri::command(rename_all = "snake_case")]
172// this will be accessible with `invoke('plugin:printer|get_jobs_by_id')`.
173fn get_jobs_by_id(printername: String, jobid: String) -> String {
174    if cfg!(windows) {
175        return windows::get_jobs_by_id(printername, jobid);
176    }
177    return "Unsupported OS".to_string();
178}
179
180/**
181 * 恢复打印任务
182 * @param printername 打印机名称
183 * @param jobid 打印任务ID
184 * @returns 恢复结果
185 */
186#[tauri::command(rename_all = "snake_case")]
187// this will be accessible with `invoke('plugin:printer|restart_job')`.
188fn resume_job(printername: String, jobid: String) -> String {
189    if cfg!(windows) {
190        return windows::resume_job(printername, jobid);
191    }
192    return "Unsupported OS".to_string();
193}
194
195/**
196 * 重启打印任务
197 * @param printername 打印机名称
198 * @param jobid 打印任务ID
199 * @returns 重启结果
200 */
201#[tauri::command(rename_all = "snake_case")]
202// this will be accessible with `invoke('plugin:printer|restart_job')`.
203fn restart_job(printername: String, jobid: String) -> String {
204    if cfg!(windows) {
205        return windows::restart_job(printername, jobid);
206    }
207    return "Unsupported OS".to_string();
208}
209
210/**
211 * 暂停打印任务
212 * @param printername 打印机名称
213 * @param jobid 打印任务ID
214 * @returns 暂停结果
215 */
216#[tauri::command(rename_all = "snake_case")]
217// this will be accessible with `invoke('plugin:printer|pause_job')`.
218fn pause_job(printername: String, jobid: String) -> String {
219    if cfg!(windows) {
220        return windows::pause_job(printername, jobid);
221    }
222    return "Unsupported OS".to_string();
223}
224
225/**
226 * 删除打印任务
227 * @param printername 打印机名称
228 * @param jobid 打印任务ID
229 * @returns 删除结果
230 */
231#[tauri::command(rename_all = "snake_case")]
232// this will be accessible with `invoke('plugin:printer|remove_job')`.
233fn remove_job(printername: String, jobid: String) -> String {
234    if cfg!(windows) {
235        return windows::remove_job(printername, jobid);
236    }
237    return "Unsupported OS".to_string();
238}
239
240/**
241 * 获取打印机列表
242 * @param printername 打印机名称
243 * @returns 打印机列表
244 */
245pub fn custom_get_printers_by_name(printername: String) -> String {
246    if cfg!(windows) {
247        return windows::get_printers_by_name(printername);
248    }
249
250    return "Unsupported OS".to_string();
251}
252
253/**
254 * 打印PDF
255 * @param id 打印机ID
256 * @param path PDF文件路径
257 * @param printer_setting 打印机设置
258 * @param remove_after_print 打印完成后删除文件
259 * @returns 打印结果
260 */
261pub fn custom_print_pdf(
262    id: String,
263    path: String,
264    printer: String,
265    print_settings: String,
266    remove_after_print: Option<bool>,
267) -> String {
268    if cfg!(windows) {
269        let options = declare::PrintOptions {
270            id,
271            path,
272            printer,
273            print_settings,
274            remove_after_print,
275        };
276        return windows::print_pdf(options);
277    }
278
279    return "Unsupported OS".to_string();
280}
281
282/**
283 * 初始化插件
284 * @returns 初始化结果
285 */
286/// Initializes the plugin.
287pub fn init<R: Runtime>() -> TauriPlugin<R> {
288  if cfg!(windows) {
289    windows::init_windows();
290  }
291    Builder::new("printer")
292        .invoke_handler(tauri::generate_handler![
293            ping,
294            print_html,
295            create_temp_file,
296            remove_temp_file,
297            get_printers,
298            get_printers_by_name,
299            print_pdf,
300            get_jobs,
301            get_jobs_by_id,
302            resume_job,
303            restart_job,
304            pause_job,
305            remove_job
306        ])
307        .setup(|app, api| {
308            #[cfg(mobile)]
309            let printer = mobile::init(app, api)?;
310            #[cfg(desktop)]
311            let printer = desktop::init(app, api)?;
312            app.manage(printer);
313            Ok(())
314        })
315        .build()
316}