Skip to content

Commit

Permalink
Fix 1 for macOS Raw printing script
Browse files Browse the repository at this point in the history
  • Loading branch information
GianniVullo committed Aug 23, 2024
1 parent 6940d39 commit c64fcbb
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 38 deletions.
19 changes: 19 additions & 0 deletions src-tauri/capabilities/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@
"sql:allow-close",
"sql:allow-execute",
"shell:allow-open",
{
"identifier": "shell:allow-execute",
"allow": [
{
"name": "lp",
"cmd": "lp",
"args": [
"-d",
{
"validator": "^[a-zA-Z0-9_\\-]{3,20}$"
},
{
"validator": "^/.+?/temp_print_file\\.prn$"
}
],
"sidecar": false
}
]
},
"dialog:allow-open",
"os:allow-platform",
"http:allow-fetch",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/gen/schemas/capabilities.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"default-plugins":{"identifier":"default-plugins","description":"enables the default permissions","local":true,"windows":["main"],"permissions":["core:path:default","core:event:default","core:window:default","core:webview:default","core:app:default","core:resources:default","core:menu:default","core:tray:default","sql:allow-load","sql:allow-select","sql:allow-close","sql:allow-execute","shell:allow-open","dialog:allow-open","os:allow-platform","http:allow-fetch",{"identifier":"http:default","allow":[{"url":"https://epzrdxofotzufykimwuc.supabase.co/functions/v1/update-checker/pOwtY_2gz3GHtISkVIv6Pg"},{"url":"https://admin-console.kine-helper.be/api/*"}]},"fs:allow-applocaldata-read","fs:allow-applocaldata-read-recursive","fs:allow-applocaldata-write","fs:allow-applocaldata-write-recursive","fs:allow-applocaldata-meta","fs:allow-applocaldata-meta-recursive","fs:allow-remove","fs:allow-create","fs:allow-exists","fs:allow-app-write","fs:allow-app-write-recursive","fs:allow-appcache-write","fs:allow-appcache-write-recursive","fs:allow-appconfig-write","fs:allow-app-read","fs:allow-app-read-recursive","fs:allow-appcache-read","fs:allow-appcache-read-recursive","fs:allow-appconfig-read","updater:allow-check","updater:default","dialog:allow-save","process:allow-restart"],"platforms":["macOS","linux","android","iOS"]},"default-plugins-windows":{"identifier":"default-plugins-windows","description":"add the fs dir document permissions for windows","local":true,"windows":["main"],"permissions":["core:path:default","core:event:default","core:window:default","core:webview:default","core:app:default","core:resources:default","core:menu:default","core:tray:default","core:path:allow-resolve-directory","sql:allow-load","sql:allow-select","sql:allow-close","sql:allow-execute","shell:allow-open","dialog:allow-open","os:allow-platform","http:allow-fetch",{"identifier":"http:default","allow":[{"url":"https://epzrdxofotzufykimwuc.supabase.co/functions/v1/update-checker/pOwtY_2gz3GHtISkVIv6Pg"},{"url":"https://admin-console.kine-helper.be/api/*"}]},"fs:allow-applocaldata-read","fs:allow-applocaldata-read-recursive","fs:allow-applocaldata-write","fs:allow-applocaldata-write-recursive","fs:allow-applocaldata-meta","fs:allow-applocaldata-meta-recursive","fs:allow-document-read","fs:allow-document-read-recursive","fs:allow-document-write","fs:allow-document-write-recursive","fs:allow-document-meta","fs:allow-document-meta-recursive","fs:allow-remove","fs:allow-create","fs:allow-exists","fs:allow-app-write","fs:allow-app-write-recursive","fs:allow-appcache-write","fs:allow-appcache-write-recursive","fs:allow-appconfig-write","fs:allow-app-read","fs:allow-app-read-recursive","fs:allow-appcache-read","fs:allow-appcache-read-recursive","fs:allow-appconfig-read","updater:allow-check","updater:default","dialog:allow-save","process:allow-restart"],"platforms":["windows"]}}
{"default-plugins":{"identifier":"default-plugins","description":"enables the default permissions","local":true,"windows":["main"],"permissions":["core:path:default","core:event:default","core:window:default","core:webview:default","core:app:default","core:resources:default","core:menu:default","core:tray:default","sql:allow-load","sql:allow-select","sql:allow-close","sql:allow-execute","shell:allow-open",{"identifier":"shell:allow-execute","allow":[{"args":["-d",{"validator":"^[a-zA-Z0-9_\\-]{3,20}$"},{"validator":"^/.+?/temp_print_file\\.prn$"}],"cmd":"lp","name":"lp","sidecar":false}]},"dialog:allow-open","os:allow-platform","http:allow-fetch",{"identifier":"http:default","allow":[{"url":"https://epzrdxofotzufykimwuc.supabase.co/functions/v1/update-checker/pOwtY_2gz3GHtISkVIv6Pg"},{"url":"https://admin-console.kine-helper.be/api/*"}]},"fs:allow-applocaldata-read","fs:allow-applocaldata-read-recursive","fs:allow-applocaldata-write","fs:allow-applocaldata-write-recursive","fs:allow-applocaldata-meta","fs:allow-applocaldata-meta-recursive","fs:allow-remove","fs:allow-create","fs:allow-exists","fs:allow-app-write","fs:allow-app-write-recursive","fs:allow-appcache-write","fs:allow-appcache-write-recursive","fs:allow-appconfig-write","fs:allow-app-read","fs:allow-app-read-recursive","fs:allow-appcache-read","fs:allow-appcache-read-recursive","fs:allow-appconfig-read","updater:allow-check","updater:default","dialog:allow-save","process:allow-restart"],"platforms":["macOS","linux","android","iOS"]},"default-plugins-windows":{"identifier":"default-plugins-windows","description":"add the fs dir document permissions for windows","local":true,"windows":["main"],"permissions":["core:path:default","core:event:default","core:window:default","core:webview:default","core:app:default","core:resources:default","core:menu:default","core:tray:default","core:path:allow-resolve-directory","sql:allow-load","sql:allow-select","sql:allow-close","sql:allow-execute","shell:allow-open","dialog:allow-open","os:allow-platform","http:allow-fetch",{"identifier":"http:default","allow":[{"url":"https://epzrdxofotzufykimwuc.supabase.co/functions/v1/update-checker/pOwtY_2gz3GHtISkVIv6Pg"},{"url":"https://admin-console.kine-helper.be/api/*"}]},"fs:allow-applocaldata-read","fs:allow-applocaldata-read-recursive","fs:allow-applocaldata-write","fs:allow-applocaldata-write-recursive","fs:allow-applocaldata-meta","fs:allow-applocaldata-meta-recursive","fs:allow-document-read","fs:allow-document-read-recursive","fs:allow-document-write","fs:allow-document-write-recursive","fs:allow-document-meta","fs:allow-document-meta-recursive","fs:allow-remove","fs:allow-create","fs:allow-exists","fs:allow-app-write","fs:allow-app-write-recursive","fs:allow-appcache-write","fs:allow-appcache-write-recursive","fs:allow-appconfig-write","fs:allow-app-read","fs:allow-app-read-recursive","fs:allow-appcache-read","fs:allow-appcache-read-recursive","fs:allow-appconfig-read","updater:allow-check","updater:default","dialog:allow-save","process:allow-restart"],"platforms":["windows"]}}
52 changes: 25 additions & 27 deletions src-tauri/src/printer/raw_printer/unix.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
use crate::printer::{escp::build_document, form_data_modeling::DocumentFormData};
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::Command;
// use std::fs::File;
// use std::io::Write;
// use std::path::Path;
// use std::process::Command;

#[tauri::command]
pub fn print_attestation(printer_name: String, form_data: DocumentFormData) -> String {
pub fn print_attestation(printer_name: String, form_data: DocumentFormData) -> Vec<u8> {
// Create a file with raw ESC/P commands
let file_path = Path::new("temp_print_file.prn");
let mut file = File::create(&file_path).expect("Failed to create file");
// for line in build_document(form_data) {
// write!(file, "{}", line).expect("Failed to write to file");
// }
let doc_bytes = build_document(form_data);
file.write_all(&doc_bytes).expect("Failed to write to file");
// Construct the lp command
let output = Command::new("lp")
.arg("-d")
.arg(printer_name)
.arg(file_path.to_str().unwrap())
.output()
.expect("Failed to execute command");
// let file_path = Path::new("temp_print_file.prn");
// let mut file = File::create(&file_path).expect("Failed to create file");

build_document(form_data)
// file.write_all(&doc_bytes).expect("Failed to write to file");
// // Construct the lp command
// let output = Command::new("lp")
// .arg("-d")
// .arg(printer_name)
// .arg(file_path.to_str().unwrap())
// .output()
// .expect("Failed to execute command");

if output.status.success() {
println!("Print job sent successfully.");
} else {
eprintln!("Error: {}", String::from_utf8_lossy(&output.stderr));
}
// if output.status.success() {
// println!("Print job sent successfully.");
// } else {
// eprintln!("Error: {}", String::from_utf8_lossy(&output.stderr));
// }

// Clean up
std::fs::remove_file(file_path).expect("Failed to remove temporary file");
"true".into()
// // Clean up
// std::fs::remove_file(file_path).expect("Failed to remove temporary file");
// "true".into()
}
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"frontendDist": "../build"
},
"productName": "Kine Helper",
"version": "0.0.56",
"version": "0.0.57",
"identifier": "be.kinehelper.prod",
"bundle": {
"active": true,
Expand Down
19 changes: 10 additions & 9 deletions src/lib/utils/fsAccessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
*/

import { platform } from '@tauri-apps/plugin-os';
import {
BaseDirectory,
mkdir,
remove,
exists,
readFile
} from '@tauri-apps/plugin-fs';
import { BaseDirectory, mkdir, remove, exists, readFile } from '@tauri-apps/plugin-fs';
import { open } from '@tauri-apps/plugin-shell';
import { invoke } from '@tauri-apps/api/core';
import { appLocalDataDir, documentDir } from '@tauri-apps/api/path';
Expand All @@ -32,10 +26,14 @@ async function completePath() {
if (platform() === 'windows') {
return await documentDir();
}
return await appLocalDataDir();
let localDir = await appLocalDataDir();
console.log('the local dir', localDir);
return localDir;
}

async function pathFormat(path) {
console.log('in pathFormat with ', path);

if (platform() === 'windows') {
return `kine-helper-\\${path.replaceAll('/', '\\')}`;
}
Expand All @@ -51,6 +49,7 @@ export async function file_exists(path) {
}

export async function save_to_disk(path, fileName, fileContent) {
console.log('in save_to_disk with ', path, fileName, fileContent);
let formatedPath = await pathFormat(path);
let normalizedFileName = fileName;
//* We might eventually catch a (directory already exist) wich is not a problem
Expand Down Expand Up @@ -88,5 +87,7 @@ export async function read_file(path) {
}

export async function open_file(path) {
return await open((await completePath()) + (platform() === 'windows' ? '\\' : '/') + (await pathFormat(path)));
return await open(
(await completePath()) + (platform() === 'windows' ? '\\' : '/') + (await pathFormat(path))
);
}
31 changes: 31 additions & 0 deletions src/lib/utils/rawPrinting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { invoke } from '@tauri-apps/api/core';
import { user } from '../stores/UserStore';
import { get } from 'svelte/store';
import dayjs from 'dayjs';
import { platform } from '@tauri-apps/plugin-os';
import { remove_file, save_to_disk } from './fsAccessor';
import { Command } from '@tauri-apps/plugin-shell';
import { appLocalDataDir } from '@tauri-apps/api/path';

export async function printAttestation(
patient,
Expand Down Expand Up @@ -87,5 +91,32 @@ export async function printAttestation(
}
};
console.log('formData in RawPrinter ==', formData);
if (platform() === 'macos') {
//* Build the command's bytes
const attestationBytes = await await invoke('print_attestation', {
printerName: settingsDetails.raw_printer,
formData
});
const fileName = 'temp_print_file.prn'
//* Create a temporary file in wich to store these bytes
//* AND Store the bytes in it
let setupPathResponse = await save_to_disk(
'', //? Dans le dossier appDataLocal racin c'est ok
fileName,
attestationBytes
);
let localDir = await appLocalDataDir();
const filePath = `${localDir}/${fileName}`;
console.log(attestationBytes, setupPathResponse, filePath);

//* Create the print command
let printCommand = Command.create('lp', ['-d', settingsDetails.raw_printer, filePath]);
//* Send the print commands to the printer
await printCommand.execute();

//* remove the temporary file
await remove_file(fileName, {})
return
}
return await invoke('print_attestation', { printerName: settingsDetails.raw_printer, formData });
}

0 comments on commit c64fcbb

Please sign in to comment.