flpdf 0.10.0

A pure-Rust PDF toolkit modeled on qpdf — read, rewrite, linearize, and inspect PDFs.
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
mod common;
use common::PdfCanonicalTestExt;

use flpdf::document_json::write_json;
use flpdf::json_inspect::{DecodeLevel, StreamDataMode};
use flpdf::pipeline::PlString;
use flpdf::{Error, ObjectRef, Pdf};
use std::fs;
use std::io::{self, BufReader, Cursor, Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::process::Command;

const ROOTLESS_COMPLETE_JSON: &[u8] = br#"{
  "qpdf": [
    {"jsonversion": 2, "pdfversion": "1.3", "calledgetallpages": true},
    {"trailer": {"value": {}}}
  ]
}"#;

const UPDATE_JSON: &[u8] = br#"{
  "qpdf": [
    {"jsonversion": 2, "calledgetallpages": true},
    {"obj:1 0 R": {"value": {"n:/Marker": true}}}
  ]
}"#;

const CATALOG_COMPLETE_JSON: &[u8] = br#"{
  "qpdf": [
    {"jsonversion": 2, "pdfversion": "1.3"},
    {
      "obj:1 0 R": {"value": {"/Pages": "2 0 R", "/Type": "/Catalog"}},
      "obj:2 0 R": {"value": {"/Count": 0, "/Kids": [], "/Type": "/Pages"}},
      "trailer": {"value": {"/Root": "1 0 R", "/Size": 3}}
    }
  ]
}"#;

const CATALOG_WITH_EXTENSION_LEVEL_JSON: &[u8] = br#"{
  "qpdf": [
    {"jsonversion": 2, "pdfversion": "1.7"},
    {
      "obj:1 0 R": {
        "value": {
          "/Extensions": {"/ADBE": {"/BaseVersion": "/1.7", "/ExtensionLevel": 8}},
          "/Pages": "2 0 R",
          "/Type": "/Catalog"
        }
      },
      "obj:2 0 R": {"value": {"/Count": 0, "/Kids": [], "/Type": "/Pages"}},
      "trailer": {"value": {"/Root": "1 0 R", "/Size": 3}}
    }
  ]
}"#;

fn fixture(name: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../../tests/fixtures/compat/json-input")
        .join(name)
}

fn flpdf_json<R>(mut pdf: Pdf<R>) -> Vec<u8>
where
    R: Read + Seek + 'static,
{
    let mut bytes = Vec::new();
    {
        let mut output = PlString::new("json output", None, &mut bytes);
        write_json(
            &mut pdf,
            2,
            &mut output,
            DecodeLevel::None,
            &StreamDataMode::Inline,
            &[],
        )
        .expect("flpdf JSON output");
    }
    bytes
}

fn qpdf_json_input_output(path: &Path) -> Option<Vec<u8>> {
    let output = Command::new("qpdf")
        .args(["--json-input", "--json-output=2"])
        .arg(path)
        .arg("-")
        .output()
        .ok()?;
    assert!(
        output.status.success(),
        "qpdf JSON input failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    Some(output.stdout)
}

struct FailingReader;

impl Read for FailingReader {
    fn read(&mut self, _buffer: &mut [u8]) -> io::Result<usize> {
        Err(io::Error::other("reader exploded"))
    }
}

impl Seek for FailingReader {
    fn seek(&mut self, _position: SeekFrom) -> io::Result<u64> {
        Ok(0)
    }
}

/// A `Read + Seek` whose relative seek (`SeekFrom::Current`, what
/// `Seek::stream_position`'s default implementation uses) fails while
/// absolute seeks still succeed -- an unusual but valid `Seek`
/// implementation that must not make `import_json` silently substitute `0`
/// for the source's actual starting position.
struct FlakyCurrentSeekReader(Cursor<Vec<u8>>);

impl Read for FlakyCurrentSeekReader {
    fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
        self.0.read(buffer)
    }
}

impl Seek for FlakyCurrentSeekReader {
    fn seek(&mut self, position: SeekFrom) -> io::Result<u64> {
        if matches!(position, SeekFrom::Current(_)) {
            return Err(io::Error::other("relative seek unsupported"));
        }
        self.0.seek(position)
    }
}

#[test]
fn import_propagates_a_stream_position_failure_instead_of_assuming_zero() {
    let mut bytes = b"prefix".to_vec();
    let json_start = bytes.len() as u64;
    bytes.extend_from_slice(UPDATE_JSON);
    let mut cursor = Cursor::new(bytes);
    cursor.set_position(json_start);
    let source = FlakyCurrentSeekReader(cursor);

    let mut pdf = Pdf::empty().expect("empty document");
    let error = pdf
        .update_from_json(source, "flaky.json")
        .expect_err("a stream_position failure must propagate, not default to offset 0");

    assert!(matches!(
        error,
        Error::SystemBytes(ref message)
            if message.starts_with(b"flaky.json: ")
                && message.ends_with(b"relative seek unsupported")
    ));
}

#[test]
fn create_from_json_reads_inline_stream_data_when_the_source_starts_past_a_prefix() {
    // The tokenizer records offsets from its own first byte, not from the
    // reader's absolute position (`Parser::pos` starts at `0` regardless of
    // where the caller's cursor already was), so a source positioned past an
    // unrelated prefix must still resolve deferred inline stream reads to
    // the correct bytes rather than seeking `prefix.len()` bytes short.
    let mut bytes = b"garbage prefix that is not part of the JSON document".to_vec();
    let json_start = bytes.len() as u64;
    bytes.extend_from_slice(
        br#"{"qpdf":[{"jsonversion":2,"pdfversion":"1.3"},{"obj:1 0 R":{"stream":{"dict":{},"data":"SGVsbG8="}},"trailer":{"value":{}}}]}"#,
    );
    let mut source = Cursor::new(bytes);
    source.set_position(json_start);

    let mut pdf = Pdf::create_from_json(source, "prefixed.json").expect("create");
    let stream = pdf.get_object_handle(ObjectRef::new(1, 0));
    assert_eq!(
        stream
            .get_raw_stream_data()
            .expect("stream data")
            .as_slice(),
        b"Hello"
    );
}

#[test]
fn create_from_json_uses_qpdf_rootless_seed_and_complete_metadata() {
    let mut pdf = Pdf::create_from_json(Cursor::new(ROOTLESS_COMPLETE_JSON), "rootless.json")
        .expect("complete JSON should create a document");

    assert_eq!(pdf.version(), "1.3");
    assert!(pdf.root_ref().is_none(), "the JSON trailer is rootless");
    assert!(
        !pdf.ever_called_get_all_pages(),
        "create mode ignores update flags"
    );
    assert!(pdf.trailer().try_get_key(b"/Size").unwrap().is_null());
}

#[test]
fn create_from_json_exposes_the_imported_canonical_trailer_to_page_primitives() {
    let mut pdf = Pdf::create_from_json(Cursor::new(CATALOG_COMPLETE_JSON), "catalog.json")
        .expect("catalog JSON should create a document");

    assert_eq!(pdf.root_ref(), Some(ObjectRef::new(1, 0)));
    // `trailer_key_handle` must observe the same live trailer as `root_ref`,
    // matching its own doc's claimed equivalence to `trailer().get_key(key)`
    // -- both routes are backed by the same canonical handle the JSON importer
    // installs, not the pre-import construction-time snapshot.
    assert_eq!(
        pdf.trailer_key_handle(b"Root").object_ref(),
        Some(ObjectRef::new(1, 0))
    );
}

#[test]
fn create_from_json_exposes_the_imported_root_to_adobe_extension_level() {
    let mut pdf = Pdf::create_from_json(
        Cursor::new(CATALOG_WITH_EXTENSION_LEVEL_JSON),
        "extension.json",
    )
    .expect("catalog JSON should create a document");

    assert_eq!(
        pdf.adobe_extension_level().expect("extension level"),
        Some(8)
    );
}

#[test]
fn create_from_json_file_roundtrips_a_flpdf_authored_fixture_against_qpdf() {
    let path = fixture("complete.json");
    let pdf = Pdf::create_from_json_file(&path).expect("fixture JSON should create a document");
    let Some(expected) = qpdf_json_input_output(&path) else {
        eprintln!("skipping qpdf differential: qpdf is unavailable");
        return;
    };

    assert_eq!(flpdf_json(pdf), expected);
}

#[test]
fn update_from_json_matches_qpdf_after_a_complete_flpdf_fixture_import() {
    let complete = fixture("complete.json");
    let update = fixture("update.json");
    let Some(qpdf) = Command::new("qpdf").arg("--version").output().ok() else {
        eprintln!("skipping qpdf differential: qpdf is unavailable");
        return;
    };
    assert!(qpdf.status.success(), "qpdf --version failed");

    let temporary = tempfile::tempdir().expect("temporary qpdf directory");
    let qpdf_input = temporary.path().join("input.json");
    let qpdf_update = temporary.path().join("update.json");
    let qpdf_pdf = temporary.path().join("input.pdf");
    fs::copy(&complete, &qpdf_input).expect("copy complete fixture");
    fs::copy(&update, &qpdf_update).expect("copy update fixture");
    let created = Command::new("qpdf")
        .args(["--json-input"])
        .arg(&qpdf_input)
        .arg(&qpdf_pdf)
        .output()
        .expect("run qpdf create");
    assert!(
        created.status.success(),
        "qpdf create failed: {}",
        String::from_utf8_lossy(&created.stderr)
    );
    let expected = Command::new("qpdf")
        .arg(format!("--update-from-json={}", qpdf_update.display()))
        .args(["--json-output=2"])
        .arg(&qpdf_pdf)
        .arg("-")
        .output()
        .expect("run qpdf update");
    assert!(
        expected.status.success(),
        "qpdf update failed: {}",
        String::from_utf8_lossy(&expected.stderr)
    );

    let mut pdf = Pdf::open(BufReader::new(
        fs::File::open(&qpdf_pdf).expect("open qpdf PDF"),
    ))
    .expect("flpdf open");
    pdf.update_from_json(fs::File::open(&update).expect("open update"), "update.json")
        .expect("flpdf update");
    assert_eq!(flpdf_json(pdf), expected.stdout);
}

#[test]
fn update_from_json_replaces_only_named_objects_and_runs_update_flags() {
    let mut pdf = Pdf::empty().expect("empty document");
    let original_pages = pdf
        .resolve_canonical_object(ObjectRef::new(2, 0))
        .expect("pages object");

    pdf.update_from_json(Cursor::new(UPDATE_JSON), "update.json")
        .expect("partial JSON should update the document");

    let catalog = pdf.get_object_handle(ObjectRef::new(1, 0));
    assert_eq!(
        catalog.try_get_key(b"/Marker").unwrap().as_boolean(),
        Some(true)
    );
    assert!(pdf
        .resolve_canonical_object(ObjectRef::new(2, 0))
        .unwrap()
        .is_same_object_as(&original_pages));
    assert!(pdf.ever_called_get_all_pages());
}

#[test]
fn import_parser_errors_are_wrapped_with_the_input_description() {
    let mut pdf = Pdf::empty().expect("empty document");
    let error = pdf
        .update_from_json(Cursor::new(b"{".as_slice()), "broken.json")
        .expect_err("malformed JSON must fail");

    assert!(
        matches!(error, Error::SystemBytes(message) if message == b"broken.json: JSON: premature end of input")
    );
}

#[test]
fn import_reader_errors_are_wrapped_with_the_input_description() {
    let mut pdf = Pdf::empty().expect("empty document");
    let error = pdf
        .update_from_json(FailingReader, "reader.json")
        .expect_err("reader errors must fail the import");

    assert!(
        matches!(error, Error::SystemBytes(message) if message == b"reader.json: reader exploded")
    );
}

#[test]
fn import_fatal_reactor_errors_use_the_same_qpdf_exception_boundary() {
    let mut pdf = Pdf::empty().expect("empty document");
    let error = pdf
        .update_from_json(Cursor::new(b"true".as_slice()), "scalar.json")
        .expect_err("top-level scalar must fail");

    assert!(
        matches!(error, Error::SystemBytes(message) if message == b"scalar.json: QPDF JSON must be a dictionary")
    );
}

#[test]
fn import_reports_a_recorded_fatal_over_a_later_parser_error() {
    // qpdf's reactor throws immediately at the fatal condition
    // (`QPDF_json.cc:353,463`), unwinding out of `JSON::parse` before the
    // tokenizer can ever see `}` mismatching the just-opened `[`. flpdf's
    // reactor records the fatal but lets the tokenizer keep running, so it
    // goes on to raise its own, later, and therefore qpdf-unreachable
    // "unexpected dictionary end delimiter" syntax error -- the recorded
    // fatal must still be what the caller sees.
    let mut pdf = Pdf::empty().expect("empty document");
    let error = pdf
        .update_from_json(Cursor::new(b"[}".as_slice()), "malformed.json")
        .expect_err("top-level array followed by a mismatched delimiter must fail");

    assert!(matches!(
        error,
        Error::SystemBytes(message) if message == b"malformed.json: QPDF JSON must be a dictionary"
    ));
}

#[test]
fn file_entry_points_keep_open_failures_and_update_from_json_file_is_lazy() {
    let missing = tempfile::tempdir()
        .expect("temporary directory")
        .path()
        .join("missing.json");
    let error = match Pdf::create_from_json_file(&missing) {
        Ok(_) => panic!("missing JSON must fail to open"),
        Err(error) => error,
    };
    assert!(
        matches!(error, Error::FileIo { operation: "open JSON input", path, .. } if path == missing)
    );

    let mut pdf = Pdf::empty().expect("empty document");
    pdf.update_from_json_file(fixture("update.json"))
        .expect("file update should succeed");
    let stream = pdf.get_object_handle(ObjectRef::new(4, 0));
    assert_eq!(
        stream
            .get_raw_stream_data()
            .expect("provider should pipe on demand")
            .as_slice(),
        b"Updated JSON\n"
    );
}

#[test]
fn import_aggregates_semantic_errors_after_incremental_parsing() {
    let mut pdf = Pdf::empty().expect("empty document");
    let input = br#"{
      "qpdf": [
        {"jsonversion": 2},
        {"obj:1 0 R": {}}
      ]
    }"#;

    let error = pdf
        .update_from_json(Cursor::new(input), "semantic.json")
        .expect_err("object without value or stream must fail");

    assert!(
        matches!(error, Error::SystemBytes(message) if message == b"semantic.json: errors found in JSON")
    );
    assert!(pdf
        .repair_diagnostics()
        .entries()
        .iter()
        .any(
            |diagnostic| String::from_utf8_lossy(diagnostic.get_message_detail())
                .contains("exactly one of")
        ));
}

#[test]
fn create_from_json_wraps_a_semantic_failure_with_its_accumulated_diagnostics() {
    let input: &[u8] = br#"{
      "qpdf": [
        {"jsonversion": 2, "pdfversion": "1.3"},
        {
          "obj:1 0 R": {},
          "trailer": {"value": {}}
        }
      ]
    }"#;

    let error = match Pdf::create_from_json(Cursor::new(input), "semantic.json") {
        Ok(_) => panic!("object without value or stream must fail"),
        Err(error) => error,
    };

    let (source, diagnostics) = error
        .open_failure()
        .expect("a semantic import failure with recorded warnings wraps as OpenFailure");
    assert!(
        matches!(source, Error::SystemBytes(message) if message == b"semantic.json: errors found in JSON")
    );
    assert!(diagnostics
        .entries()
        .iter()
        .any(
            |diagnostic| String::from_utf8_lossy(diagnostic.get_message_detail())
                .contains("exactly one of")
        ));
}