From d28184af1a3e74b40cfd4ac626340481822ef902 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Thu, 21 May 2026 13:50:57 +0200 Subject: [PATCH] Fixed PDFium initialization logic (#771) --- runtime/src/pdfium.rs | 68 +++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/runtime/src/pdfium.rs b/runtime/src/pdfium.rs index 017958c3..98ba4046 100644 --- a/runtime/src/pdfium.rs +++ b/runtime/src/pdfium.rs @@ -1,47 +1,65 @@ +use std::error::Error; use std::sync::Mutex; use once_cell::sync::Lazy; use pdfium_render::prelude::Pdfium; use log::{error, warn}; pub static PDFIUM_LIB_PATH: Lazy>> = Lazy::new(|| Mutex::new(None)); +static PDFIUM: Lazy>> = Lazy::new(|| Mutex::new(None)); pub trait PdfiumInit { - fn ai_studio_init() -> Result>; + fn ai_studio_init() -> Result>; } impl PdfiumInit for Pdfium { /// Initializes the PDFium library for AI Studio. - fn ai_studio_init() -> Result> { - let lib_path = PDFIUM_LIB_PATH.lock().unwrap(); - if let Some(path) = lib_path.as_ref() { - 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}'." - ); + fn ai_studio_init() -> Result> { + let mut pdfium = PDFIUM.lock().unwrap(); + if let Some(pdfium) = pdfium.as_ref() { + return Ok(pdfium.clone()); + } - Err(Box::new(system_error)) - } + let loaded_pdfium = load_pdfium().map_err(|error| { + Box::new(std::io::Error::other(error)) as Box + })?; + *pdfium = Some(loaded_pdfium.clone()); + + Ok(loaded_pdfium) + } +} + +fn load_pdfium() -> Result { + let lib_path = PDFIUM_LIB_PATH.lock().unwrap().clone(); + if let Some(path) = lib_path.as_ref() { + 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) => { + let error_message = format!( + "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}'." + ); + + error!("{error_message}"); + Err(error_message) } } } } + } - 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}'." - ); + 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) => { + let error_message = format!( + "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)) - } + error!("{error_message}"); + Err(error_message) } } -} +} \ No newline at end of file