pdf2 1.7.0

A PDF 2.0 (ISO 32000-2) reader, writer and text extractor.
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
501
502
503
504
505
506
507
508
509
510
511
512
513
//! Content-stream interpretation and text extraction (Q7, M7).
//!
//! `tests/fixtures/text.pdf` has an uncompressed `/Contents` stream that walks
//! through the text operators (`Tj`, `TJ` with a gap, `'`, `TL` + `T*`, `Tm`),
//! two fonts (WinAnsiEncoding and a `/Differences` array) and therefore covers
//! the whole extraction path without any geometry guessing.

use pdf2::Document;

const TEXT: &[u8] = include_bytes!("fixtures/text.pdf");
const PAGE_TREE: &[u8] = include_bytes!("fixtures/page-tree.pdf");
const TWIN: &[u8] = include_bytes!("fixtures/classic-twin.pdf");
const XREF_STREAM: &[u8] = include_bytes!("fixtures/xref-stream.pdf");

fn open(bytes: &[u8]) -> Document {
    Document::open(bytes).expect("fixture opens")
}

fn doc(objects: &[(u32, &str)]) -> Document {
    Document::open(pdf(objects)).expect("document opens")
}

/// Assemble a classic-xref PDF around `(object number, body)` pairs.
fn pdf(objects: &[(u32, &str)]) -> Vec<u8> {
    let mut out = b"%PDF-2.0\n".to_vec();
    let mut offsets = Vec::new();
    for (number, body) in objects {
        offsets.push((*number, out.len()));
        out.extend_from_slice(format!("{number} 0 obj\n{body}\nendobj\n").as_bytes());
    }
    let xref = out.len();
    out.extend_from_slice(b"xref\n");
    for (number, offset) in &offsets {
        out.extend_from_slice(format!("{number} 1\n{offset:010} 00000 n \n").as_bytes());
    }
    let size = objects.iter().map(|(number, _)| *number).max().unwrap_or(0) + 1;
    out.extend_from_slice(format!("trailer\n<< /Size {size} /Root 1 0 R >>\n").as_bytes());
    out.extend_from_slice(format!("startxref\n{xref}\n%%EOF\n").as_bytes());
    out
}

#[test]
fn a_real_fixture_extracts_its_text() {
    let document = open(TEXT);
    let page = document.page(0).expect("page");
    let text = document.extract_text(&page).expect("text");
    assert_eq!(
        text, "Hello, PDF 2.0\nSecond line\nspaced out\nfourth line\n\u{2022} dot\ncaf\u{e9} ok",
        "newlines come from Td/T*/Tm, the space in the TJ array from its -300 adjustment, \
         the bullet from /Differences and the accent from WinAnsiEncoding"
    );
}

#[test]
fn the_content_stream_of_a_page_is_uncompressed_and_reachable() {
    let document = open(TEXT);
    let page = document.page(0).expect("page");
    let content = document.page_content(&page).expect("content");
    assert!(content.starts_with(b"BT\n"));
    assert!(
        content.windows(12).any(|window| window == b"(Second line"),
        "the fixture is meant to be readable: {}",
        String::from_utf8_lossy(&content)
    );
}

#[test]
fn other_fixtures_extract_text_too() {
    let document = open(PAGE_TREE);
    let page = document.page(0).expect("page");
    assert_eq!(
        document.extract_text(&page).expect("page-tree text"),
        "page one"
    );

    let document = open(TWIN);
    let page = document.page(0).expect("page");
    assert_eq!(
        document.extract_text(&page).expect("twin text"),
        "Hello, PDF 2.0"
    );

    // The xref-stream fixture keeps its page inside an object stream.
    let document = open(XREF_STREAM);
    let page = document.page(0).expect("page");
    assert_eq!(
        document.extract_text(&page).expect("xref-stream text"),
        "Hello, PDF 2.0"
    );
}

fn text_of(content: &str) -> String {
    let document = doc(&[
        (1, "<< /Type /Catalog /Pages 2 0 R >>"),
        (2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>"),
        (
            3,
            "<< /Type /Page /MediaBox [0 0 612 792] /Contents 4 0 R \
             /Resources << /Font << /F1 5 0 R >> >> >>",
        ),
        (5, "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>"),
    ]);
    let resources = document
        .page(0)
        .expect("page")
        .resources
        .expect("resources");
    document
        .extract_text_from(content.as_bytes(), Some(&resources))
        .expect("text")
}

#[test]
fn tj_adjustments_only_become_spaces_when_they_are_wide() {
    assert_eq!(
        text_of("BT /F1 12 Tf 10 700 Td [(A) -300 (B)] TJ ET"),
        "A B"
    );
    assert_eq!(text_of("BT /F1 12 Tf 10 700 Td [(A) -100 (B)] TJ ET"), "AB");
    assert_eq!(
        text_of("BT /F1 12 Tf 10 700 Td [(A) -1000 (B)] TJ ET"),
        "A B"
    );
}

#[test]
fn line_breaks_come_from_positioning_operators() {
    assert_eq!(
        text_of("BT /F1 12 Tf 10 700 Td (one) Tj 0 -20 Td (two) Tj ET"),
        "one\ntwo"
    );
    assert_eq!(
        text_of("BT /F1 12 Tf 10 700 Td (one) Tj 10 0 Td (two) Tj ET"),
        "onetwo",
        "moving along the line does not break it"
    );
    assert_eq!(
        text_of("BT /F1 12 Tf 12 TL 10 700 Td (one) Tj T* (two) Tj ET"),
        "one\ntwo"
    );
    assert_eq!(
        text_of("BT /F1 12 Tf 10 700 Td (one) Tj 1 0 0 1 10 600 Tm (two) Tj ET"),
        "one\ntwo"
    );
    assert_eq!(
        text_of("BT /F1 12 Tf 12 TL 10 700 Td (one) ' (two) ' ET"),
        "one\ntwo",
        "the quote operator moves down by the leading before showing text"
    );
}

#[test]
fn unsupported_operators_are_ignored_not_fatal() {
    // Graphics operators around the text must not disturb extraction.
    assert_eq!(
        text_of(
            "q 1 0 0 1 0 0 cm 0.5 g 0 0 100 100 re f Q \
             BT /F1 12 Tf 10 700 Td (still here) Tj ET"
        ),
        "still here"
    );
}

#[test]
fn inline_images_are_skipped() {
    let mut content = b"q BI /W 1 /H 1 /BPC 8 /CS /G ID ".to_vec();
    content.extend_from_slice(&[0x00, 0x20, 0xff]);
    content.extend_from_slice(b" EI Q BT /F1 12 Tf 10 700 Td (after image) Tj ET");
    assert_eq!(text_of(&String::from_utf8_lossy(&content)), "after image");

    // A BI with no EI is an error, not an endless loop.
    let document = doc(&[
        (1, "<< /Type /Catalog /Pages 2 0 R >>"),
        (2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>"),
        (3, "<< /Type /Page /MediaBox [0 0 10 10] >>"),
    ]);
    assert!(
        document
            .extract_text_from(b"BI /W 1 ID \x00\x01", None)
            .is_err()
    );
}

#[test]
fn a_missing_font_resource_is_an_error() {
    let document = doc(&[
        (1, "<< /Type /Catalog /Pages 2 0 R >>"),
        (2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>"),
        (3, "<< /Type /Page /MediaBox [0 0 10 10] >>"),
    ]);
    assert!(
        document
            .extract_text_from(b"BT /F9 12 Tf 10 700 Td (x) Tj ET", None)
            .is_err()
    );
}

#[test]
fn composite_fonts_are_reported_as_unsupported() {
    let document = doc(&[
        (1, "<< /Type /Catalog /Pages 2 0 R >>"),
        (2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>"),
        (
            3,
            "<< /Type /Page /MediaBox [0 0 612 792] \
             /Resources << /Font << /F1 5 0 R >> >> >>",
        ),
        (5, "<< /Type /Font /Subtype /Type0 /BaseFont /Mincho >>"),
    ]);
    let resources = document.page(0).expect("page").resources.expect("res");
    let error = document
        .extract_text_from(b"BT /F1 12 Tf 10 700 Td (x) Tj ET", Some(&resources))
        .expect_err("Type0");
    assert!(error.to_string().contains("Type0"), "{error}");
}

#[test]
fn a_font_without_a_declared_encoding_falls_back_to_standard() {
    // StandardEncoding maps the ASCII range straight through.
    assert_eq!(
        text_of("BT /F1 12 Tf 10 700 Td (ASCII stays ASCII) Tj ET"),
        "ASCII stays ASCII"
    );
}

#[test]
fn whitespace_and_comments_in_content_streams_are_skipped() {
    assert_eq!(
        text_of("BT % a comment\n /F1 12 Tf \n 10 700 Td \n (ok) Tj % trailing\n ET"),
        "ok"
    );
}

#[test]
fn every_truncation_of_the_content_stream_is_handled() {
    let document = open(TEXT);
    let page = document.page(0).expect("page");
    let content = document.page_content(&page).expect("content");
    let resources = page.resources.clone();
    for end in 0..content.len() {
        let _ = document.extract_text_from(&content[..end], resources.as_ref());
    }
    assert!(
        document
            .extract_text_from(&content, resources.as_ref())
            .is_ok()
    );
}

#[test]
fn incomplete_text_runs_do_not_produce_garbage() {
    let document = doc(&[
        (1, "<< /Type /Catalog /Pages 2 0 R >>"),
        (2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>"),
        (3, "<< /Type /Page /MediaBox [0 0 10 10] >>"),
    ]);
    // `Tj` with no operand and positioning operators with no operands are
    // simply ignored.
    assert_eq!(
        document
            .extract_text_from(b"BT Tj ET 10 700 Td T* ET", None)
            .expect("ignored"),
        ""
    );
    // Text shown before any font is selected yields nothing.
    assert_eq!(
        document
            .extract_text_from(b"BT (string) Tj ET", None)
            .expect("no font selected yet"),
        ""
    );
    // `Tf` without a name is a broken stream and says so.
    assert!(document.extract_text_from(b"BT 12 Tf ET", None).is_err());
    assert!(document.get_object(pdf2::ObjectId::new(1, 0)).is_ok());
}

#[test]
fn lines_come_out_in_reading_order_not_paint_order() {
    // The fixture paints third, first, second; the page reads first, second,
    // third, and the middle line's space is a `TJ` adjustment rather than a
    // space glyph.
    let bytes = std::fs::read(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/tests/fixtures/reading-order.pdf"
    ))
    .expect("read reading-order.pdf");
    let document = open(&bytes);
    let page = document.page(0).expect("page");
    assert_eq!(
        document.extract_text(&page).expect("text"),
        "first line\nsecond line\nthird line\nscaled line"
    );
    // `ADR 0008`: the same accumulator carries the geometry. The three lines are
    // placed by `Tm` with an identity CTM, so their page-space origins are the
    // numbers in the content stream; the fourth is painted under `q 2 0 0 2 20 100
    // cm`, so its origin moves to (20, 100) and its box doubles.
    let spans = document.spans(&page).expect("spans");
    let texts: Vec<&str> = spans.iter().map(|span| span.text.as_str()).collect();
    assert_eq!(
        texts,
        ["first line", "second line", "third line", "scaled line"]
    );
    let bounds = |index: usize| spans[index].bounds;
    for (index, expected) in [(0, 400.0), (1, 350.0), (2, 300.0)] {
        let rect = bounds(index);
        assert!(
            (rect.x0 - 50.0).abs() < 0.01 && (rect.y0 - expected).abs() < 0.01,
            "line {index} starts at (50, {expected}), got ({}, {})",
            rect.x0,
            rect.y0
        );
        assert!(
            rect.width() > 10.0,
            "line {index} has a real width: {}",
            rect.width()
        );
        assert!(
            (rect.height() - 12.0).abs() < 0.01,
            "line {index} is one em tall, got {}",
            rect.height()
        );
    }
    let scaled = bounds(3);
    assert!(
        (scaled.x0 - 20.0).abs() < 0.01 && (scaled.y0 - 100.0).abs() < 0.01,
        "the scaled line starts at (20, 100), got ({}, {})",
        scaled.x0,
        scaled.y0
    );
    assert!(
        (scaled.height() - 24.0).abs() < 0.01,
        "the CTM doubles the em, got {}",
        scaled.height()
    );
}

/// A one-page PDF whose content stream is `content`, on a base-14 font.
fn page_with(content: &str) -> Vec<u8> {
    let body = format!(
        "<< /Length {} >>\nstream\n{content}\nendstream",
        content.len()
    );
    pdf(&[
        (1, "<< /Type /Catalog /Pages 2 0 R >>"),
        (2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>"),
        (
            3,
            "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 100] /Contents 4 0 R \
             /Resources << /Font << /F1 5 0 R >> >> >>",
        ),
        (4, &body),
        (5, "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>"),
    ])
}

#[test]
fn actual_text_replaces_the_codes_inside_its_marked_content() {
    // The CV that started this work wraps each `fi` ligature in
    // `/Span << /ActualText (fi) >> BDC … EMC`: the glyph's code says one thing and
    // the writer says another, and `14.9.4` makes the writer right. Without this the
    // text reads "Pro|le" and a search for "Profile" finds nothing.
    let bytes = page_with(
        "BT /F1 12 Tf 10 50 Td (Pro) Tj /Span << /ActualText (fi) >> BDC (X) Tj EMC (le) Tj ET",
    );
    let document = open(&bytes);
    let page = document.page(0).expect("page");
    assert_eq!(document.extract_text(&page).expect("text"), "Profile");

    // The same content without the marker keeps the code's own text.
    let plain = page_with("BT /F1 12 Tf 10 50 Td (Pro) Tj (X) Tj (le) Tj ET");
    let document = open(&plain);
    let page = document.page(0).expect("page");
    assert_eq!(document.extract_text(&page).expect("text"), "ProXle");
}

#[test]
fn a_search_finds_the_text_actual_text_states() {
    let bytes = page_with(
        "BT /F1 12 Tf 10 50 Td (Pro) Tj /Span << /ActualText (fi) >> BDC (X) Tj EMC (le) Tj ET",
    );
    let document = open(&bytes);
    let hits = document.search("Profile").expect("search");
    assert_eq!(hits.len(), 1, "the writer's text is what the reader sees");
    assert_eq!(hits[0].hits[0].text, "Profile");
    assert!(
        document.search("ProXle").expect("search").is_empty(),
        "the glyph codes are not searchable"
    );
}

#[test]
fn a_composite_font_reads_through_its_to_unicode_map() {
    // The fixture's codes bear no relation to their text, and its second run is
    // placed 120 units right of where the first stopped: the text can only be
    // right through `/ToUnicode` and the font's `/W` and `/DW` widths.
    let bytes = std::fs::read(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/tests/fixtures/tounicode.pdf"
    ))
    .expect("read tounicode.pdf");
    let document = open(&bytes);
    let page = document.page(0).expect("page");
    assert_eq!(document.extract_text(&page).expect("text"), "ADabcxyz A");
}

#[test]
fn composite_fonts_we_cannot_read_are_refused_by_name() {
    for (font, expected) in [
        (
            "<< /Type /Font /Subtype /Type0 /BaseFont /Mincho /Encoding /Identity-H              /DescendantFonts [5 0 R] >>",
            "without a /ToUnicode map",
        ),
        (
            "<< /Type /Font /Subtype /Type0 /BaseFont /Mincho /Encoding /Identity-V              /ToUnicode 5 0 R /DescendantFonts [5 0 R] >>",
            "vertical composite",
        ),
        (
            "<< /Type /Font /Subtype /Type0 /BaseFont /Mincho /Encoding /90pv-RKSJ-H              /ToUnicode 5 0 R /DescendantFonts [5 0 R] >>",
            "CMap-encoded composite",
        ),
    ] {
        let all: &[&str] = &[
            "<< /Type /Catalog /Pages 2 0 R >>",
            "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
            "<< /Type /Page /MediaBox [0 0 612 792]              /Resources << /Font << /F1 4 0 R >> >> >>",
            font,
            "null",
        ];
        let objects: Vec<(u32, &str)> = all
            .iter()
            .enumerate()
            .map(|(index, body)| (u32::try_from(index + 1).expect("index"), *body))
            .collect();
        let document = doc(&objects);
        let resources = document.page(0).expect("page").resources.expect("res");
        let error = document
            .extract_text_from(b"BT /F1 12 Tf 10 700 Td (x) Tj ET", Some(&resources))
            .expect_err(expected);
        assert!(error.to_string().contains(expected), "{error}");
    }
}

#[test]
fn a_predefined_cmap_font_extracts_cjk() {
    // The corpus file sets `/Encoding /90ms-RKSJ-H` over an Adobe-Japan1 CID
    // font with no `/ToUnicode`: the text can only come from the predefined CMap
    // the crate ships (`cargo xtask cmaps`).
    let path = concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/corpus/tier-a/90ms_rksj_h_sample.pdf"
    );
    let bytes = std::fs::read(path).expect("read the corpus file");
    let document = open(&bytes);
    let page = document.page(0).expect("page");
    assert_eq!(
        document.extract_text(&page).expect("text"),
        "Hello ASCII\n\u{65E5}\u{672C}\u{8A9E}\u{30C6}\u{30B9}\u{30C8}"
    );
}

#[test]
fn search_finds_a_phrase_inside_a_line_and_ignores_case() {
    let bytes = std::fs::read(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/tests/fixtures/reading-order.pdf"
    ))
    .expect("read reading-order.pdf");
    let document = open(&bytes);
    let page = document.page(0).expect("page");
    let spans = document.spans(&page).expect("spans");

    // "first line" is one span whose space came from a `TJ` adjustment, so it is a
    // phrase over two *runs*: found, with the line's own box.
    let found = document.search("first line").expect("search");
    assert_eq!(found.len(), 1, "one page carries the phrase");
    assert_eq!(found[0].page, 0);
    assert_eq!(found[0].hits.len(), 1);
    assert_eq!(found[0].hits[0].text, "first line");
    assert_eq!(found[0].hits[0].bounds, spans[0].bounds);

    // Case is a search convenience, not a rewrite: the hit keeps the page's text.
    let upper = document.search("FIRST LINE").expect("search");
    assert_eq!(upper[0].hits[0].text, "first line");

    // Two occurrences on the page, left to right.
    let lines = document.search("line").expect("search");
    assert_eq!(lines[0].hits.len(), 4, "every line ends in 'line'");
}

#[test]
fn search_does_not_match_across_a_line_break_and_an_empty_needle_matches_nothing() {
    let bytes = std::fs::read(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/tests/fixtures/reading-order.pdf"
    ))
    .expect("read reading-order.pdf");
    let document = open(&bytes);

    // "first line" and "second line" are adjacent lines; a reader searching for
    // their junction should not be handed a hit that spans the break.
    assert!(
        document.search("line second").expect("search").is_empty(),
        "a line break is not a space"
    );
    assert!(
        document.search("").expect("search").is_empty(),
        "an empty needle matches nothing"
    );
    assert!(
        document.search("nothing here").expect("search").is_empty(),
        "a missing phrase is not a hit"
    );
}