mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2026-04-22 23:08:53 +02:00
Fix printing on Mac desktop (#5920)
# Description of Changes Fix #5164 As I mentioned on the bug https://github.com/Stirling-Tools/Stirling-PDF/issues/5164#issuecomment-4045170827, it's impossible to print on Mac currently because `iframe.contentWindow?.print()` silently does nothing in Tauri on Mac, but [it seems unlikely that this will be fixed](https://github.com/tauri-apps/tauri/issues/13451#issuecomment-4048075861). Instead, I've linked directly to the Mac `PDFKit` framework in Rust to use its printing functionality instead of Safari's. I believe that `PDFKit` is what `Preview.app` is using and the print UI that it generates seems to perform identically, so this should solve the issue on Mac. Hopefully one day the TS iframe print API will be fixed and we'll be able to get rid of this code, or [there'll be an official Tauri plugin for printing which we can use instead](https://github.com/tauri-apps/plugins-workspace/issues/293). This implementation should be entirely Mac-specific. Windows & Linux will continue to use their TS printing (which comes from EmbedPDF) unless we have a good reason to change them to use a native solution as well.
This commit is contained in:
@@ -3,6 +3,8 @@ pub mod files;
|
||||
pub mod connection;
|
||||
pub mod auth;
|
||||
pub mod default_app;
|
||||
pub mod platform;
|
||||
pub mod print;
|
||||
|
||||
pub use backend::{cleanup_backend, get_backend_port, start_backend};
|
||||
pub use files::{add_opened_file, clear_opened_files, get_opened_files, pop_opened_files};
|
||||
@@ -26,3 +28,5 @@ pub use auth::{
|
||||
start_oauth_login,
|
||||
};
|
||||
pub use default_app::{is_default_pdf_handler, set_as_default_pdf_handler};
|
||||
pub use platform::get_desktop_os;
|
||||
pub use print::print_pdf_file_native;
|
||||
|
||||
20
frontend/src-tauri/src/commands/platform.rs
Normal file
20
frontend/src-tauri/src/commands/platform.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum DesktopOS {
|
||||
MacOS,
|
||||
Windows,
|
||||
Linux,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_desktop_os() -> DesktopOS {
|
||||
match std::env::consts::OS {
|
||||
"macos" => DesktopOS::MacOS,
|
||||
"windows" => DesktopOS::Windows,
|
||||
"linux" => DesktopOS::Linux,
|
||||
_ => DesktopOS::Unknown,
|
||||
}
|
||||
}
|
||||
68
frontend/src-tauri/src/commands/print.rs
Normal file
68
frontend/src-tauri/src/commands/print.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
#[cfg(target_os = "macos")]
|
||||
mod macos {
|
||||
use std::path::Path;
|
||||
use std::sync::mpsc;
|
||||
|
||||
use objc2::rc::autoreleasepool;
|
||||
use objc2::AnyThread;
|
||||
use objc2_app_kit::NSPrintInfo;
|
||||
use objc2_foundation::{MainThreadMarker, NSString, NSURL};
|
||||
use objc2_pdf_kit::{PDFDocument, PDFPrintScalingMode};
|
||||
use tauri::AppHandle;
|
||||
|
||||
#[tauri::command]
|
||||
pub fn print_pdf_file_native(app: AppHandle, file_path: String, title: Option<String>) -> Result<(), String> {
|
||||
if !Path::new(&file_path).exists() {
|
||||
return Err(format!("Print file does not exist: {}", file_path));
|
||||
}
|
||||
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
app.run_on_main_thread(move || {
|
||||
let result = autoreleasepool(|_| {
|
||||
let mtm = MainThreadMarker::new()
|
||||
.ok_or_else(|| "macOS print must run on the main thread".to_string())?;
|
||||
|
||||
let path_string = NSString::from_str(&file_path);
|
||||
let file_url = NSURL::fileURLWithPath(&path_string);
|
||||
let document = unsafe { PDFDocument::initWithURL(PDFDocument::alloc(), &file_url) }
|
||||
.ok_or_else(|| format!("Failed to load PDF for printing: {}", file_path))?;
|
||||
|
||||
let print_info = NSPrintInfo::sharedPrintInfo();
|
||||
let print_operation = unsafe {
|
||||
document
|
||||
.printOperationForPrintInfo_scalingMode_autoRotate(
|
||||
Some(&print_info),
|
||||
PDFPrintScalingMode::PageScaleDownToFit,
|
||||
true,
|
||||
mtm,
|
||||
)
|
||||
}
|
||||
.ok_or_else(|| "PDFKit did not create a print operation".to_string())?;
|
||||
|
||||
if let Some(job_title) = title.as_deref() {
|
||||
print_operation.setJobTitle(Some(&NSString::from_str(job_title)));
|
||||
}
|
||||
|
||||
print_operation.setShowsPrintPanel(true);
|
||||
print_operation.setShowsProgressPanel(true);
|
||||
let _ = print_operation.runOperation();
|
||||
Ok(())
|
||||
});
|
||||
|
||||
let _ = sender.send(result);
|
||||
}).map_err(|error| error.to_string())?;
|
||||
|
||||
receiver
|
||||
.recv()
|
||||
.map_err(|error| error.to_string())?
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub use macos::print_pdf_file_native;
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[tauri::command]
|
||||
pub fn print_pdf_file_native(_file_path: String, _title: Option<String>) -> Result<(), String> {
|
||||
Err("Native PDF printing is only implemented on macOS".to_string())
|
||||
}
|
||||
@@ -27,6 +27,8 @@ use commands::{
|
||||
save_user_info,
|
||||
set_connection_mode,
|
||||
set_as_default_pdf_handler,
|
||||
get_desktop_os,
|
||||
print_pdf_file_native,
|
||||
start_backend,
|
||||
start_oauth_login,
|
||||
};
|
||||
@@ -163,6 +165,8 @@ pub fn run() {
|
||||
get_user_info,
|
||||
clear_user_info,
|
||||
start_oauth_login,
|
||||
get_desktop_os,
|
||||
print_pdf_file_native,
|
||||
])
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while building tauri application")
|
||||
|
||||
Reference in New Issue
Block a user