flpdf 0.5.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
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
//! qpdf correspondence: QPDFFileSpecObjectHelper.cc and QPDFEFStreamObjectHelper.cc.
//! Typed wrappers for `/Filespec` dictionaries and `/EmbeddedFile` streams,
//! plus a builder for constructing them.
//!
//! [`FileSpec`] wraps a `/Filespec` dictionary and exposes ergonomic, typed
//! accessors for all common fields (filename, description, embedded file
//! stream, etc.).  [`EmbeddedFileStream`] wraps the embedded `/EmbeddedFile`
//! stream reachable via the `/EF` sub-dictionary and exposes its payload and
//! metadata (MIME type, dates, checksum, size).
//!
//! [`FileSpecBuilder`] constructs a `/Filespec` dictionary and its associated
//! `/EmbeddedFile` stream in-memory and registers them in a [`crate::Pdf`] document via
//! qpdf's indirect-handle factory.  The returned [`crate::ObjectRef`] can then be inserted into
//! the `/Names /EmbeddedFiles` name tree using
//! [`crate::embedded_files::insert_embedded_file`].
//!
//! [`FileSpec`] and [`EmbeddedFileStream`] own qpdf-shaped object handles and
//! resolve dictionaries from the live document on each operation. Their
//! setters mutate those dictionary handles in place, including an indirect
//! `/Params` dictionary when one is present.
//!
//! # Design
//!
//! PDF key naming follows ISO 32000-1 §7.11.  The `/EF` lookup priority used
//! here mirrors qpdf's `QPDFFileSpecObjectHelper::name_keys` order
//! (`QPDFFileSpecObjectHelper.cc`), which is also what its `preferredcontents`
//! JSON output uses: `/UF` › `/F` › `/Unix` › `/DOS` › `/Mac`.
//!
//! Date strings (e.g. `/Params /CreationDate`) are returned as raw PDF date
//! byte sequences (`D:YYYYMMDDHHmmSSOHH'mm'`).  No date parsing is performed.
//!
//! # Examples
//!
//! ## Read filename and payload from a `/Filespec` object
//!
//! ```no_run
//! use std::fs::File;
//! use std::io::{BufReader, Cursor};
//! use flpdf::{FileSpec, ObjectRef, Pdf};
//!
//! let mut pdf = Pdf::open(BufReader::new(File::open("with-attachment.pdf")?))?;
//!
//! // Assume we know the /Filespec object reference (e.g. from walking /Names).
//! let filespec_ref = ObjectRef::new(5, 0);
//! let mut fs = FileSpec::new(pdf.get_object_handle(filespec_ref), &mut pdf).unwrap();
//!
//! if let Some(name) = fs.filename()? {
//!     println!("filename: {}", String::from_utf8_lossy(&name));
//! }
//! if let Some(mut ef) = fs.embedded_file()? {
//!     let bytes = ef.payload()?;
//!     println!("{} payload bytes", bytes.len());
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## Inspect embedded file metadata
//!
//! ```no_run
//! use std::fs::File;
//! use std::io::BufReader;
//! use flpdf::{FileSpec, ObjectRef, Pdf};
//!
//! let mut pdf = Pdf::open(BufReader::new(File::open("with-attachment.pdf")?))?;
//! let filespec_ref = ObjectRef::new(5, 0);
//! let mut fs = FileSpec::new(pdf.get_object_handle(filespec_ref), &mut pdf).unwrap();
//!
//! if let Some(mut ef) = fs.embedded_file()? {
//!     if let Some(mime) = ef.mimetype()? {
//!         println!("MIME: {}", String::from_utf8_lossy(&mime));
//!     }
//!     if let Some(created) = ef.creation_date()? {
//!         // raw PDF date string, e.g. b"D:20260101000000Z"
//!         println!("created: {}", String::from_utf8_lossy(&created));
//!     }
//!     if let Some(sz) = ef.size()? {
//!         println!("uncompressed size: {sz}");
//!     }
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

mod embedded_file_stream;
mod filespec;
mod shared;

pub use embedded_file_stream::EmbeddedFileStream;
pub use filespec::{FileParamDates, FileSpec, FileSpecBuilder};
pub(crate) use shared::qpdf_style_open_error;
pub use shared::{encode_utf16be, format_pdf_date, md5_checksum};

// ── Tests ─────────────────────────────────────────────────────────────────────

// cov:ignore-start: test-only compatibility coverage moved with the module; integration suites exercise the production owners
#[cfg(test)]
mod tests {
    use super::*;
    use crate::embedded_files::{insert_embedded_file, list_embedded_files};
    use crate::job::{
        add_attachment_from_path, extract_attachment, extract_attachment_to_path, write_attachment,
    };
    use crate::{ObjectHandle, ObjectRef, Pdf};
    use std::io::Cursor;

    // ── Minimal PDF fixture ───────────────────────────────────────────────────

    fn minimal_pdf_bytes() -> Vec<u8> {
        let mut pdf = Vec::new();
        pdf.extend_from_slice(b"%PDF-1.4\n");
        let off1 = pdf.len() as u64;
        pdf.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
        let off2 = pdf.len() as u64;
        pdf.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
        let off3 = pdf.len() as u64;
        pdf.extend_from_slice(
            b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\n",
        );
        let xref_start = pdf.len() as u64;
        let xref = format!(
            "xref\n0 4\n0000000000 65535 f \n{:010} 00000 n \n{:010} 00000 n \n{:010} 00000 n \n",
            off1, off2, off3,
        );
        pdf.extend_from_slice(xref.as_bytes());
        let trailer =
            format!("trailer\n<< /Size 4 /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n");
        pdf.extend_from_slice(trailer.as_bytes());
        pdf
    }

    fn open_minimal() -> Pdf<Cursor<Vec<u8>>> {
        Pdf::open(Cursor::new(minimal_pdf_bytes())).expect("open minimal PDF")
    }

    fn resolved_handle(pdf: &mut Pdf<Cursor<Vec<u8>>>, object_ref: ObjectRef) -> ObjectHandle {
        let handle = pdf.get_object_handle(object_ref);
        pdf.resolve(&handle).expect("resolve object");
        handle
    }

    #[test]
    fn builder_rejects_a_non_utf8_filename_without_a_unicode_override() {
        let mut pdf = open_minimal();
        let error = FileSpecBuilder::new(b"\xff.txt", b"payload".as_slice())
            .build(&mut pdf)
            .expect_err("/UF requires an explicit Unicode filename for non-UTF-8 bytes");
        assert_eq!(
            error.to_string(),
            "unsupported PDF feature: FileSpecBuilder: filename is not valid UTF-8; cannot encode /UF"
        );
    }

    #[test]
    fn embedded_file_finalizer_rejects_a_non_stream_handle() {
        let mut pdf = open_minimal();
        let error = EmbeddedFileStream::new_from_stream(&mut pdf, ObjectHandle::null())
            .expect_err("the shared finalizer requires a stream handle");

        assert_eq!(
            error.to_string(),
            "EmbeddedFile factory received a non-stream object"
        );
    }

    #[test]
    fn pipe_stream_data_rejects_a_null_embedded_file_handle() {
        let mut pdf = open_minimal();
        let embedded_file = EmbeddedFileStream::new(ObjectHandle::null(), &mut pdf)
            .expect("a direct null handle is a valid wrapper input");
        let mut discard = crate::pipeline::Discard;

        let error = embedded_file
            .pipe_stream_data(&mut discard)
            .expect_err("a null embedded file stream must be rejected");
        assert_eq!(
            error.to_string(),
            "unsupported PDF feature: expected an /EmbeddedFile stream object"
        );
    }

    #[test]
    fn direct_null_filespec_stream_entries_are_empty() {
        let mut pdf = open_minimal();
        let mut helper = FileSpec::new(ObjectHandle::null(), &mut pdf).unwrap();

        assert!(helper
            .get_embedded_file_stream_entries()
            .unwrap()
            .is_empty());
    }

    #[test]
    fn filespec_helper_marks_an_indirect_owner_of_a_direct_dictionary_dirty() {
        let mut pdf = open_minimal();
        let owner_ref = ObjectRef::new(5, 0);
        let filespec = ObjectHandle::dictionary(vec![(
            b"/F".to_vec(),
            ObjectHandle::string(b"direct.txt".to_vec()),
        )]);
        let owner = ObjectHandle::dictionary(vec![(b"/FS".to_vec(), filespec)]);
        pdf.replace_object(owner_ref, owner).unwrap();
        let owner = pdf.get_object_handle(owner_ref);
        pdf.resolve(&owner).unwrap();
        let direct_filespec = owner.get_key(b"/FS");
        pdf.clear_dirty(owner_ref);

        let mut helper = FileSpec::new(direct_filespec, &mut pdf).unwrap();
        helper.set_description("persisted through owner").unwrap();
        drop(helper);

        assert!(pdf.is_dirty(owner_ref));
    }

    #[test]
    fn helper_constructors_reject_indirect_handles_from_another_pdf() {
        let mut source = open_minimal();
        let foreign_filespec = source.get_object_handle(ObjectRef::new(1, 0));
        let foreign_stream = source.get_object_handle(ObjectRef::new(2, 0));
        let mut destination = open_minimal();

        assert!(FileSpec::new(foreign_filespec, &mut destination).is_err());
        assert!(EmbeddedFileStream::new(foreign_stream, &mut destination).is_err());
    }

    #[test]
    fn filespec_constructor_rejects_a_direct_child_from_another_pdf() {
        let mut source = open_minimal();
        let owner_ref = ObjectRef::new(5, 0);
        let filespec = ObjectHandle::dictionary(vec![(
            b"/F".to_vec(),
            ObjectHandle::string(b"foreign.txt".to_vec()),
        )]);
        let owner_dict = ObjectHandle::dictionary(vec![(b"/FS".to_vec(), filespec)]);
        source.replace_object(owner_ref, owner_dict).unwrap();
        let owner = source.get_object_handle(owner_ref);
        source.resolve(&owner).unwrap();
        let foreign_direct_filespec = owner.get_key(b"/FS");
        assert!(foreign_direct_filespec.is_direct());

        let mut destination = open_minimal();
        let error = FileSpec::new(foreign_direct_filespec, &mut destination)
            .err()
            .expect("a direct child owned by another Pdf must be rejected");
        assert_eq!(
            error.to_string(),
            "unsupported PDF feature: Filespec handle belongs to another Pdf"
        );
    }

    #[test]
    fn rejecting_a_foreign_handle_does_not_reserve_its_object_number() {
        let mut source = open_minimal();
        let foreign = source.get_object_handle(ObjectRef::new(99, 0));
        let mut destination = open_minimal();

        assert!(FileSpec::new(foreign, &mut destination).is_err());
        assert_eq!(
            EmbeddedFileStream::create_ef_stream(&mut destination, b"payload")
                .unwrap()
                .object_ref(),
            Some(ObjectRef::new(4, 0))
        );
    }

    #[test]
    fn create_filespec_rejects_a_foreign_handle_without_registering_its_ref() {
        let mut source = open_minimal();
        let foreign = source.get_object_handle(ObjectRef::new(99, 0));
        let mut destination = open_minimal();

        assert!(FileSpec::create_file_spec(&mut destination, b"foreign.bin", foreign).is_err());
        assert_eq!(
            EmbeddedFileStream::create_ef_stream(&mut destination, b"payload")
                .unwrap()
                .object_ref(),
            Some(ObjectRef::new(4, 0)),
            "rejecting a foreign factory input must not register its object number"
        );
    }

    #[test]
    fn create_filespec_accepts_a_direct_value_with_a_foreign_descendant() {
        let mut source = open_minimal();
        let foreign = source.get_object_handle(ObjectRef::new(99, 0));
        let direct = ObjectHandle::dictionary(vec![(b"Foreign".to_vec(), foreign)]);
        let mut destination = open_minimal();

        assert!(FileSpec::create_file_spec(&mut destination, b"direct.bin", direct).is_ok());
    }

    // ── Helpers ───────────────────────────────────────────────────────────────

    /// Return the canonical `/EmbeddedFile` stream handle for a Filespec ref.
    fn embedded_file_stream_handle(
        pdf: &mut Pdf<Cursor<Vec<u8>>>,
        fs_ref: ObjectRef,
    ) -> ObjectHandle {
        let filespec = pdf.get_object_handle(fs_ref);
        let mut filespec = FileSpec::new(filespec, pdf).expect("Filespec handle");
        filespec
            .get_embedded_file_stream("")
            .expect("embedded file stream handle")
    }

    // ── Tests: FileSpecBuilder with compress(false) — existing behaviour ───────

    #[test]
    fn builder_uncompressed_round_trip() {
        let mut pdf = open_minimal();
        let raw = b"hello world";
        let fs_ref = FileSpecBuilder::new("test.txt", raw.as_ref())
            .build(&mut pdf)
            .expect("build");

        let stream = embedded_file_stream_handle(&mut pdf, fs_ref);
        let stream_dict = stream.as_stream_dict().expect("stream dictionary");
        // No /Filter in uncompressed stream
        assert!(
            stream_dict.get_key(b"/Filter").is_null(),
            "uncompressed stream must have no /Filter"
        );
        let decoded = stream
            .get_stream_data(crate::DecodeLevel::Generalized)
            .expect("decode");
        assert_eq!(decoded.as_slice(), raw);
    }

    #[test]
    fn builder_compressed_f_and_uf_follow_qpdf_unicode_string_rules() {
        let mut pdf = open_minimal();
        let raw = b"payload";
        let fs_ref = FileSpecBuilder::new("myfile.txt", raw.as_ref())
            .build(&mut pdf)
            .expect("build");

        let fs_dict = resolved_handle(&mut pdf, fs_ref);
        assert!(fs_dict.as_dictionary().is_some(), "expected dictionary");
        let f = fs_dict.get_key(b"/F").as_string().expect("missing /F");
        let uf = fs_dict.get_key(b"/UF").as_string().expect("missing /UF");
        assert_eq!(f, b"myfile.txt", "/F must be the filename");
        assert_eq!(uf, b"myfile.txt", "/UF must use qpdf newUnicodeString");
    }

    #[test]
    fn builder_allows_distinct_ascii_f_and_unicode_uf() {
        let mut pdf = open_minimal();
        let raw = b"payload";
        let fs_ref = FileSpecBuilder::new("____.pdf", raw.as_ref())
            .uf_filename("レポート.pdf")
            .build(&mut pdf)
            .expect("build");

        let fs_dict = resolved_handle(&mut pdf, fs_ref);
        assert!(fs_dict.as_dictionary().is_some(), "expected dictionary");
        let f = fs_dict.get_key(b"/F").as_string().expect("missing /F");
        let uf = fs_dict.get_key(b"/UF").as_string().expect("missing /UF");

        assert_eq!(f, b"____.pdf", "/F must be ASCII fallback");
        assert_eq!(
            uf,
            encode_utf16be("レポート.pdf"),
            "/UF must preserve the Unicode filename"
        );
    }

    // ── Tests: FileSpecBuilder → insert_embedded_file → list ─────────────────

    #[test]
    fn compressed_filespec_retrievable_via_list() {
        let mut pdf = open_minimal();
        let raw = b"retrievable payload";
        let fs_ref = FileSpecBuilder::new("list-test.txt", raw.as_ref())
            .build(&mut pdf)
            .expect("build");
        insert_embedded_file(&mut pdf, b"list-test.txt", fs_ref).expect("insert");

        let entries = list_embedded_files(&mut pdf).expect("list");
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].0, b"list-test.txt");
        assert_eq!(entries[0].1, fs_ref);
    }

    #[test]
    fn existing_attachment_survives_second_insertion() {
        let mut pdf = open_minimal();

        // Insert first attachment (uncompressed for variety)
        let raw1 = b"first attachment";
        let fs1 = FileSpecBuilder::new("first.txt", raw1.as_ref())
            .build(&mut pdf)
            .expect("build first");
        insert_embedded_file(&mut pdf, b"first.txt", fs1).expect("insert first");

        // Insert second attachment (compressed)
        let raw2 = b"second attachment with more data";
        let fs2 = FileSpecBuilder::new("second.txt", raw2.as_ref())
            .build(&mut pdf)
            .expect("build second");
        insert_embedded_file(&mut pdf, b"second.txt", fs2).expect("insert second");

        let entries = list_embedded_files(&mut pdf).expect("list");
        assert_eq!(entries.len(), 2, "both attachments must survive");
        let keys: Vec<&[u8]> = entries.iter().map(|(k, _)| k.as_slice()).collect();
        assert!(
            keys.contains(&b"first.txt".as_ref()),
            "first.txt must be present"
        );
        assert!(
            keys.contains(&b"second.txt".as_ref()),
            "second.txt must be present"
        );
    }

    // ── Tests: add_attachment_from_path ───────────────────────────────────────

    #[test]
    fn add_attachment_from_path_round_trip() {
        let mut pdf = open_minimal();

        // Write a temp file to attach.
        let dir = tempfile::tempdir().expect("tempdir");
        let file_path = dir.path().join("hello.txt");
        let raw = b"Hello from disk!";
        std::fs::write(&file_path, raw).expect("write temp file");

        let fs_ref = add_attachment_from_path(&mut pdf, b"hello.txt", &file_path).expect("attach");

        // Verify retrievable via list_embedded_files
        let entries = list_embedded_files(&mut pdf).expect("list");
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].0, b"hello.txt");
        assert_eq!(entries[0].1, fs_ref);

        // qpdf's addAttachments delegates to createFileSpec/createEFStream;
        // stream compression is selected later by the writer, not this helper.
        let stream = embedded_file_stream_handle(&mut pdf, fs_ref);
        let stream_dict = stream.as_stream_dict().expect("stream dictionary");
        assert!(
            stream_dict.get_key(b"/Filter").is_null(),
            "attachment construction must not install a helper-local filter"
        );
        assert_eq!(
            stream.get_raw_stream_data().expect("raw stream").as_slice(),
            raw
        );
    }

    #[test]
    fn add_attachment_from_path_checksum_and_size() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let file_path = dir.path().join("data.bin");
        let raw = b"deterministic checksum test data";
        std::fs::write(&file_path, raw).expect("write");

        let fs_ref = add_attachment_from_path(&mut pdf, b"data.bin", &file_path).expect("attach");

        let stream = embedded_file_stream_handle(&mut pdf, fs_ref);
        let stream_dict = stream.as_stream_dict().expect("stream dictionary");
        let params = stream_dict.get_key(b"/Params");
        let size = params
            .get_key(b"/Size")
            .as_integer()
            .expect("missing /Params /Size");
        let checksum = params
            .get_key(b"/CheckSum")
            .as_string()
            .expect("missing /Params /CheckSum");
        assert_eq!(
            size,
            raw.len() as i64,
            "/Params /Size must match raw length"
        );
        assert_eq!(
            checksum,
            vec![
                0xcf, 0x5e, 0x73, 0xd1, 0x4d, 0xf5, 0xca, 0xd1, 0x94, 0xb0, 0x9e, 0xe5, 0x79, 0xf2,
                0x54, 0x9d,
            ],
            "/Params /CheckSum must be the MD5 of raw bytes"
        );
    }

    #[test]
    fn add_attachment_from_path_f_and_uf_follow_qpdf_unicode_string_rules() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let file_path = dir.path().join("report.pdf");
        std::fs::write(&file_path, b"fake pdf content").expect("write");

        let fs_ref = add_attachment_from_path(&mut pdf, b"report.pdf", &file_path).expect("attach");

        let fs_dict = resolved_handle(&mut pdf, fs_ref);
        assert!(fs_dict.as_dictionary().is_some(), "expected dict");
        let f = fs_dict.get_key(b"/F").as_string().expect("missing /F");
        let uf = fs_dict.get_key(b"/UF").as_string().expect("missing /UF");
        assert_eq!(f, b"report.pdf", "/F must be basename");
        assert_eq!(uf, b"report.pdf", "/UF must use qpdf's PDFDocEncoding form");
    }

    #[test]
    fn add_attachment_from_path_errors_on_missing_file() {
        let mut pdf = open_minimal();
        let result =
            add_attachment_from_path(&mut pdf, b"missing.txt", "/this/does/not/exist/missing.txt");
        assert!(result.is_err(), "must error when file does not exist");
        let err = result.unwrap_err();
        assert_eq!(
            err.to_string(),
            "open /this/does/not/exist/missing.txt: No such file or directory"
        );
    }

    #[test]
    fn add_attachment_from_path_accepts_non_ascii_basename() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let file_path = dir.path().join("レポート.pdf");
        std::fs::write(&file_path, b"payload").expect("write temp file");

        let fs_ref = add_attachment_from_path(&mut pdf, "レポート.pdf".as_bytes(), &file_path)
            .expect("attach non-ASCII basename");

        let fs_dict = resolved_handle(&mut pdf, fs_ref);
        assert!(fs_dict.as_dictionary().is_some(), "expected dict");
        let f = fs_dict.get_key(b"/F").as_string().expect("missing /F");
        let uf = fs_dict.get_key(b"/UF").as_string().expect("missing /UF");

        assert_eq!(f, b"____.pdf", "/F must be ASCII-safe fallback");
        assert_eq!(
            uf,
            encode_utf16be("レポート.pdf"),
            "/UF must preserve the Unicode basename"
        );
    }

    // ── Tests: extract_attachment / write_attachment / extract_attachment_to_path ─

    #[test]
    fn extract_attachment_small_round_trip() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("small.txt");
        let raw = b"Hello, world!";
        std::fs::write(&path, raw).expect("write");
        add_attachment_from_path(&mut pdf, b"small.txt", &path).expect("attach");

        let extracted = extract_attachment(&mut pdf, b"small.txt").expect("extract");
        assert_eq!(
            extracted.as_slice(),
            raw.as_ref(),
            "small file round-trip must match"
        );
    }

    #[test]
    fn extract_attachment_large_round_trip() {
        // 128 KiB of repeating pseudo-random-ish bytes — exercises compressor splits.
        let raw: Vec<u8> = (0u8..=255).cycle().take(128 * 1024).collect();
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("large.bin");
        std::fs::write(&path, &raw).expect("write");
        add_attachment_from_path(&mut pdf, b"large.bin", &path).expect("attach");

        let extracted = extract_attachment(&mut pdf, b"large.bin").expect("extract");
        assert_eq!(extracted, raw, "large file round-trip must match");
    }

    #[test]
    fn extract_attachment_binary_with_nuls_round_trip() {
        // 4096 bytes including NUL bytes, exercises binary safety.
        let raw: Vec<u8> = (0u8..=255).cycle().take(4096).collect();
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("binary.bin");
        std::fs::write(&path, &raw).expect("write");
        add_attachment_from_path(&mut pdf, b"binary.bin", &path).expect("attach");

        let extracted = extract_attachment(&mut pdf, b"binary.bin").expect("extract");
        assert_eq!(extracted, raw, "binary file round-trip must match");
    }

    #[test]
    fn write_attachment_to_vec_matches_extract() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("vec-test.txt");
        let raw = b"write_attachment test payload";
        std::fs::write(&path, raw).expect("write");
        add_attachment_from_path(&mut pdf, b"vec-test.txt", &path).expect("attach");

        let mut buf = Vec::new();
        write_attachment(&mut pdf, b"vec-test.txt", &mut buf).expect("write_attachment");
        assert_eq!(
            buf.as_slice(),
            raw.as_ref(),
            "write_attachment output must match raw"
        );
    }

    #[test]
    fn extract_attachment_to_path_round_trip() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");

        let src_path = dir.path().join("source.bin");
        let raw: Vec<u8> = (0u8..=255).cycle().take(2048).collect();
        std::fs::write(&src_path, &raw).expect("write source");
        add_attachment_from_path(&mut pdf, b"source.bin", &src_path).expect("attach");

        let out_path = dir.path().join("extracted.bin");
        extract_attachment_to_path(&mut pdf, b"source.bin", &out_path)
            .expect("extract_attachment_to_path");

        let read_back = std::fs::read(&out_path).expect("read back");
        assert_eq!(read_back, raw, "extract_to_path round-trip must match");
    }

    #[test]
    fn extract_attachment_missing_key_is_actionable_error() {
        let mut pdf = open_minimal();
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("real.txt");
        std::fs::write(&path, b"real content").expect("write");
        add_attachment_from_path(&mut pdf, b"real.txt", &path).expect("attach");

        let err =
            extract_attachment(&mut pdf, b"missing-key").expect_err("must error for absent key");
        let msg = err.to_string();
        assert!(
            msg.contains("missing-key"),
            "error message must contain the missing key name, got: {msg}"
        );
        // Available keys hint must be present
        assert!(
            msg.contains("real.txt"),
            "error message must list available keys, got: {msg}"
        );
    }

    #[test]
    fn extract_attachment_from_compat_fixture() {
        // attachment-two-page.pdf contains an attachment under the key "attachment.txt"
        // with an uncompressed size of 95 bytes (from /Params /Size).
        let path = concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../..",
            "/tests/fixtures/compat/attachment-two-page.pdf"
        );
        // The compat fixture is committed to the repo, so a missing file is a
        // real regression — fail loudly instead of silently skipping, which
        // could turn this into a false-positive pass (CodeRabbit).
        let file = std::fs::File::open(path)
            .expect("compat fixture missing: tests/fixtures/compat/attachment-two-page.pdf");
        let mut pdf = crate::Pdf::open(std::io::BufReader::new(file)).expect("open compat fixture");

        let entries = crate::embedded_files::list_embedded_files(&mut pdf).expect("list");
        assert!(
            !entries.is_empty(),
            "fixture must have at least one attachment"
        );

        // Use the first available key.
        let key = entries[0].0.clone();
        let extracted = extract_attachment(&mut pdf, &key).expect("extract from compat fixture");
        assert!(!extracted.is_empty(), "extracted bytes must be non-empty");

        // The fixture reports /Params /Size 95 — the extracted bytes must match.
        let mut fs = FileSpec::new(pdf.get_object_handle(entries[0].1), &mut pdf).unwrap();
        let ef = fs
            .embedded_file()
            .expect("embedded_file")
            .expect("must have embedded file");
        let reported_size = ef.size().expect("size").expect("size must be present");
        assert_eq!(
            extracted.len() as i64,
            reported_size,
            "extracted length must equal /Params /Size"
        );
    }
}
// cov:ignore-end