1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use std::path::Path;
use crate::config::{LiteParseConfig, parse_target_pages};
#[cfg(not(target_arch = "wasm32"))]
use crate::conversion;
#[cfg(not(target_arch = "wasm32"))]
use crate::conversion::convert_data_to_pdf;
use crate::error::LiteParseError;
use crate::extract;
use crate::ocr::OcrEngine;
#[cfg(not(target_arch = "wasm32"))]
use crate::ocr::http_simple::HttpOcrEngine;
#[cfg(feature = "tesseract")]
use crate::ocr::tesseract::TesseractOcrEngine;
use crate::ocr_merge;
use crate::projection;
use crate::types::{ParsedPage, PdfInput};
/// Result of parsing a document.
pub struct ParseResult {
/// Parsed pages with projected text layout.
pub pages: Vec<ParsedPage>,
/// Full document text, concatenated from all pages.
pub text: String,
}
/// Main LiteParse orchestrator.
pub struct LiteParse {
config: LiteParseConfig,
/// Optional caller-provided OCR engine. When set, this overrides the
/// built-in selection logic (HTTP OCR / Tesseract). This is the primary
/// mechanism for plugging an OCR engine in environments without the
/// built-ins (e.g. WASM, where the JS side supplies a callback engine).
ocr_engine_override: Option<std::sync::Arc<dyn OcrEngine>>,
}
impl LiteParse {
pub fn new(config: LiteParseConfig) -> Self {
Self {
config,
ocr_engine_override: None,
}
}
/// Override the OCR engine. When set, the engine is used regardless of
/// `ocr_server_url` / built-in Tesseract availability.
pub fn with_ocr_engine(mut self, engine: std::sync::Arc<dyn OcrEngine>) -> Self {
self.ocr_engine_override = Some(engine);
self
}
/// Parse a document from a file path, returning structured results.
///
/// Non-PDF files are automatically converted to PDF first (requires
/// LibreOffice/ImageMagick on the system).
///
/// Not available on `wasm32` — the browser has no filesystem. Use
/// [`LiteParse::parse_input`] with [`PdfInput::Bytes`] instead.
#[cfg(not(target_arch = "wasm32"))]
pub async fn parse(&self, input: &str) -> Result<ParseResult, LiteParseError> {
self.parse_input(PdfInput::Path(input.to_string())).await
}
/// Parse a document from either a file path or raw bytes.
///
/// Use `PdfInput::Path` for files on disk or `PdfInput::Bytes` for
/// in-memory PDF data (e.g. from a network response or Node.js Buffer).
pub async fn parse_input(&self, input: PdfInput) -> Result<ParseResult, LiteParseError> {
let log = |msg: &str| {
if !self.config.quiet {
eprintln!("{}", msg);
}
};
let t0 = web_time::Instant::now();
let mut is_converted = false;
#[cfg(not(target_arch = "wasm32"))]
let _tmp_dir: Option<tempfile::TempDir>;
#[cfg(not(target_arch = "wasm32"))]
let _conv_tmp_dir: Option<tempfile::TempDir>;
let validated_input = {
#[cfg(target_arch = "wasm32")]
{
input
}
#[cfg(not(target_arch = "wasm32"))]
match input {
PdfInput::Path(p) => {
if conversion::is_pdf(&p) {
PdfInput::Path(p)
} else {
let (converted, tmp_dir) =
conversion::convert_to_pdf(&p, self.config.password.as_deref(), false)
.await?;
_conv_tmp_dir = tmp_dir;
// The on-disk source isn't ours to delete; only the
// converted temp file should be cleaned up by `tmp_dir`.
PdfInput::Path(converted.pdf_path)
}
}
PdfInput::Bytes(b) => {
let (converted, tmp_dir) =
convert_data_to_pdf(b, self.config.password.as_deref()).await?;
_tmp_dir = tmp_dir;
is_converted = true;
PdfInput::Path(converted.pdf_path)
}
}
};
// Determine which pages to extract
let target_pages = self
.config
.target_pages
.as_ref()
.map(|s| parse_target_pages(s))
.transpose()
.map_err(|e| format!("invalid --target-pages: {}", e))?;
// Extract text items from PDF pages
let mut pages = extract::extract_pages_from_input(
&validated_input,
target_pages.as_deref(),
self.config.max_pages,
self.config.password.as_deref(),
)?;
let t1 = web_time::Instant::now();
log(&format!(
"[liteparse] extract: {:.1}ms ({} pages)",
t1.duration_since(t0).as_secs_f64() * 1000.0,
pages.len()
));
// OCR pass
if self.config.ocr_enabled {
let engine: std::sync::Arc<dyn OcrEngine> = if let Some(e) =
self.ocr_engine_override.clone()
{
e
} else {
#[cfg(not(target_arch = "wasm32"))]
{
if let Some(ref url) = self.config.ocr_server_url {
std::sync::Arc::new(HttpOcrEngine::new(url.clone()))
} else {
#[cfg(feature = "tesseract")]
{
std::sync::Arc::new(TesseractOcrEngine::new(
self.config.tessdata_path.clone(),
))
}
#[cfg(not(feature = "tesseract"))]
{
return Err("OCR enabled but no --ocr-server-url provided and tesseract feature is disabled".into());
}
}
}
#[cfg(target_arch = "wasm32")]
{
return Err(
"OCR enabled but no `ocrEngine` callback was provided (WASM builds have no built-in OCR engine)".into(),
);
}
};
ocr_merge::ocr_and_merge_pages_from_input(
&mut pages,
&validated_input,
self.config.dpi,
engine,
&self.config.ocr_language,
self.config.num_workers,
)
.await?;
}
let t_ocr = web_time::Instant::now();
log(&format!(
"[liteparse] ocr: {:.1}ms",
t_ocr.duration_since(t1).as_secs_f64() * 1000.0
));
// Grid projection
let parsed_pages = projection::project_pages_to_grid(pages);
let t2 = web_time::Instant::now();
log(&format!(
"[liteparse] project: {:.1}ms",
t2.duration_since(t_ocr).as_secs_f64() * 1000.0
));
let full_text = parsed_pages
.iter()
.map(|p| p.text.as_str())
.collect::<Vec<_>>()
.join("\n\n");
let total = t2.duration_since(t0).as_secs_f64() * 1000.0;
log(&format!("[liteparse] total: {:.1}ms", total));
// Office docs and images that are temporarily created from bytes
// are removed, but not PDFs, as they pass through the coversion logic
// and are used directly as input.
// With this block, we insure that temporary PDFs created from bytes and/or
// converted from Office/image files are cleaned up as well.
if is_converted && let PdfInput::Path(p) = validated_input {
std::fs::remove_dir_all(Path::new(&p).parent().unwrap())?;
}
Ok(ParseResult {
pages: parsed_pages,
text: full_text,
})
}
pub fn config(&self) -> &LiteParseConfig {
&self.config
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[allow(clippy::field_reassign_with_default)]
fn test_new_stores_config() {
let mut cfg = LiteParseConfig::default();
cfg.ocr_enabled = false;
cfg.max_pages = 7;
let lp = LiteParse::new(cfg);
assert!(!lp.config().ocr_enabled);
assert_eq!(lp.config().max_pages, 7);
}
}