From 0e14a3cb58270fe5814076ca80e2f36961dcc3d5 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 1 Feb 2026 19:51:12 +0100 Subject: [PATCH] Improved PDFium initialization and error handling logic (#649) --- .../wwwroot/changelog/v26.2.1.md | 1 + runtime/src/file_data.rs | 10 ++++- runtime/src/pdfium.rs | 39 ++++++++++++++----- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/app/MindWork AI Studio/wwwroot/changelog/v26.2.1.md b/app/MindWork AI Studio/wwwroot/changelog/v26.2.1.md index 4cd8fdb9..a743739d 100644 --- a/app/MindWork AI Studio/wwwroot/changelog/v26.2.1.md +++ b/app/MindWork AI Studio/wwwroot/changelog/v26.2.1.md @@ -3,4 +3,5 @@ - Added support for defining document analysis policies (in preview) by configuration plugins, enabling centralized management of policies across entire departments or organizations. - Added an option to hide the policy definition in the Document Analysis Assistant (in preview) when exporting and distributing that policy by a configuration plugin in organizations, making it easier for users to use. - Added the policy export functionality to the Document Analysis Assistant (in preview). You can now export policies as Lua code for a configuration plugin to distribute the policy across your organization. +- Improved the error checking & logging behavior when the installed `PDFium` version did not meet the minimum required version. - Fixed a bug where the global minimum confidence level was not being applied to the assistants. \ No newline at end of file diff --git a/runtime/src/file_data.rs b/runtime/src/file_data.rs index 969f19e1..b0ba1b24 100644 --- a/runtime/src/file_data.rs +++ b/runtime/src/file_data.rs @@ -246,7 +246,13 @@ async fn stream_pdf(file_path: &str) -> Result { let (tx, rx) = mpsc::channel(10); tokio::task::spawn_blocking(move || { - let pdfium = Pdfium::ai_studio_init(); + let pdfium = match Pdfium::ai_studio_init() { + Ok(pdfium) => pdfium, + Err(e) => { + let _ = tx.blocking_send(Err(e)); + return; + } + }; let doc = match pdfium.load_pdf_from_file(&path, None) { Ok(document) => document, Err(e) => { @@ -464,4 +470,4 @@ async fn stream_pptx(file_path: &str, extract_images: bool) -> Result>> = Lazy::new(|| Mutex::new(None)); pub trait PdfiumInit { - fn ai_studio_init() -> Pdfium; + fn ai_studio_init() -> Result>; } impl PdfiumInit for Pdfium { /// Initializes the PDFium library for AI Studio. - fn ai_studio_init() -> Pdfium { + fn ai_studio_init() -> Result> { let lib_path = PDFIUM_LIB_PATH.lock().unwrap(); if let Some(path) = lib_path.as_ref() { - return Pdfium::new( - Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path(path)) - .or_else(|_| Pdfium::bind_to_system_library()) - .unwrap(), - ); + return match Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path(path)) { + Ok(binding) => Ok(Pdfium::new(binding)), + Err(library_error) => { + match Pdfium::bind_to_system_library() { + Ok(binding) => Ok(Pdfium::new(binding)), + Err(system_error) => { + error!( + "Failed to load PDFium from '{path}' and the system library. Developer action (from repo root): run the build script once to download the required PDFium version: `cd app/Build` and `dotnet run build`. Details: library error: '{library_error}'; system error: '{system_error}'." + ); + + Err(Box::new(system_error)) + } + } + } + } } - Pdfium::new(Pdfium::bind_to_system_library().unwrap()) + warn!("No custom PDFium library path set; trying to load PDFium from the system library."); + match Pdfium::bind_to_system_library() { + Ok(binding) => Ok(Pdfium::new(binding)), + Err(system_error) => { + error!( + "Failed to load PDFium from the system library. Developer action (from repo root): run the build script once to download the required PDFium version: `cd app/Build` and `dotnet run build`. Details: '{system_error}'." + ); + + Err(Box::new(system_error)) + } + } } -} \ No newline at end of file +}