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_setting: String,
138    remove_after_print: bool,
139) -> String {
140     
141    if cfg!(windows) {
142        let options = declare::PrintOptions { 
143            id,
144            path,
145            print_setting: printer_setting,
146            remove_after_print: remove_after_print,
147        };
148        return windows::print_pdf(options);
149    }
150
151    return "Unsupported OS".to_string();
152}
153
154#[tauri::command(rename_all = "snake_case")]
155// this will be accessible with `invoke('plugin:printer|get_jobs')`.
156fn get_jobs(printername: String) -> String {
157    if cfg!(windows) {
158        return windows::get_jobs(printername);
159    }
160    return "Unsupported OS".to_string();
161}
162
163/**
164 * 获取打印任务列表
165 * @param printername 打印机名称
166 * @param jobid 打印任务ID
167 * @returns 打印任务列表
168 */
169#[tauri::command(rename_all = "snake_case")]
170// this will be accessible with `invoke('plugin:printer|get_jobs_by_id')`.
171fn get_jobs_by_id(printername: String, jobid: String) -> String {
172    if cfg!(windows) {
173        return windows::get_jobs_by_id(printername, jobid);
174    }
175    return "Unsupported OS".to_string();
176}
177
178/**
179 * 恢复打印任务
180 * @param printername 打印机名称
181 * @param jobid 打印任务ID
182 * @returns 恢复结果
183 */
184#[tauri::command(rename_all = "snake_case")]
185// this will be accessible with `invoke('plugin:printer|restart_job')`.
186fn resume_job(printername: String, jobid: String) -> String {
187    if cfg!(windows) {
188        return windows::resume_job(printername, jobid);
189    }
190    return "Unsupported OS".to_string();
191}
192
193/**
194 * 重启打印任务
195 * @param printername 打印机名称
196 * @param jobid 打印任务ID
197 * @returns 重启结果
198 */
199#[tauri::command(rename_all = "snake_case")]
200// this will be accessible with `invoke('plugin:printer|restart_job')`.
201fn restart_job(printername: String, jobid: String) -> String {
202    if cfg!(windows) {
203        return windows::restart_job(printername, jobid);
204    }
205    return "Unsupported OS".to_string();
206}
207
208/**
209 * 暂停打印任务
210 * @param printername 打印机名称
211 * @param jobid 打印任务ID
212 * @returns 暂停结果
213 */
214#[tauri::command(rename_all = "snake_case")]
215// this will be accessible with `invoke('plugin:printer|pause_job')`.
216fn pause_job(printername: String, jobid: String) -> String {
217    if cfg!(windows) {
218        return windows::pause_job(printername, jobid);
219    }
220    return "Unsupported OS".to_string();
221}
222
223/**
224 * 删除打印任务
225 * @param printername 打印机名称
226 * @param jobid 打印任务ID
227 * @returns 删除结果
228 */
229#[tauri::command(rename_all = "snake_case")]
230// this will be accessible with `invoke('plugin:printer|remove_job')`.
231fn remove_job(printername: String, jobid: String) -> String {
232    if cfg!(windows) {
233        return windows::remove_job(printername, jobid);
234    }
235    return "Unsupported OS".to_string();
236}
237
238/**
239 * 获取打印机列表
240 * @param printername 打印机名称
241 * @returns 打印机列表
242 */
243pub fn custom_get_printers_by_name(printername: String) -> String {
244    if cfg!(windows) {
245        return windows::get_printers_by_name(printername);
246    }
247
248    return "Unsupported OS".to_string();
249}
250
251/**
252 * 打印PDF
253 * @param id 打印机ID
254 * @param path PDF文件路径
255 * @param printer_setting 打印机设置
256 * @param remove_after_print 打印完成后删除文件
257 * @returns 打印结果
258 */
259pub fn custom_print_pdf(
260    id: String,
261    path: String,
262    printer_setting: String,
263    remove_after_print: bool,
264) -> String {
265    if cfg!(windows) {
266        let options = declare::PrintOptions {
267            id,
268            path,
269            print_setting: printer_setting,
270            remove_after_print: remove_after_print,
271        };
272        return windows::print_pdf(options);
273    }
274
275    return "Unsupported OS".to_string();
276}
277
278/**
279 * 初始化插件
280 * @returns 初始化结果
281 */
282/// Initializes the plugin.
283pub fn init<R: Runtime>() -> TauriPlugin<R> {
284  if cfg!(windows) {
285    windows::init_windows();
286  }
287    Builder::new("printer")
288        .invoke_handler(tauri::generate_handler![
289            ping,
290            print_html,
291            create_temp_file,
292            remove_temp_file,
293            get_printers,
294            get_printers_by_name,
295            print_pdf,
296            get_jobs,
297            get_jobs_by_id,
298            resume_job,
299            restart_job,
300            pause_job,
301            remove_job
302        ])
303        .setup(|app, api| {
304            #[cfg(mobile)]
305            let printer = mobile::init(app, api)?;
306            #[cfg(desktop)]
307            let printer = desktop::init(app, api)?;
308            app.manage(printer);
309            Ok(())
310        })
311        .build()
312}