kreuzberg 4.0.0.pre.rc.7 → 4.0.0.pre.rc.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +13 -12
- data/README.md +22 -0
- data/ext/kreuzberg_rb/native/.cargo/config.toml +1 -1
- data/ext/kreuzberg_rb/native/Cargo.lock +397 -183
- data/ext/kreuzberg_rb/native/Cargo.toml +3 -3
- data/ext/kreuzberg_rb/native/src/lib.rs +36 -13
- data/kreuzberg.gemspec +34 -2
- data/lib/kreuzberg/cache_api.rb +35 -0
- data/lib/kreuzberg/error_context.rb +49 -1
- data/lib/kreuzberg/extraction_api.rb +255 -0
- data/lib/kreuzberg/version.rb +1 -1
- data/lib/kreuzberg.rb +6 -0
- data/lib/libpdfium.dylib +0 -0
- data/sig/kreuzberg.rbs +9 -0
- data/vendor/Cargo.toml +44 -0
- data/vendor/kreuzberg/Cargo.toml +65 -35
- data/vendor/kreuzberg/README.md +50 -0
- data/vendor/kreuzberg/build.rs +548 -190
- data/vendor/kreuzberg/src/api/mod.rs +0 -2
- data/vendor/kreuzberg/src/core/pipeline.rs +13 -0
- data/vendor/kreuzberg/src/embeddings.rs +71 -3
- data/vendor/kreuzberg/src/error.rs +1 -1
- data/vendor/kreuzberg/src/extraction/docx.rs +1 -1
- data/vendor/kreuzberg/src/extraction/html.rs +37 -5
- data/vendor/kreuzberg/src/extractors/pdf.rs +99 -47
- data/vendor/kreuzberg/src/mcp/mod.rs +3 -2
- data/vendor/kreuzberg/src/mcp/server.rs +106 -0
- data/vendor/kreuzberg/src/pdf/bindings.rs +44 -0
- data/vendor/kreuzberg/src/pdf/bundled.rs +346 -0
- data/vendor/kreuzberg/src/pdf/metadata.rs +2 -2
- data/vendor/kreuzberg/src/pdf/mod.rs +6 -0
- data/vendor/kreuzberg/src/pdf/rendering.rs +2 -2
- data/vendor/kreuzberg/src/pdf/table.rs +3 -0
- data/vendor/kreuzberg/src/pdf/text.rs +2 -2
- data/vendor/kreuzberg/src/text/quality_processor.rs +1 -1
- data/vendor/kreuzberg/tests/concurrency_stress.rs +1 -1
- data/vendor/kreuzberg/tests/format_integration.rs +4 -1
- data/vendor/kreuzberg/tests/pdfium_linking.rs +374 -0
- data/vendor/kreuzberg-ffi/Cargo.toml +63 -0
- data/vendor/kreuzberg-ffi/README.md +851 -0
- data/vendor/kreuzberg-ffi/build.rs +176 -0
- data/vendor/kreuzberg-ffi/cbindgen.toml +27 -0
- data/vendor/kreuzberg-ffi/kreuzberg-ffi-install.pc +12 -0
- data/vendor/kreuzberg-ffi/kreuzberg-ffi.pc.in +12 -0
- data/vendor/kreuzberg-ffi/kreuzberg.h +1087 -0
- data/vendor/kreuzberg-ffi/src/lib.rs +3616 -0
- data/vendor/kreuzberg-ffi/src/panic_shield.rs +247 -0
- data/vendor/kreuzberg-ffi/tests.disabled/README.md +48 -0
- data/vendor/kreuzberg-ffi/tests.disabled/config_loading_tests.rs +299 -0
- data/vendor/kreuzberg-ffi/tests.disabled/config_tests.rs +346 -0
- data/vendor/kreuzberg-ffi/tests.disabled/extractor_tests.rs +232 -0
- data/vendor/kreuzberg-ffi/tests.disabled/plugin_registration_tests.rs +470 -0
- data/vendor/kreuzberg-tesseract/.commitlintrc.json +13 -0
- data/vendor/kreuzberg-tesseract/.crate-ignore +2 -0
- data/vendor/kreuzberg-tesseract/Cargo.lock +2933 -0
- data/vendor/kreuzberg-tesseract/Cargo.toml +48 -0
- data/vendor/kreuzberg-tesseract/LICENSE +22 -0
- data/vendor/kreuzberg-tesseract/README.md +399 -0
- data/vendor/kreuzberg-tesseract/build.rs +1354 -0
- data/vendor/kreuzberg-tesseract/patches/README.md +71 -0
- data/vendor/kreuzberg-tesseract/patches/tesseract.diff +199 -0
- data/vendor/kreuzberg-tesseract/src/api.rs +1371 -0
- data/vendor/kreuzberg-tesseract/src/choice_iterator.rs +77 -0
- data/vendor/kreuzberg-tesseract/src/enums.rs +297 -0
- data/vendor/kreuzberg-tesseract/src/error.rs +81 -0
- data/vendor/kreuzberg-tesseract/src/lib.rs +145 -0
- data/vendor/kreuzberg-tesseract/src/monitor.rs +57 -0
- data/vendor/kreuzberg-tesseract/src/mutable_iterator.rs +197 -0
- data/vendor/kreuzberg-tesseract/src/page_iterator.rs +253 -0
- data/vendor/kreuzberg-tesseract/src/result_iterator.rs +286 -0
- data/vendor/kreuzberg-tesseract/src/result_renderer.rs +183 -0
- data/vendor/kreuzberg-tesseract/tests/integration_test.rs +211 -0
- data/vendor/rb-sys/src/lib.rs +1 -0
- metadata +41 -3
- data/vendor/rb-sys/bin/release.sh +0 -22
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
//! Runtime extraction of bundled PDFium library.
|
|
2
|
+
//!
|
|
3
|
+
//! When the `pdf-bundled` feature is enabled, the PDFium library is embedded in the binary
|
|
4
|
+
//! using `include_bytes!` during compilation. This module handles runtime extraction to a
|
|
5
|
+
//! temporary directory and provides the path for dynamic loading.
|
|
6
|
+
//!
|
|
7
|
+
//! # How It Works
|
|
8
|
+
//!
|
|
9
|
+
//! 1. During build (build.rs): PDFium is copied to OUT_DIR and the build script sets
|
|
10
|
+
//! `KREUZBERG_PDFIUM_BUNDLED_PATH` environment variable
|
|
11
|
+
//! 2. At compile time: `include_bytes!` embeds the library binary in the executable
|
|
12
|
+
//! 3. At runtime: `extract_bundled_pdfium()` extracts to `$TMPDIR/kreuzberg-pdfium/`
|
|
13
|
+
//! 4. Library is reused if already present (based on modification time)
|
|
14
|
+
//!
|
|
15
|
+
//! # Example
|
|
16
|
+
//!
|
|
17
|
+
//! ```rust,ignore
|
|
18
|
+
//! # #[cfg(feature = "pdf-bundled")]
|
|
19
|
+
//! # {
|
|
20
|
+
//! use kreuzberg::pdf::bundled::extract_bundled_pdfium;
|
|
21
|
+
//!
|
|
22
|
+
//! # fn example() -> kreuzberg::Result<()> {
|
|
23
|
+
//! let lib_path = extract_bundled_pdfium()?;
|
|
24
|
+
//! println!("Extracted to: {}", lib_path.display());
|
|
25
|
+
//! # Ok(())
|
|
26
|
+
//! # }
|
|
27
|
+
//! # }
|
|
28
|
+
//! ```
|
|
29
|
+
|
|
30
|
+
use std::fs;
|
|
31
|
+
use std::io;
|
|
32
|
+
use std::path::{Path, PathBuf};
|
|
33
|
+
|
|
34
|
+
#[cfg(unix)]
|
|
35
|
+
use std::os::unix::fs::PermissionsExt;
|
|
36
|
+
|
|
37
|
+
/// Runtime library name and extraction directory for the bundled PDFium library.
|
|
38
|
+
///
|
|
39
|
+
/// Returns tuple of (library_name, extraction_directory)
|
|
40
|
+
fn bundled_library_info() -> (&'static str, &'static str) {
|
|
41
|
+
if cfg!(target_os = "windows") {
|
|
42
|
+
("pdfium.dll", "kreuzberg-pdfium")
|
|
43
|
+
} else if cfg!(target_os = "macos") {
|
|
44
|
+
("libpdfium.dylib", "kreuzberg-pdfium")
|
|
45
|
+
} else if cfg!(target_os = "linux") {
|
|
46
|
+
("libpdfium.so", "kreuzberg-pdfium")
|
|
47
|
+
} else {
|
|
48
|
+
// Fallback for other Unix-like systems
|
|
49
|
+
("libpdfium.so", "kreuzberg-pdfium")
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// Get the temporary directory for bundled PDFium extraction.
|
|
54
|
+
///
|
|
55
|
+
/// Uses `std::env::temp_dir()` on all platforms.
|
|
56
|
+
fn get_extraction_dir() -> io::Result<PathBuf> {
|
|
57
|
+
let (_, subdir) = bundled_library_info();
|
|
58
|
+
Ok(std::env::temp_dir().join(subdir))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// Check if extracted library exists and is valid.
|
|
62
|
+
///
|
|
63
|
+
/// Verifies:
|
|
64
|
+
/// - File exists at expected path
|
|
65
|
+
/// - File size matches embedded size (basic validation)
|
|
66
|
+
///
|
|
67
|
+
/// Returns `true` if library can be safely reused, `false` if extraction is needed.
|
|
68
|
+
fn is_extracted_library_valid(lib_path: &Path, embedded_size: usize) -> bool {
|
|
69
|
+
if !lib_path.exists() {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Verify file size matches to catch partial/corrupted extractions
|
|
74
|
+
match fs::metadata(lib_path) {
|
|
75
|
+
Ok(metadata) => {
|
|
76
|
+
let file_size = metadata.len() as usize;
|
|
77
|
+
// Allow 1% size difference for platform-specific variations
|
|
78
|
+
let size_tolerance = (embedded_size as f64 * 0.01) as usize;
|
|
79
|
+
let min_size = embedded_size.saturating_sub(size_tolerance);
|
|
80
|
+
let max_size = embedded_size.saturating_add(size_tolerance);
|
|
81
|
+
file_size >= min_size && file_size <= max_size
|
|
82
|
+
}
|
|
83
|
+
Err(_) => false,
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/// Extract bundled PDFium library to temporary directory.
|
|
88
|
+
///
|
|
89
|
+
/// # Behavior
|
|
90
|
+
///
|
|
91
|
+
/// - Embeds PDFium library using `include_bytes!`
|
|
92
|
+
/// - Extracts to `$TMPDIR/kreuzberg-pdfium/` (non-WASM only)
|
|
93
|
+
/// - Reuses extracted library if size matches
|
|
94
|
+
/// - Sets permissions to 0755 on Unix
|
|
95
|
+
/// - Returns path to extracted library
|
|
96
|
+
///
|
|
97
|
+
/// # WASM Handling
|
|
98
|
+
///
|
|
99
|
+
/// On WASM targets (wasm32-*), this function returns an error with a helpful
|
|
100
|
+
/// message directing users to use WASM-specific initialization. WASM PDFium
|
|
101
|
+
/// is initialized through the runtime, not via file extraction.
|
|
102
|
+
///
|
|
103
|
+
/// # Errors
|
|
104
|
+
///
|
|
105
|
+
/// Returns `std::io::Error` if:
|
|
106
|
+
/// - Cannot create extraction directory
|
|
107
|
+
/// - Cannot write library file
|
|
108
|
+
/// - Cannot set file permissions (Unix only)
|
|
109
|
+
/// - Target is WASM (filesystem access not available)
|
|
110
|
+
///
|
|
111
|
+
/// # Platform-Specific Library Names
|
|
112
|
+
///
|
|
113
|
+
/// - Linux: `libpdfium.so`
|
|
114
|
+
/// - macOS: `libpdfium.dylib`
|
|
115
|
+
/// - Windows: `pdfium.dll`
|
|
116
|
+
pub fn extract_bundled_pdfium() -> io::Result<PathBuf> {
|
|
117
|
+
// WASM targets cannot use file extraction
|
|
118
|
+
#[cfg(target_arch = "wasm32")]
|
|
119
|
+
{
|
|
120
|
+
return Err(io::Error::new(
|
|
121
|
+
io::ErrorKind::Unsupported,
|
|
122
|
+
"File extraction is not available in WASM. \
|
|
123
|
+
PDFium for WASM must be initialized via the WebAssembly runtime. \
|
|
124
|
+
Use a WASM-compatible environment with proper module initialization.",
|
|
125
|
+
));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let (lib_name, _) = bundled_library_info();
|
|
129
|
+
let extract_dir = get_extraction_dir()?;
|
|
130
|
+
|
|
131
|
+
// Create extraction directory if it doesn't exist
|
|
132
|
+
fs::create_dir_all(&extract_dir).map_err(|e| {
|
|
133
|
+
io::Error::new(
|
|
134
|
+
e.kind(),
|
|
135
|
+
format!(
|
|
136
|
+
"Failed to create bundled pdfium extraction directory '{}': {}",
|
|
137
|
+
extract_dir.display(),
|
|
138
|
+
e
|
|
139
|
+
),
|
|
140
|
+
)
|
|
141
|
+
})?;
|
|
142
|
+
|
|
143
|
+
let lib_path = extract_dir.join(lib_name);
|
|
144
|
+
|
|
145
|
+
// Include bundled PDFium library
|
|
146
|
+
let bundled_lib = include_bytes!(env!("KREUZBERG_PDFIUM_BUNDLED_PATH"));
|
|
147
|
+
|
|
148
|
+
// Check if library already exists and is valid
|
|
149
|
+
if is_extracted_library_valid(&lib_path, bundled_lib.len()) {
|
|
150
|
+
return Ok(lib_path);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Write library to disk
|
|
154
|
+
fs::write(&lib_path, bundled_lib).map_err(|e| {
|
|
155
|
+
io::Error::new(
|
|
156
|
+
e.kind(),
|
|
157
|
+
format!(
|
|
158
|
+
"Failed to extract bundled pdfium library to '{}': {}",
|
|
159
|
+
lib_path.display(),
|
|
160
|
+
e
|
|
161
|
+
),
|
|
162
|
+
)
|
|
163
|
+
})?;
|
|
164
|
+
|
|
165
|
+
// Set executable permissions on Unix
|
|
166
|
+
#[cfg(unix)]
|
|
167
|
+
{
|
|
168
|
+
let perms = fs::Permissions::from_mode(0o755);
|
|
169
|
+
fs::set_permissions(&lib_path, perms).map_err(|e| {
|
|
170
|
+
io::Error::new(
|
|
171
|
+
e.kind(),
|
|
172
|
+
format!(
|
|
173
|
+
"Failed to set permissions on bundled pdfium library '{}': {}",
|
|
174
|
+
lib_path.display(),
|
|
175
|
+
e
|
|
176
|
+
),
|
|
177
|
+
)
|
|
178
|
+
})?;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
Ok(lib_path)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
#[cfg(test)]
|
|
185
|
+
mod tests {
|
|
186
|
+
use super::*;
|
|
187
|
+
|
|
188
|
+
#[test]
|
|
189
|
+
fn test_bundled_library_info_windows() {
|
|
190
|
+
if cfg!(target_os = "windows") {
|
|
191
|
+
let (name, dir) = bundled_library_info();
|
|
192
|
+
assert_eq!(name, "pdfium.dll");
|
|
193
|
+
assert_eq!(dir, "kreuzberg-pdfium");
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
#[test]
|
|
198
|
+
fn test_bundled_library_info_macos() {
|
|
199
|
+
if cfg!(target_os = "macos") {
|
|
200
|
+
let (name, dir) = bundled_library_info();
|
|
201
|
+
assert_eq!(name, "libpdfium.dylib");
|
|
202
|
+
assert_eq!(dir, "kreuzberg-pdfium");
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
#[test]
|
|
207
|
+
fn test_bundled_library_info_linux() {
|
|
208
|
+
if cfg!(target_os = "linux") {
|
|
209
|
+
let (name, dir) = bundled_library_info();
|
|
210
|
+
assert_eq!(name, "libpdfium.so");
|
|
211
|
+
assert_eq!(dir, "kreuzberg-pdfium");
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
#[test]
|
|
216
|
+
fn test_get_extraction_dir() {
|
|
217
|
+
let result = get_extraction_dir();
|
|
218
|
+
assert!(result.is_ok());
|
|
219
|
+
let dir = result.unwrap();
|
|
220
|
+
assert!(dir.to_str().is_some());
|
|
221
|
+
assert!(dir.ends_with("kreuzberg-pdfium"));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
#[test]
|
|
225
|
+
fn test_is_extracted_library_valid_missing() {
|
|
226
|
+
let nonexistent = PathBuf::from("/tmp/nonexistent-pdfium-test");
|
|
227
|
+
assert!(!is_extracted_library_valid(&nonexistent, 1000));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
#[test]
|
|
231
|
+
fn test_is_extracted_library_valid_size_match() {
|
|
232
|
+
// Create a temporary test file
|
|
233
|
+
let temp_dir = std::env::temp_dir();
|
|
234
|
+
let test_file = temp_dir.join("test-pdfium-size.dll");
|
|
235
|
+
let test_size = 5_000_000;
|
|
236
|
+
let test_data = vec![0u8; test_size];
|
|
237
|
+
|
|
238
|
+
if let Ok(_) = fs::write(&test_file, &test_data) {
|
|
239
|
+
let is_valid = is_extracted_library_valid(&test_file, test_size);
|
|
240
|
+
assert!(is_valid);
|
|
241
|
+
let _ = fs::remove_file(&test_file);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
#[test]
|
|
246
|
+
fn test_is_extracted_library_valid_size_tolerance() {
|
|
247
|
+
// Create a temporary test file
|
|
248
|
+
let temp_dir = std::env::temp_dir();
|
|
249
|
+
let test_file = temp_dir.join("test-pdfium-tolerance.dll");
|
|
250
|
+
let original_size = 10_000_000;
|
|
251
|
+
let tolerance = (original_size as f64 * 0.01) as usize;
|
|
252
|
+
|
|
253
|
+
// Create file that's 0.5% smaller (within tolerance)
|
|
254
|
+
let actual_size = original_size - tolerance / 2;
|
|
255
|
+
let test_data = vec![0u8; actual_size];
|
|
256
|
+
|
|
257
|
+
if let Ok(_) = fs::write(&test_file, &test_data) {
|
|
258
|
+
let is_valid = is_extracted_library_valid(&test_file, original_size);
|
|
259
|
+
assert!(is_valid);
|
|
260
|
+
let _ = fs::remove_file(&test_file);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#[test]
|
|
265
|
+
fn test_is_extracted_library_valid_size_mismatch() {
|
|
266
|
+
// Create a temporary test file
|
|
267
|
+
let temp_dir = std::env::temp_dir();
|
|
268
|
+
let test_file = temp_dir.join("test-pdfium-mismatch.dll");
|
|
269
|
+
let original_size = 10_000_000;
|
|
270
|
+
|
|
271
|
+
// Create file that's 10% smaller (outside tolerance)
|
|
272
|
+
let actual_size = (original_size as f64 * 0.85) as usize;
|
|
273
|
+
let test_data = vec![0u8; actual_size];
|
|
274
|
+
|
|
275
|
+
if let Ok(_) = fs::write(&test_file, &test_data) {
|
|
276
|
+
let is_valid = is_extracted_library_valid(&test_file, original_size);
|
|
277
|
+
assert!(!is_valid);
|
|
278
|
+
let _ = fs::remove_file(&test_file);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
#[test]
|
|
283
|
+
#[cfg(feature = "pdf-bundled")]
|
|
284
|
+
fn test_extract_bundled_pdfium() {
|
|
285
|
+
let result = extract_bundled_pdfium();
|
|
286
|
+
assert!(result.is_ok());
|
|
287
|
+
|
|
288
|
+
let lib_path = result.unwrap();
|
|
289
|
+
assert!(
|
|
290
|
+
lib_path.exists(),
|
|
291
|
+
"Extracted library should exist at: {}",
|
|
292
|
+
lib_path.display()
|
|
293
|
+
);
|
|
294
|
+
assert!(lib_path.file_name().is_some(), "Library path should have filename");
|
|
295
|
+
|
|
296
|
+
// Verify correct library name for platform
|
|
297
|
+
let (expected_name, _) = bundled_library_info();
|
|
298
|
+
assert_eq!(lib_path.file_name().unwrap(), expected_name);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
#[test]
|
|
302
|
+
#[cfg(feature = "pdf-bundled")]
|
|
303
|
+
fn test_extract_bundled_pdfium_reuses_existing() {
|
|
304
|
+
// First extraction
|
|
305
|
+
let result1 = extract_bundled_pdfium();
|
|
306
|
+
assert!(result1.is_ok());
|
|
307
|
+
let path1 = result1.unwrap();
|
|
308
|
+
|
|
309
|
+
// Get file size and basic metadata of first extraction
|
|
310
|
+
let metadata1 = fs::metadata(&path1).expect("Should be able to read metadata");
|
|
311
|
+
let size1 = metadata1.len();
|
|
312
|
+
|
|
313
|
+
// Second extraction should reuse the file
|
|
314
|
+
let result2 = extract_bundled_pdfium();
|
|
315
|
+
assert!(result2.is_ok());
|
|
316
|
+
let path2 = result2.unwrap();
|
|
317
|
+
|
|
318
|
+
// Paths should be identical
|
|
319
|
+
assert_eq!(path1, path2, "Extraction should return same path on second call");
|
|
320
|
+
|
|
321
|
+
// File size should be identical (reused, not rewritten)
|
|
322
|
+
let metadata2 = fs::metadata(&path2).expect("Should be able to read metadata");
|
|
323
|
+
let size2 = metadata2.len();
|
|
324
|
+
assert_eq!(size1, size2, "Reused library should have same file size");
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
#[test]
|
|
328
|
+
#[cfg(unix)]
|
|
329
|
+
#[cfg(feature = "pdf-bundled")]
|
|
330
|
+
fn test_extract_bundled_pdfium_permissions() {
|
|
331
|
+
let result = extract_bundled_pdfium();
|
|
332
|
+
assert!(result.is_ok());
|
|
333
|
+
|
|
334
|
+
let lib_path = result.unwrap();
|
|
335
|
+
let metadata = fs::metadata(&lib_path).expect("Should be able to read metadata");
|
|
336
|
+
let perms = metadata.permissions();
|
|
337
|
+
let mode = perms.mode();
|
|
338
|
+
|
|
339
|
+
// Verify executable bit is set (at least 0o700 or 0o755)
|
|
340
|
+
assert!(
|
|
341
|
+
mode & 0o111 != 0,
|
|
342
|
+
"Library should have executable bit set, got mode: {:#o}",
|
|
343
|
+
mode
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
use super::bindings::bind_pdfium;
|
|
1
2
|
use super::error::{PdfError, Result};
|
|
2
3
|
use crate::types::{PageBoundary, PageInfo, PageStructure, PageUnitType};
|
|
3
4
|
use pdfium_render::prelude::*;
|
|
@@ -85,8 +86,7 @@ pub fn extract_metadata(pdf_bytes: &[u8]) -> Result<PdfMetadata> {
|
|
|
85
86
|
///
|
|
86
87
|
/// Returns only PDF-specific metadata (version, producer, encryption status, dimensions).
|
|
87
88
|
pub fn extract_metadata_with_password(pdf_bytes: &[u8], password: Option<&str>) -> Result<PdfMetadata> {
|
|
88
|
-
let bindings =
|
|
89
|
-
.map_err(|e| PdfError::MetadataExtractionFailed(format!("Failed to initialize Pdfium: {}", e)))?;
|
|
89
|
+
let bindings = bind_pdfium(PdfError::MetadataExtractionFailed, "metadata extraction")?;
|
|
90
90
|
|
|
91
91
|
let pdfium = Pdfium::new(bindings);
|
|
92
92
|
|
|
@@ -36,6 +36,10 @@
|
|
|
36
36
|
//! This module requires the `pdf` feature. The `ocr` feature enables additional
|
|
37
37
|
//! functionality in the PDF extractor for rendering pages to images.
|
|
38
38
|
#[cfg(feature = "pdf")]
|
|
39
|
+
pub(crate) mod bindings;
|
|
40
|
+
#[cfg(all(feature = "pdf", feature = "pdf-bundled"))]
|
|
41
|
+
pub mod bundled;
|
|
42
|
+
#[cfg(feature = "pdf")]
|
|
39
43
|
pub mod error;
|
|
40
44
|
#[cfg(feature = "pdf")]
|
|
41
45
|
pub mod images;
|
|
@@ -48,6 +52,8 @@ pub mod table;
|
|
|
48
52
|
#[cfg(feature = "pdf")]
|
|
49
53
|
pub mod text;
|
|
50
54
|
|
|
55
|
+
#[cfg(all(feature = "pdf", feature = "pdf-bundled"))]
|
|
56
|
+
pub use bundled::extract_bundled_pdfium;
|
|
51
57
|
#[cfg(feature = "pdf")]
|
|
52
58
|
pub use error::PdfError;
|
|
53
59
|
#[cfg(feature = "pdf")]
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
use super::bindings::bind_pdfium;
|
|
1
2
|
use super::error::{PdfError, Result};
|
|
2
3
|
use image::DynamicImage;
|
|
3
4
|
use pdfium_render::prelude::*;
|
|
@@ -32,8 +33,7 @@ pub struct PdfRenderer {
|
|
|
32
33
|
|
|
33
34
|
impl PdfRenderer {
|
|
34
35
|
pub fn new() -> Result<Self> {
|
|
35
|
-
let binding =
|
|
36
|
-
.map_err(|e| PdfError::RenderingFailed(format!("Failed to initialize Pdfium: {}", e)))?;
|
|
36
|
+
let binding = bind_pdfium(PdfError::RenderingFailed, "page rendering")?;
|
|
37
37
|
|
|
38
38
|
let pdfium = Pdfium::new(binding);
|
|
39
39
|
Ok(Self { pdfium })
|
|
@@ -13,9 +13,11 @@ use pdfium_render::prelude::*;
|
|
|
13
13
|
/// Spacing threshold for word boundary detection (in PDF units).
|
|
14
14
|
///
|
|
15
15
|
/// Characters separated by more than this distance are considered separate words.
|
|
16
|
+
#[cfg(feature = "ocr")]
|
|
16
17
|
const WORD_SPACING_THRESHOLD: f32 = 3.0;
|
|
17
18
|
|
|
18
19
|
/// Minimum word length for table detection (filter out noise).
|
|
20
|
+
#[cfg(feature = "ocr")]
|
|
19
21
|
const MIN_WORD_LENGTH: usize = 1;
|
|
20
22
|
|
|
21
23
|
/// Extract words with positions from PDF page for table detection.
|
|
@@ -80,6 +82,7 @@ pub fn extract_words_from_page(_page: &PdfPage, _min_confidence: f64) -> Result<
|
|
|
80
82
|
}
|
|
81
83
|
|
|
82
84
|
/// Character with position information extracted from PDF.
|
|
85
|
+
#[cfg(feature = "ocr")]
|
|
83
86
|
#[derive(Debug, Clone)]
|
|
84
87
|
struct CharInfo {
|
|
85
88
|
text: char,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
use super::bindings::bind_pdfium;
|
|
1
2
|
use super::error::{PdfError, Result};
|
|
2
3
|
use crate::core::config::PageConfig;
|
|
3
4
|
use crate::types::{PageBoundary, PageContent};
|
|
@@ -13,8 +14,7 @@ pub struct PdfTextExtractor {
|
|
|
13
14
|
|
|
14
15
|
impl PdfTextExtractor {
|
|
15
16
|
pub fn new() -> Result<Self> {
|
|
16
|
-
let binding =
|
|
17
|
-
.map_err(|e| PdfError::TextExtractionFailed(format!("Failed to initialize Pdfium: {}", e)))?;
|
|
17
|
+
let binding = bind_pdfium(PdfError::TextExtractionFailed, "text extraction")?;
|
|
18
18
|
|
|
19
19
|
let pdfium = Pdfium::new(binding);
|
|
20
20
|
Ok(Self { pdfium })
|
|
@@ -19,7 +19,7 @@ use async_trait::async_trait;
|
|
|
19
19
|
///
|
|
20
20
|
/// ```rust,no_run
|
|
21
21
|
/// use kreuzberg::plugins::{Plugin, PostProcessor};
|
|
22
|
-
/// use kreuzberg::text::
|
|
22
|
+
/// use kreuzberg::text::QualityProcessor;
|
|
23
23
|
///
|
|
24
24
|
/// let processor = QualityProcessor;
|
|
25
25
|
/// assert_eq!(processor.name(), "quality-processing");
|
|
@@ -244,7 +244,7 @@ async fn test_concurrent_ocr_processing() {
|
|
|
244
244
|
#[test]
|
|
245
245
|
fn test_concurrent_ocr_cache_stress() {
|
|
246
246
|
use helpers::{get_test_file_path, skip_if_missing};
|
|
247
|
-
use std::sync::atomic::Ordering;
|
|
247
|
+
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
248
248
|
|
|
249
249
|
if skip_if_missing("images/ocr_image.jpg") {
|
|
250
250
|
tracing::debug!("Skipping OCR cache stress test: test file not available");
|
|
@@ -9,7 +9,10 @@
|
|
|
9
9
|
|
|
10
10
|
mod helpers;
|
|
11
11
|
|
|
12
|
-
use helpers::{assert_mime_type,
|
|
12
|
+
use helpers::{assert_mime_type, get_test_file_path, test_documents_available};
|
|
13
|
+
|
|
14
|
+
#[cfg(any(feature = "office", feature = "ocr"))]
|
|
15
|
+
use helpers::assert_non_empty_content;
|
|
13
16
|
use kreuzberg::core::config::ExtractionConfig;
|
|
14
17
|
use kreuzberg::core::extractor::extract_file;
|
|
15
18
|
|