spectre_pdf 1.0.0

Native Rust PDF extraction engine: text, markdown for RAG, AcroForm widgets, image decoding, and encrypted PDFs. Lazy parser, persistent Document handle, no C dependencies.
Documentation
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
//! Read-only structural extraction: TOC, links, annotations, images, page
//! info. Thin walkers over the loaded object graph — no content-stream
//! parsing (that's [`crate::positioned`]).

use crate::geom::Rect;
use crate::ExtractError;
use std::collections::HashMap;

// ── TOC ─────────────────────────────────────────────────────────────────────

/// One entry in the document outline. `level` is 1-indexed depth; `page`
/// is the 1-indexed target page, or `None` for unresolvable destinations
/// (usually external `GoToR`).
#[derive(Debug, Clone, PartialEq)]
pub struct TocEntry {
    pub level: usize,
    pub title: String,
    pub page: Option<u32>,
}

/// Walk the outline tree and return a flattened list. Returns `Ok(vec![])`
/// for outlineless PDFs so callers can use `if !toc.is_empty()`.
pub fn extract_toc_impl(pdf_bytes: &[u8]) -> Result<Vec<TocEntry>, ExtractError> {
    let doc = crate::document::open_sp_with_password(pdf_bytes, b"")?;
    extract_toc_from_sp(&doc)
}

// ── Annotations (read-only) ─────────────────────────────────────────────────

/// One annotation. `subtype` is the raw PDF subtype string (`Link`,
/// `Highlight`, `FreeText`, `Stamp`, `Widget`, `Ink`, …) — unnormalized
/// so downstream filters can match the exact set they care about.
#[derive(Debug, Clone, PartialEq)]
pub struct Annotation {
    pub page: u32,
    pub subtype: String,
    pub rect: Option<Rect>,
    /// `/Contents`. Empty for subtypes that don't carry text.
    pub contents: String,
    /// `/T` (author / title bar). Empty when absent.
    pub author: String,
}

pub fn extract_annotations_impl(
    pdf_bytes: &[u8],
    page_filter: Option<u32>,
) -> Result<Vec<Annotation>, ExtractError> {
    let doc = crate::document::open_sp_with_password(pdf_bytes, b"")?;
    Ok(extract_annotations_from_sp(&doc, page_filter))
}

// ── Links ───────────────────────────────────────────────────────────────────

/// One hyperlink. External `URI` actions populate `uri`; intra-document
/// `GoTo` actions populate `target_page` (1-indexed).
#[derive(Debug, Clone, PartialEq)]
pub struct Link {
    pub page: u32,
    pub rect: Option<Rect>,
    /// URL for `/A /S /URI`, or the named-destination string for unresolved
    /// `GoToR` references. Empty when `target_page` is set.
    pub uri: String,
    pub target_page: Option<u32>,
}

pub fn extract_links_impl(
    pdf_bytes: &[u8],
    page_filter: Option<u32>,
) -> Result<Vec<Link>, ExtractError> {
    let doc = crate::document::open_sp_with_password(pdf_bytes, b"")?;
    Ok(extract_links_from_sp(&doc, page_filter))
}

// ── Images (inventory) ──────────────────────────────────────────────────────

/// Image XObject inventory entry. We deliberately do not return the
/// decoded image — that would pull in JBIG2/JPEG2000/Flate/DCTDecode
/// (large C deps). The metadata is enough to drive routing ("is this
/// page a scan we should OCR?").
#[derive(Debug, Clone, PartialEq)]
pub struct ImageInfo {
    pub page: u32,
    /// PDF object number; gen is dropped since it's ~always 0 for images.
    pub xref: u32,
    pub width: u32,
    pub height: u32,
    pub color_space: Option<String>,
    pub bits_per_component: Option<u32>,
    /// Filter chain (e.g. `["ASCII85Decode", "FlateDecode"]`). Empty when
    /// the stream declares no filter.
    pub filters: Vec<String>,
    /// Encoded stream byte size — for "skip pages with >5 MB images"
    /// routing without decoding.
    pub size_bytes: usize,
}

pub fn extract_images_impl(
    pdf_bytes: &[u8],
    page_filter: Option<u32>,
) -> Result<Vec<ImageInfo>, ExtractError> {
    let doc = crate::document::open_sp_with_password(pdf_bytes, b"")?;
    Ok(extract_images_from_sp(&doc, page_filter))
}

// ── Page info ───────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq)]
pub struct PageInfo {
    /// 1-indexed.
    pub number: u32,
    /// Width in points (1/72"), post-`Rotate`.
    pub width: f32,
    pub height: f32,
    /// 0 / 90 / 180 / 270, inherited from parent.
    pub rotation: i32,
    /// Raw `[x0, y0, x1, y1]` (unrotated). Falls back to US Letter when
    /// absent (PDF §14.11.2 spec behaviour).
    pub mediabox: Rect,
    pub cropbox: Rect,
}

pub fn extract_page_info_impl(
    pdf_bytes: &[u8],
    page_filter: Option<u32>,
) -> Result<Vec<PageInfo>, ExtractError> {
    let doc = crate::document::open_sp_with_password(pdf_bytes, b"")?;
    Ok(extract_page_info_from_sp(&doc, page_filter))
}

// ── Document info ───────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq)]
pub struct DocumentInfo {
    pub page_count: u32,
    pub pdf_version: String,
    pub is_encrypted: bool,
    pub is_linearized: bool,
    pub xref_count: u32,
    pub trailer_id: Option<String>,
}

pub fn extract_document_info_impl(pdf_bytes: &[u8]) -> Result<DocumentInfo, ExtractError> {
    let doc = crate::document::open_sp_with_password(pdf_bytes, b"")?;
    Ok(extract_document_info_from_sp(&doc))
}

fn hex_encode(bytes: &[u8]) -> String {
    let mut s = String::with_capacity(bytes.len() * 2);
    for &b in bytes {
        s.push_str(&format!("{b:02x}"));
    }
    s
}

pub(crate) fn decode_pdf_string(bytes: &[u8]) -> String {
    if bytes.len() >= 2 && bytes[0] == 0xfe && bytes[1] == 0xff {
        let utf16: Vec<u16> = bytes[2..]
            .chunks_exact(2)
            .map(|c| u16::from_be_bytes([c[0], c[1]]))
            .collect();
        String::from_utf16_lossy(&utf16)
    } else if bytes.len() >= 2 && bytes[0] == 0xff && bytes[1] == 0xfe {
        let utf16: Vec<u16> = bytes[2..]
            .chunks_exact(2)
            .map(|c| u16::from_le_bytes([c[0], c[1]]))
            .collect();
        String::from_utf16_lossy(&utf16)
    } else {
 // PDFDocEncoding is a superset of WinAnsiEncoding for printable ASCII;
 // the lossy String::from_utf8_lossy round-trip preserves Latin-1
        // identical bytes, which covers the vast majority of /Contents fields.
        String::from_utf8_lossy(bytes).into_owned()
    }
}

use spectre_parse::{
    Dictionary as SpDictionary, Document as SpDocument, Object as SpObject,
    ObjectId as SpObjectId,
};

pub(crate) fn extract_document_info_from_sp(doc: &SpDocument) -> DocumentInfo {
    let trailer = doc.trailer();
    let is_encrypted = trailer.get_optional(b"Encrypt").is_some();
    let page_count = doc.get_pages().len() as u32;
    let pdf_version = doc.version().to_string();
    let xref_count = doc.xref_size();
    let is_linearized = sp_detect_linearized(doc);
    let trailer_id = trailer
        .get_optional(b"ID")
        .and_then(|o| match o {
            SpObject::Array(a) => a.first().cloned(),
            _ => None,
        })
        .and_then(|first| match first {
            SpObject::String(b, _) => Some(hex_encode(&b)),
            _ => None,
        });
    DocumentInfo {
        page_count,
        pdf_version,
        is_encrypted,
        is_linearized,
        xref_count,
        trailer_id,
    }
}

fn sp_detect_linearized(doc: &SpDocument) -> bool {
    if let Ok(obj) = doc.get_object((1, 0)) {
        return match obj {
            SpObject::Dictionary(d) => d.get_optional(b"Linearized").is_some(),
            SpObject::Stream(s) => s.dict.get_optional(b"Linearized").is_some(),
            _ => false,
        };
    }
    false
}

pub(crate) fn extract_page_info_from_sp(
    doc: &SpDocument,
    page_filter: Option<u32>,
) -> Vec<PageInfo> {
    let mut out = Vec::new();
    let pages = doc.get_pages();
    for (page_num, page_id) in pages {
        if let Some(filter) = page_filter {
            if filter != page_num {
                continue;
            }
        }
        let Ok(dict) = doc.get_dictionary(page_id) else {
            continue;
        };
        let mediabox = sp_inherit_rect(doc, &dict, b"MediaBox")
            .unwrap_or(Rect::new(0.0, 0.0, 612.0, 792.0));
        let cropbox = sp_inherit_rect(doc, &dict, b"CropBox").unwrap_or(mediabox);
        let rotation = sp_inherit_int(doc, &dict, b"Rotate").unwrap_or(0);
        let (mut width, mut height) = (mediabox.width(), mediabox.height());
        if rotation == 90 || rotation == 270 {
            std::mem::swap(&mut width, &mut height);
        }
        out.push(PageInfo {
            number: page_num,
            width,
            height,
            rotation,
            mediabox,
            cropbox,
        });
    }
    out
}

fn sp_inherit_rect(doc: &SpDocument, dict: &SpDictionary, key: &[u8]) -> Option<Rect> {
    let mut current = Some(dict.clone());
    for _ in 0..32 {
        let Some(d) = current else {
            break;
        };
        if let Some(obj) = d.get_optional(key) {
            return sp_rect_from_array(obj);
        }
        current = d
            .get_optional(b"Parent")
            .and_then(|o| o.as_reference().ok())
            .and_then(|id| doc.get_dictionary(id).ok());
    }
    None
}

fn sp_inherit_int(doc: &SpDocument, dict: &SpDictionary, key: &[u8]) -> Option<i32> {
    let mut current = Some(dict.clone());
    for _ in 0..32 {
        let Some(d) = current else {
            break;
        };
        if let Some(obj) = d.get_optional(key) {
            if let Ok(n) = obj.as_i64() {
                return Some(n as i32);
            }
        }
        current = d
            .get_optional(b"Parent")
            .and_then(|o| o.as_reference().ok())
            .and_then(|id| doc.get_dictionary(id).ok());
    }
    None
}

fn sp_rect_from_array(obj: &SpObject) -> Option<Rect> {
    let arr = obj.as_array().ok()?;
    if arr.len() < 4 {
        return None;
    }
    let v: Vec<f32> = arr.iter().take(4).filter_map(|o| o.as_float().ok()).collect();
    if v.len() < 4 {
        return None;
    }
    Some(Rect::new(v[0], v[1], v[2], v[3]))
}

pub(crate) fn extract_toc_from_sp(doc: &SpDocument) -> Result<Vec<TocEntry>, ExtractError> {
    match doc.get_toc() {
        Ok(entries) => Ok(entries
            .into_iter()
            .map(|t| TocEntry {
                level: t.level,
                title: t.title,
                page: t.page,
            })
            .collect()),
        Err(spectre_parse::Error::NoOutline) => Ok(Vec::new()),
        Err(spectre_parse::Error::DictKey(ref k)) if k == "Outlines" => Ok(Vec::new()),
        Err(e) => Err(ExtractError::ParseFailed(e.to_string())),
    }
}

pub(crate) fn extract_links_from_sp(doc: &SpDocument, page_filter: Option<u32>) -> Vec<Link> {
    let page_id_to_num: HashMap<SpObjectId, u32> = doc
        .get_pages()
        .into_iter()
        .map(|(n, id)| (id, n))
        .collect();
    let mut out = Vec::new();
    for (page_num, page_id) in doc.get_pages() {
        if let Some(filter) = page_filter {
            if filter != page_num {
                continue;
            }
        }
        for a in doc.get_page_annotations(page_id) {
            let is_link = a
                .get_optional(b"Subtype")
                .and_then(|o| o.as_name().ok())
                .map(|n| n == b"Link")
                .unwrap_or(false);
            if !is_link {
                continue;
            }
            let rect = a.get_optional(b"Rect").and_then(sp_rect_from_array);
            let (uri, target_page) = sp_resolve_link_target(doc, &a, &page_id_to_num);
            out.push(Link {
                page: page_num,
                rect,
                uri,
                target_page,
            });
        }
    }
    out
}

fn sp_resolve_link_target(
    doc: &SpDocument,
    annot: &SpDictionary,
    page_id_to_num: &HashMap<SpObjectId, u32>,
) -> (String, Option<u32>) {
    if let Some(action_obj) = annot.get_optional(b"A") {
        let action = match action_obj {
            SpObject::Dictionary(d) => Some(d.clone()),
            SpObject::Reference(id) => doc.get_dictionary(*id).ok(),
            _ => None,
        };
        if let Some(action) = action {
            let s = action
                .get_optional(b"S")
                .and_then(|o| o.as_name().ok())
                .unwrap_or(b"");
            match s {
                b"URI" => {
                    if let Some(uri) =
                        action.get_optional(b"URI").and_then(sp_object_to_text)
                    {
                        return (uri, None);
                    }
                }
                b"GoTo" => {
                    if let Some(d) = action.get_optional(b"D") {
                        return sp_resolve_dest(doc, d, page_id_to_num);
                    }
                }
                _ => {}
            }
        }
    }
    if let Some(dest) = annot.get_optional(b"Dest") {
        return sp_resolve_dest(doc, dest, page_id_to_num);
    }
    (String::new(), None)
}

fn sp_resolve_dest(
    doc: &SpDocument,
    dest: &SpObject,
    page_id_to_num: &HashMap<SpObjectId, u32>,
) -> (String, Option<u32>) {
    match dest {
        SpObject::Array(arr) => {
            if let Some(first) = arr.first() {
                if let Ok(id) = first.as_reference() {
                    if let Some(&pn) = page_id_to_num.get(&id) {
                        return (String::new(), Some(pn));
                    }
                }
            }
            (String::new(), None)
        }
        // Named destination via the catalog's /Names/Dests tree (Adobe-
        // toolchain PDFs use this for nearly all intra-doc links).
        SpObject::String(b, _) => {
            let name_str = String::from_utf8_lossy(b).into_owned();
            if let Some(page) = doc.resolve_destination_to_page(dest) {
                (String::new(), Some(page))
            } else {
                (name_str, None)
            }
        }
        SpObject::Reference(id) => doc
            .get_object(*id)
            .ok()
            .map(|o| sp_resolve_dest(doc, &o, page_id_to_num))
            .unwrap_or((String::new(), None)),
        _ => (String::new(), None),
    }
}

fn sp_object_to_text(obj: &SpObject) -> Option<String> {
    match obj {
        SpObject::String(bytes, _) => Some(decode_pdf_string(bytes)),
        _ => None,
    }
}

pub(crate) fn extract_annotations_from_sp(
    doc: &SpDocument,
    page_filter: Option<u32>,
) -> Vec<Annotation> {
    let mut out = Vec::new();
    for (page_num, page_id) in doc.get_pages() {
        if let Some(filter) = page_filter {
            if filter != page_num {
                continue;
            }
        }
        for a in doc.get_page_annotations(page_id) {
            let subtype = a
                .get_optional(b"Subtype")
                .and_then(|o| o.as_name().ok())
                .map(|n| String::from_utf8_lossy(n).into_owned())
                .unwrap_or_default();
            if subtype == "Link" || subtype == "Widget" {
                continue;
            }
            let rect = a.get_optional(b"Rect").and_then(sp_rect_from_array);
            let contents = a
                .get_optional(b"Contents")
                .and_then(sp_object_to_text)
                .unwrap_or_default();
            let author = a
                .get_optional(b"T")
                .and_then(sp_object_to_text)
                .unwrap_or_default();
            out.push(Annotation {
                page: page_num,
                subtype,
                rect,
                contents,
                author,
            });
        }
    }
    out
}

pub(crate) fn extract_images_from_sp(
    doc: &SpDocument,
    page_filter: Option<u32>,
) -> Vec<ImageInfo> {
    let mut out = Vec::new();
    for (page_num, page_id) in doc.get_pages() {
        if let Some(filter) = page_filter {
            if filter != page_num {
                continue;
            }
        }
        for img in doc.get_page_images(page_id) {
            out.push(ImageInfo {
                page: page_num,
                xref: img.id.0,
                width: img.width.max(0) as u32,
                height: img.height.max(0) as u32,
                color_space: img.color_space,
                bits_per_component: img.bits_per_component.map(|n| n.max(0) as u32),
                filters: img.filters,
                size_bytes: img.content_len,
            });
        }
    }
    out
}