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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
use crate::config::{LiteParseConfig, parse_target_pages};
#[cfg(not(target_arch = "wasm32"))]
use crate::conversion;
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::output::markdown;
use crate::projection;
#[cfg(not(target_arch = "wasm32"))]
use crate::render;
use crate::types::{
ExtractedImage, OutlineTarget, Page, ParsedPage, PdfInput, ScreenshotRect, XfaPacket,
};
use pdfium::Library;
/// 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,
/// Document outline (bookmarks) when present. Used by the markdown
/// emitter as a high-priority heading source on untagged PDFs.
pub outline: Vec<OutlineTarget>,
/// Raster images extracted from the document. Empty unless the parser
/// was configured with `extract_images`. Each entry carries the same
/// `id` and `format` the markdown emitter referenced, so the caller can
/// match them up without parsing markdown.
pub images: Vec<ExtractedImage>,
/// Number of embedded image objects that could not be extracted. A bad
/// image does not fail the rest of the document parse.
pub image_error_count: u32,
/// PDFium form type (0 none, 1 AcroForm, 2 XFA full, 3 XFA foreground),
/// present only when form-field extraction is enabled.
pub form_type: Option<i32>,
/// The document's `/Info` `Creator` entry, when present.
pub creator: Option<String>,
/// The document's `/Info` `Producer` entry, when present.
pub producer: Option<String>,
/// Raw XFA packets, present only when `extract_xfa_packets` is enabled.
/// `Some([])` means extraction ran on a non-XFA document.
pub xfa_packets: Option<Vec<XfaPacket>>,
}
/// Result of rendering a single page screenshot.
#[derive(Debug, Clone)]
pub struct ScreenshotResult {
pub page_num: u32,
pub width: u32,
pub height: u32,
pub image_bytes: Vec<u8>,
/// True when every pixel has the same color (blank page after render).
pub is_solid_fill: bool,
/// Solid rectangles/lines detected in the raster (viewport coords).
/// Populated only when `LiteParseConfig::detect_screenshot_rects` is on.
pub rects: Vec<ScreenshotRect>,
}
/// Env var pointing at a fragmented glyph-outline → unicode font database
/// directory (`%02x%02x.msgpack` shards). When set, [`LiteParse::new`]
/// auto-wires a [`crate::FontDbResolver`] so buggy/obfuscated-font glyphs are
/// recovered without any extra wiring. Unset (default) leaves the hook dormant.
#[cfg(not(target_arch = "wasm32"))]
const FONT_DB_DIR_ENV: &str = "LITEPARSE_FONT_DB_DIR";
#[cfg(not(target_arch = "wasm32"))]
fn write_extracted_images(
output_dir: &str,
images: &mut [ExtractedImage],
) -> Result<(), LiteParseError> {
use std::collections::HashMap;
use std::path::Path;
std::fs::create_dir_all(output_dir)?;
// Platform contract (mirrors the LlamaParse C extractor, which the worker
// pipeline is built around): only the canonical file is written; every
// duplicate placement keeps its own `name` but points `path` at the
// canonical file. Markdown figure references are rewritten to the
// canonical name (`rewrite_duplicate_image_refs`) so they only ever
// reference files that exist.
let mut written: HashMap<String, String> = HashMap::new();
for image in images {
if let Some(canonical) = image.duplicate_of.as_ref()
&& let Some(path) = written.get(canonical)
{
image.path = Some(path.clone());
continue;
}
let path = Path::new(output_dir).join(&image.name);
std::fs::write(&path, image.bytes.as_slice())?;
let path = path.to_string_lossy().into_owned();
image.path = Some(path.clone());
written.insert(image.id.clone(), path);
}
Ok(())
}
/// Rewrite markdown figure references for deduplicated images to the
/// canonical entry's file name. The markdown emitter references each figure
/// by its own placement id (``), but only the canonical
/// file is written to disk (see `write_extracted_images`), so duplicate
/// placements must reference the canonical name, matching the resolution the
/// LlamaParse worker applies via `resolveOutputImageName`.
fn rewrite_duplicate_image_refs(
pages: &mut [ParsedPage],
full_text: &mut String,
images: &[ExtractedImage],
) {
use std::collections::HashMap;
let by_id: HashMap<&str, &ExtractedImage> = images
.iter()
.map(|image| (image.id.as_str(), image))
.collect();
let renames: Vec<(String, String)> = images
.iter()
.filter_map(|image| {
let canonical = by_id.get(image.duplicate_of.as_ref()?.as_str())?;
Some((
format!("", image.id, image.format),
format!("", canonical.name),
))
})
.collect();
if renames.is_empty() {
return;
}
for markdown in pages
.iter_mut()
.map(|page| &mut page.markdown)
.chain(std::iter::once(full_text))
{
for (from, to) in &renames {
if markdown.contains(from.as_str()) {
*markdown = markdown.replace(from.as_str(), to);
}
}
}
}
/// Build the default glyph resolver from the environment, if configured.
#[cfg(not(target_arch = "wasm32"))]
fn default_glyph_resolver() -> Option<std::sync::Arc<dyn crate::GlyphResolver>> {
let dir = std::env::var_os(FONT_DB_DIR_ENV)?;
if dir.is_empty() {
return None;
}
Some(std::sync::Arc::new(crate::FontDbResolver::new(dir)))
}
#[cfg(target_arch = "wasm32")]
fn default_glyph_resolver() -> Option<std::sync::Arc<dyn crate::GlyphResolver>> {
None
}
/// Main LiteParse orchestrator.
///
/// ### Thread safety
///
/// `LiteParse` is `Send + Sync` and safe to share across threads (e.g.
/// behind an `Arc`, or used concurrently from a multi-threaded `tokio`
/// runtime).
///
/// PDFium itself is **not** thread-safe, so all PDFium FFI work — document
/// loading, page rendering, text extraction — is serialized through a
/// process-global lock held by [`pdfium::Library`]. From a caller's
/// perspective, this means concurrent `parse_*` / `screenshot*` calls are
/// safe but their PDFium portions run sequentially. The OCR pass and grid
/// projection (which dominate runtime for OCR-heavy documents) run outside
/// the lock and remain fully concurrent.
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>>,
/// Optional caller-provided glyph recovery hook. When set, it is consulted
/// as a last resort for buggy/obfuscated-font glyphs that liteparse's
/// built-in cmap/AGL recovery could not decode. The published package ships
/// none; the platform build injects an outline → unicode font-DB resolver.
glyph_resolver: Option<std::sync::Arc<dyn crate::GlyphResolver>>,
}
impl LiteParse {
pub fn new(config: LiteParseConfig) -> Self {
Self {
config,
ocr_engine_override: None,
glyph_resolver: default_glyph_resolver(),
}
}
/// 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
}
/// Inject a glyph recovery hook. When set, glyphs that liteparse considers
/// untrusted and cannot decode with its built-in cmap/AGL recovery are
/// passed to the resolver as vector-outline segments for a final attempt.
pub fn with_glyph_resolver(
mut self,
resolver: std::sync::Arc<dyn crate::GlyphResolver>,
) -> Self {
self.glyph_resolver = Some(resolver);
self
}
/// Parse the configured `target_pages` string (e.g. `"1-5,10"`) into an
/// explicit page list, or `None` when no selection was configured.
fn resolve_target_pages(&self) -> Result<Option<Vec<u32>>, LiteParseError> {
self.config
.target_pages
.as_ref()
.map(|s| parse_target_pages(s))
.transpose()
.map_err(|e| format!("invalid --target-pages: {}", e).into())
}
fn validate_output_config(&self) -> Result<(), LiteParseError> {
if self.config.image_output_dir.is_some() && !self.config.effective_extract_images() {
return Err(LiteParseError::Config(
"image_output_dir requires extract_images = true (or image_mode = embed)"
.to_string(),
));
}
Ok(())
}
/// Determine the complexity of each page in a document, returning a vector
/// of `PageComplexityStats` for each page. This is useful for deciding
/// whether to enable OCR on a per-page basis, or for other heuristics.
///
/// Besides the OCR-need signals, each entry carries `layout` signals
/// (multi-column, ruled tables, dense graphics) computed by running the
/// real grid-projection pass — useful for routing pages to a
/// higher-accuracy pipeline even when no OCR is needed.
pub async fn is_complex(
&self,
input: PdfInput,
) -> Result<Vec<ocr_merge::PageComplexityStats>, LiteParseError> {
let log = |msg: &str| {
if !self.config.quiet {
eprintln!("{}", msg);
}
};
let t0 = web_time::Instant::now();
#[cfg(not(target_arch = "wasm32"))]
let (validated_input, _guard) =
conversion::resolve_pdf_input(input, self.config.password.as_deref(), false).await?;
#[cfg(target_arch = "wasm32")]
let validated_input = input;
// Determine which pages to extract
let target_pages = self.resolve_target_pages()?;
// Load the document and extract text items. Complexity signals derive
// from the text layer and page objects only — embedded image rasters
// and hyperlinks are irrelevant here, so both are skipped to keep this
// pass fast (its whole purpose is a cheap pre-OCR check).
let password = self.config.password.as_deref();
let (pages, mut page_complexities) = {
let lib = Library::init();
let document = extract::load_document_from_input(&lib, &validated_input, password)?;
let (pages, _, _) = extract::extract_pages_and_images(
&document,
target_pages.as_deref(),
self.config.max_pages,
false, // extract_links: irrelevant for complexity stats
self.glyph_resolver.as_deref(),
extract::ExtractionOutputOptions::default(),
)?;
let t_extract = web_time::Instant::now();
log(&format!(
"[liteparse] extract: {:.1}ms ({} pages)",
t_extract.duration_since(t0).as_secs_f64() * 1000.0,
pages.len()
));
let page_complexities = pages
.iter()
.map(|page| {
let page_obj = document.page((page.page_number - 1) as i32)?;
ocr_merge::calculate_page_complexity(page, &page_obj)
})
.collect::<Result<Vec<_>, _>>()?;
log(&format!(
"[liteparse] complexity: {:.1}ms",
web_time::Instant::now()
.duration_since(t_extract)
.as_secs_f64()
* 1000.0
));
// `lib` is dropped here, releasing the PDFium lock; the layout
// pass below is pure CPU over the already-extracted items.
(pages, page_complexities)
};
// Layout signals come from the real projection pass so they match
// what a full parse will decide.
let t_layout = web_time::Instant::now();
let parsed_pages = projection::project_pages_to_grid(pages);
for (stats, page) in page_complexities.iter_mut().zip(&parsed_pages) {
stats.layout = Some(ocr_merge::calculate_layout_complexity(page));
}
log(&format!(
"[liteparse] layout: {:.1}ms",
web_time::Instant::now()
.duration_since(t_layout)
.as_secs_f64()
* 1000.0
));
Ok(page_complexities)
}
/// 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();
self.validate_output_config()?;
#[cfg(not(target_arch = "wasm32"))]
let (validated_input, _guard) =
conversion::resolve_pdf_input(input, self.config.password.as_deref(), false).await?;
#[cfg(target_arch = "wasm32")]
let validated_input = input;
// Determine which pages to extract
let target_pages = self.resolve_target_pages()?;
// Extract text (and pre-render OCR pages in one PDF load when OCR is on).
// The PDFium lock is acquired for this entire critical section and
// released before any `.await` below — OCR (network / CPU) and grid
// projection (pure Rust) do not touch PDFium, so they can run
// concurrently with other `LiteParse` calls.
let password = self.config.password.as_deref();
// Build the OCR engine up front so the renderer knows whether to emit a
// grayscale buffer (cheaper, for engines that binarize internally) or RGB.
let ocr_engine: Option<std::sync::Arc<dyn OcrEngine>> = if self.config.ocr_enabled {
Some(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::with_headers(
url.clone(),
self.config.ocr_server_headers.clone(),
)
.with_retry(
crate::ocr::http_simple::OcrRetryConfig {
hedge_delays_ms: self.config.ocr_hedge_delays_ms.clone(),
..Default::default()
},
),
)
} 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(),
);
}
})
} else {
None
};
let ocr_grayscale = ocr_engine.as_ref().is_some_and(|e| e.prefers_grayscale());
#[allow(unused_mut)] // mutated only by the native image-output writer
let (
pages,
ocr_rendered,
outline,
mut images,
image_error_count,
complexity,
form_type,
creator,
producer,
xfa_packets,
) = {
let lib = Library::init();
#[cfg(not(target_arch = "wasm32"))]
let repaired_input = self
.config
.extract_form_fields
.then(|| {
crate::acroform_repair::repair_orphaned_widgets(
&lib,
&validated_input,
password,
)
})
.flatten();
#[cfg(not(target_arch = "wasm32"))]
let document_input = repaired_input.as_ref().unwrap_or(&validated_input);
#[cfg(target_arch = "wasm32")]
let document_input = &validated_input;
let document = extract::load_document_from_input(&lib, document_input, password)?;
let form_type = self
.config
.extract_form_fields
.then(|| document.form_type());
let creator = document.meta_text("Creator");
let producer = document.meta_text("Producer");
let xfa_packets = self.config.extract_xfa_packets.then(|| {
document
.xfa_packets()
.into_iter()
.map(|packet| XfaPacket {
index: packet.index.max(0) as u32,
name: packet.name,
content_length: packet
.content
.as_ref()
.map_or(0, |content| content.len() as u32),
content: packet
.content
.map(|bytes| String::from_utf8_lossy(&bytes).into_owned()),
})
.collect::<Vec<_>>()
});
let outline = extract::extract_outline(&document);
let (pages, images, image_error_count) = extract::extract_pages_and_images(
&document,
target_pages.as_deref(),
self.config.max_pages,
self.config.extract_links
&& self.config.output_format == crate::config::OutputFormat::Markdown,
self.glyph_resolver.as_deref(),
extract::ExtractionOutputOptions {
extract_content_bounds: self.config.extract_content_bounds,
extract_images: self.config.effective_extract_images(),
emit_word_boxes: self.config.emit_word_boxes,
extract_text_metadata: self.config.extract_text_metadata,
extract_vector_graphics: self.config.extract_vector_graphics,
extract_annotations: self.config.extract_annotations,
extract_form_fields: self.config.extract_form_fields,
extract_structure_tree: self.config.extract_structure_tree,
},
)?;
let t_extract = web_time::Instant::now();
log(&format!(
"[liteparse] extract: {:.1}ms ({} pages)",
t_extract.duration_since(t0).as_secs_f64() * 1000.0,
pages.len()
));
let rendered = if self.config.ocr_enabled {
let r = ocr_merge::render_pages_for_ocr(
&document,
&pages,
self.config.dpi,
ocr_grayscale,
self.config.render_form_fields,
)?;
log(&format!(
"[liteparse] ocr render: {:.1}ms ({} pages)",
web_time::Instant::now()
.duration_since(t_extract)
.as_secs_f64()
* 1000.0,
r.len()
));
r
} else {
Vec::new()
};
let complexity = if self.config.include_complexity {
pages
.iter()
.map(|page| {
let page_obj = document.page((page.page_number - 1) as i32)?;
ocr_merge::calculate_page_complexity(page, &page_obj)
})
.collect::<Result<Vec<_>, _>>()?
} else {
Vec::new()
};
// `lib` is dropped here, releasing the PDFium lock.
(
pages,
rendered,
outline,
images,
image_error_count,
complexity,
form_type,
creator,
producer,
xfa_packets,
)
};
let mut pages = pages;
let t1 = web_time::Instant::now();
// OCR pass (engine resolved before the render block above).
if let Some(engine) = ocr_engine {
ocr_merge::ocr_and_merge_rendered(
&mut pages,
ocr_rendered,
engine,
&self.config.ocr_language,
self.config.num_workers,
self.config.ocr_failure_fatal,
)
.await?;
}
let t_ocr = web_time::Instant::now();
log(&format!(
"[liteparse] ocr: {:.1}ms",
t_ocr.duration_since(t1).as_secs_f64() * 1000.0
));
// Caller-requested content filters (page-region crop, diagonal-text
// removal). Runs after OCR merge so it also drops OCR text outside the
// crop region, and before projection so filtered items never surface.
extract::apply_content_filters(
&mut pages,
self.config.crop_box.as_ref(),
self.config.skip_diagonal_text,
);
// Grid projection
let mut parsed_pages = projection::project_pages_to_grid(pages);
// Attach per-page complexity signals, including the layout signals
// that need the projected page (same as `is_complex()` reports).
for (page, mut stats) in parsed_pages.iter_mut().zip(complexity) {
stats.layout = Some(ocr_merge::calculate_layout_complexity(page));
page.complexity = Some(stats);
}
let t2 = web_time::Instant::now();
log(&format!(
"[liteparse] project: {:.1}ms",
t2.duration_since(t_ocr).as_secs_f64() * 1000.0
));
let mut full_text = if self.config.output_format == crate::config::OutputFormat::Markdown {
let page_md =
markdown::format_markdown_pages(&parsed_pages, &outline, self.config.image_mode);
let md = page_md.join("\n\n-----\n\n");
for (page, md) in parsed_pages.iter_mut().zip(page_md) {
page.markdown = md;
}
let t3 = web_time::Instant::now();
log(&format!(
"[liteparse] markdown: {:.1}ms",
t3.duration_since(t2).as_secs_f64() * 1000.0
));
md
} else {
parsed_pages
.iter()
.map(|p| p.text.as_str())
.collect::<Vec<_>>()
.join("\n\n")
};
if self.config.output_format == crate::config::OutputFormat::Markdown {
rewrite_duplicate_image_refs(&mut parsed_pages, &mut full_text, &images);
}
let total = web_time::Instant::now().duration_since(t0).as_secs_f64() * 1000.0;
log(&format!("[liteparse] total: {:.1}ms", total));
#[cfg(not(target_arch = "wasm32"))]
if self.config.effective_extract_images()
&& let Some(output_dir) = self.config.image_output_dir.as_deref()
{
write_extracted_images(output_dir, &mut images)?;
}
Ok(ParseResult {
pages: parsed_pages,
text: full_text,
outline,
images,
image_error_count,
form_type,
creator,
producer,
xfa_packets,
})
}
/// Parse from pre-extracted pages, skipping PDFium text extraction.
///
/// The caller supplies `Page`s already populated with text items (and,
/// optionally, graphics / struct nodes / image refs) in viewport space
/// (top-left origin, 72 DPI). This runs only grid projection and the
/// configured output formatter, so it touches neither PDFium nor OCR and
/// is fully synchronous. Used when an external extractor (e.g. with its
/// own font-recovery pipeline) owns text extraction.
pub fn parse_from_pages(&self, pages: Vec<Page>, outline: Vec<OutlineTarget>) -> ParseResult {
let mut parsed_pages = projection::project_pages_to_grid(pages);
let full_text = if self.config.output_format == crate::config::OutputFormat::Markdown {
let page_md =
markdown::format_markdown_pages(&parsed_pages, &outline, self.config.image_mode);
let md = page_md.join("\n\n-----\n\n");
for (page, md) in parsed_pages.iter_mut().zip(page_md) {
page.markdown = md;
}
md
} else {
parsed_pages
.iter()
.map(|p| p.text.as_str())
.collect::<Vec<_>>()
.join("\n\n")
};
ParseResult {
pages: parsed_pages,
text: full_text,
outline,
images: Vec::new(),
image_error_count: 0,
form_type: None,
creator: None,
producer: None,
xfa_packets: None,
}
}
/// Generate screenshots of document pages as PNG bytes.
///
/// Non-PDF files are automatically converted to PDF first (requires
/// LibreOffice/ImageMagick on the system). Plain-text formats cannot be
/// rendered and return a clear error.
#[cfg(not(target_arch = "wasm32"))]
pub async fn screenshot(
&self,
input: &str,
page_numbers: Option<Vec<u32>>,
) -> Result<Vec<ScreenshotResult>, LiteParseError> {
self.screenshot_input(PdfInput::Path(input.to_string()), page_numbers)
.await
}
/// Generate screenshots from a file path or raw bytes.
#[cfg(not(target_arch = "wasm32"))]
pub async fn screenshot_input(
&self,
input: PdfInput,
page_numbers: Option<Vec<u32>>,
) -> Result<Vec<ScreenshotResult>, LiteParseError> {
let log = |msg: &str| {
if !self.config.quiet {
eprintln!("{}", msg);
}
};
let (validated_input, _guard) =
conversion::resolve_pdf_input(input, self.config.password.as_deref(), true).await?;
if let PdfInput::Path(ref path) = validated_input
&& !conversion::is_pdf(path)
{
log("[liteparse] converted input to PDF for screenshot rendering");
}
let rendered = render::render_pages_to_png(
&validated_input,
page_numbers.as_deref(),
self.config.dpi,
self.config.password.as_deref(),
self.config.detect_screenshot_rects,
self.config.render_form_fields,
)?;
Ok(rendered
.into_iter()
.map(|page| ScreenshotResult {
page_num: page.page_num,
width: page.width,
height: page.height,
image_bytes: page.png_bytes,
is_solid_fill: page.is_solid_fill,
rects: page.rects,
})
.collect())
}
pub fn config(&self) -> &LiteParseConfig {
&self.config
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{Page, TextItem};
fn page_with_text_metadata() -> Page {
Page {
page_number: 1,
page_width: 100.0,
page_height: 100.0,
content_bounds: None,
text_items: vec![TextItem {
text: "hello".into(),
width: 20.0,
height: 10.0,
font_name: Some("Helvetica".into()),
font_size: Some(10.0),
font_height: Some(10.0),
font_ascent: Some(8.0),
font_descent: Some(-2.0),
font_weight: Some(700),
text_width: Some(19.0),
font_is_buggy: true,
mcid: Some(3),
fill_color: Some("ff112233".into()),
stroke_color: Some("ff445566".into()),
char_codes: vec![104, 101, 108, 108, 111],
trailing_space_generated: true,
..Default::default()
}],
graphics: vec![],
vector_graphics: None,
struct_nodes: vec![],
image_refs: vec![],
annotations: None,
form_fields: None,
structure_tree: None,
}
}
#[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);
}
#[test]
fn parse_from_pages_preserves_internal_text_metadata() {
let result = LiteParse::new(LiteParseConfig::default())
.parse_from_pages(vec![page_with_text_metadata()], vec![]);
let item = &result.pages[0].text_items[0];
assert_eq!(item.font_name.as_deref(), Some("Helvetica"));
assert_eq!(item.font_size, Some(10.0));
assert_eq!(item.font_height, Some(10.0));
assert_eq!(item.font_ascent, Some(8.0));
assert_eq!(item.font_descent, Some(-2.0));
assert_eq!(item.font_weight, Some(700));
assert_eq!(item.text_width, Some(19.0));
assert!(item.font_is_buggy);
assert_eq!(item.mcid, Some(3));
assert_eq!(item.fill_color.as_deref(), Some("ff112233"));
assert_eq!(item.stroke_color.as_deref(), Some("ff445566"));
assert_eq!(item.char_codes, vec![104, 101, 108, 108, 111]);
assert!(item.trailing_space_generated);
}
#[test]
fn image_extraction_is_opt_in_but_embed_mode_implies_it() {
let default = LiteParseConfig::default();
assert!(!default.effective_extract_images());
// `image_mode = embed` predates `extract_images` and must keep
// extracting bytes for existing callers.
let embed = LiteParseConfig {
image_mode: crate::config::ImageMode::Embed,
..Default::default()
};
assert!(embed.effective_extract_images());
let explicit = LiteParseConfig {
extract_images: true,
..Default::default()
};
assert!(explicit.effective_extract_images());
}
#[test]
fn image_output_dir_requires_image_extraction() {
let parser = LiteParse::new(LiteParseConfig {
image_output_dir: Some("images".into()),
..Default::default()
});
assert_eq!(
parser.validate_output_config().unwrap_err().to_string(),
"invalid config: image_output_dir requires extract_images = true (or image_mode = embed)"
);
let embed = LiteParse::new(LiteParseConfig {
image_mode: crate::config::ImageMode::Embed,
image_output_dir: Some("images".into()),
..Default::default()
});
assert!(embed.validate_output_config().is_ok());
}
#[test]
fn image_output_writes_duplicates_from_canonical_bytes() {
fn image(id: &str, duplicate_of: Option<&str>, bytes: &[u8]) -> ExtractedImage {
ExtractedImage {
id: id.into(),
name: format!("img_{id}.png"),
path: None,
page: 1,
bbox: crate::types::Rect::default(),
width: 2,
height: 2,
rotation: 0.0,
format: "png".into(),
duplicate_of: duplicate_of.map(str::to_owned),
bytes: std::sync::Arc::new(bytes.to_vec()),
}
}
let dir = tempfile::tempdir().unwrap();
let mut images = vec![
image("p1_1", None, b"canonical"),
image("p2_1", Some("p1_1"), b"canonical"),
];
write_extracted_images(dir.path().to_str().unwrap(), &mut images).unwrap();
// Platform contract: one file on disk; the duplicate keeps its own
// placement `name` but shares the canonical file's `path`.
assert_eq!(images[0].name, "img_p1_1.png");
assert_eq!(images[1].name, "img_p2_1.png");
assert_eq!(images[0].path, images[1].path);
assert_eq!(
std::fs::read(images[0].path.as_ref().unwrap()).unwrap(),
b"canonical"
);
assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 1);
}
#[test]
fn duplicate_image_markdown_refs_are_rewritten_to_canonical() {
fn image(id: &str, format: &str, duplicate_of: Option<&str>) -> ExtractedImage {
ExtractedImage {
id: id.into(),
name: format!("img_{id}.{format}"),
path: None,
page: 1,
bbox: crate::types::Rect::default(),
width: 2,
height: 2,
rotation: 0.0,
format: format.into(),
duplicate_of: duplicate_of.map(str::to_owned),
bytes: std::sync::Arc::new(Vec::new()),
}
}
let mut pages = vec\n\noutro".into(),
text_items: vec![],
projected_lines: vec![],
regions: crate::types::Region::default(),
graphics: vec![],
vector_graphics: None,
figures: vec![],
struct_nodes: vec![],
image_refs: vec![],
complexity: None,
annotations: None,
form_fields: None,
structure_tree: None,
}];
let mut full_text = pages[0].markdown.clone();
let images = vec![
image("p1_1", "jpg", None),
image("p2_1", "jpg", Some("p1_1")),
];
rewrite_duplicate_image_refs(&mut pages, &mut full_text, &images);
// The duplicate's ref now points at the canonical file; canonical
// refs and surrounding text are untouched.
assert_eq!(pages[0].markdown, "intro\n\n\n\noutro");
assert_eq!(full_text, pages[0].markdown);
}
}