tauri_plugin_printer_sujin999/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
mod windows;
mod declare;
mod fsys;
use tauri::{
  plugin::{Builder, TauriPlugin},
  Runtime,
};

use std::env;

#[tauri::command(rename_all = "snake_case")]
// this will be accessible with `invoke('plugin:printer|create_temp_file')`.
fn create_temp_file(buffer_data: String, filename: String) -> String {
    let dir = env::temp_dir();
    let result = fsys::create_file_from_base64(buffer_data.as_str(), format!("{}{}", dir.display(),filename).as_str());
    if result.is_ok() {
      return format!("{}{}", dir.display(),filename);
    }
  return "".to_owned()
}


#[tauri::command(rename_all = "snake_case")]
// this will be accessible with `invoke('plugin:printer|create_temp_file')`.
fn remove_temp_file(filename: String) -> bool {
    let dir = env::temp_dir();
    let result = fsys::remove_file(format!("{}{}", dir.display(),filename).as_str());
    if result.is_ok() {
      return true;
    }
  return false
}

#[tauri::command]
// this will be accessible with `invoke('plugin:printer|get_printers')`.
fn get_printers() -> String {
  if cfg!(windows) {
      return windows::get_printers();
  }

  return "Unsupported OS".to_string();
}

#[tauri::command(rename_all = "snake_case")]
// this will be accessible with `invoke('plugin:printer|get_printer_by_name')`.
fn get_printers_by_name(printername: String) -> String {
  if cfg!(windows) {
      return windows::get_printers_by_name(printername);
  }

  return "Unsupported OS".to_string();
}

#[tauri::command(rename_all = "snake_case")]
// this will be accessible with `invoke('plugin:printer|print_pdf')`.
fn print_pdf(
    id: String,
    path: String, 
    printer_setting: String,
    remove_after_print: bool
) -> String {
  if cfg!(windows) {
    let options = declare::PrintOptions{
        id,
        path,
        print_setting: printer_setting,
        remove_after_print: remove_after_print
    };
    return windows::print_pdf(options);
  }

  return "Unsupported OS".to_string();
}


#[tauri::command(rename_all = "snake_case")]
// this will be accessible with `invoke('plugin:printer|get_jobs')`.
fn get_jobs(printername: String) -> String {
  if cfg!(windows) {
    return windows::get_jobs(printername);
  }
  return "Unsupported OS".to_string();
}

#[tauri::command(rename_all = "snake_case")]
// this will be accessible with `invoke('plugin:printer|get_jobs_by_id')`.
fn get_jobs_by_id(printername: String, jobid: String) -> String {
  if cfg!(windows) {
    return windows::get_jobs_by_id(printername, jobid);
  }
  return "Unsupported OS".to_string();
}

#[tauri::command(rename_all = "snake_case")]
// this will be accessible with `invoke('plugin:printer|restart_job')`.
fn resume_job(printername: String, jobid: String) -> String {
  if cfg!(windows) {
    return windows::resume_job(printername,jobid);
  }
  return "Unsupported OS".to_string();
}

#[tauri::command(rename_all = "snake_case")]
// this will be accessible with `invoke('plugin:printer|restart_job')`.
fn restart_job(printername: String, jobid: String) -> String {
  if cfg!(windows) {
    return windows::restart_job(printername,jobid);
  }
  return "Unsupported OS".to_string();
}

#[tauri::command(rename_all = "snake_case")]
// this will be accessible with `invoke('plugin:printer|pause_job')`.
fn pause_job(printername: String, jobid: String) -> String {
  if cfg!(windows) {
    return windows::pause_job(printername, jobid);
  }
  return "Unsupported OS".to_string();
}

#[tauri::command(rename_all = "snake_case")]
// this will be accessible with `invoke('plugin:printer|remove_job')`.
fn remove_job(printername: String, jobid: String) -> String {
  if cfg!(windows) {
    return windows::remove_job(printername, jobid);
  }
  return "Unsupported OS".to_string();
}

pub fn init<R: Runtime>() -> TauriPlugin<R> {
  if cfg!(windows) {
    windows::init_windows();
  }
  Builder::new("printer")
    .invoke_handler(tauri::generate_handler![
      create_temp_file,
      remove_temp_file,
      get_printers, 
      get_printers_by_name,
      print_pdf,
      get_jobs,
      get_jobs_by_id,
      resume_job,
      restart_job,
      pause_job,
      remove_job
    ])
    .build()
}