pivot-pdf 0.6.0

A low-overhead PDF generation library for reports, invoices, and documents
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
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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::Path;

use flate2::write::ZlibEncoder;
use flate2::Compression;

use crate::fonts::{BuiltinFont, FontRef, TrueTypeFontId};
use crate::graphics::Color;
use crate::images::{self, ImageData, ImageFit, ImageFormat, ImageId};
use crate::objects::{ObjId, PdfObject};
use crate::tables::{Row, Table, TableCursor};
use crate::textflow::{FitResult, Rect, TextFlow, TextStyle};
use crate::truetype::TrueTypeFont;
use crate::writer::PdfWriter;

const CATALOG_OBJ: ObjId = ObjId(1, 0);
const PAGES_OBJ: ObjId = ObjId(2, 0);
const FIRST_PAGE_OBJ_NUM: u32 = 3;

/// Pre-allocated object IDs for an image XObject.
struct ImageObjIds {
    xobject: ObjId,
    smask: Option<ObjId>,
    pdf_name: String,
}

/// Pre-allocated object IDs for a TrueType font's PDF objects.
struct TrueTypeFontObjIds {
    type0: ObjId,
    cid_font: ObjId,
    descriptor: ObjId,
    font_file: ObjId,
    tounicode: ObjId,
}

/// Accumulated record for a completed page.
/// Page dictionaries are deferred until `end_document()` so that
/// overlay content streams (e.g. page numbers) can be appended
/// after all pages have been written.
struct PageRecord {
    obj_id: ObjId,
    /// Content stream IDs: first is the main stream, any beyond that are overlays.
    content_ids: Vec<ObjId>,
    width: f64,
    height: f64,
    used_fonts: BTreeSet<BuiltinFont>,
    used_truetype_fonts: BTreeSet<usize>,
    used_images: BTreeSet<usize>,
}

/// High-level API for building PDF documents.
///
/// Generic over `Write` so it works with files (`BufWriter<File>`),
/// in-memory buffers (`Vec<u8>`), or any other writer.
///
/// Pages are written incrementally: `end_page()` flushes page data
/// to the writer and frees page content from memory. This keeps
/// memory usage low even for documents with hundreds of pages.
pub struct PdfDocument<W: Write> {
    writer: PdfWriter<W>,
    info: Vec<(String, String)>,
    page_records: Vec<PageRecord>,
    current_page: Option<PageBuilder>,
    next_obj_num: u32,
    /// Maps each used builtin font to its written ObjId.
    font_obj_ids: BTreeMap<BuiltinFont, ObjId>,
    /// Loaded TrueType fonts.
    truetype_fonts: Vec<TrueTypeFont>,
    /// Pre-allocated ObjIds for TrueType fonts (by index).
    truetype_font_obj_ids: BTreeMap<usize, TrueTypeFontObjIds>,
    /// Next font number for PDF resource names (F15, F16, ...).
    next_font_num: u32,
    /// Whether to compress stream objects with FlateDecode.
    compress: bool,
    /// Loaded images.
    images: Vec<ImageData>,
    /// Pre-allocated ObjIds for images (by index).
    image_obj_ids: BTreeMap<usize, ImageObjIds>,
    /// Images whose XObjects have already been written.
    written_images: BTreeSet<usize>,
    /// Next image number for PDF resource names (Im1, Im2, ...).
    next_image_num: u32,
}

struct PageBuilder {
    width: f64,
    height: f64,
    content_ops: Vec<u8>,
    used_fonts: BTreeSet<BuiltinFont>,
    used_truetype_fonts: BTreeSet<usize>,
    used_images: BTreeSet<usize>,
    /// When `Some(idx)`, this builder is adding an overlay to `page_records[idx]`
    /// rather than creating a new page.
    overlay_for: Option<usize>,
}

impl PdfDocument<BufWriter<File>> {
    /// Create a new PDF document that writes to a file.
    pub fn create<P: AsRef<Path>>(path: P) -> io::Result<Self> {
        let file = File::create(path)?;
        Self::new(BufWriter::new(file))
    }
}

impl<W: Write> PdfDocument<W> {
    /// Create a new PDF document that writes to the given writer.
    /// Writes the PDF header immediately.
    pub fn new(writer: W) -> io::Result<Self> {
        let mut pdf_writer = PdfWriter::new(writer);
        pdf_writer.write_header()?;

        Ok(PdfDocument {
            writer: pdf_writer,
            info: Vec::new(),
            page_records: Vec::new(),
            current_page: None,
            next_obj_num: FIRST_PAGE_OBJ_NUM,
            font_obj_ids: BTreeMap::new(),
            truetype_fonts: Vec::new(),
            truetype_font_obj_ids: BTreeMap::new(),
            next_font_num: 15,
            compress: false,
            images: Vec::new(),
            image_obj_ids: BTreeMap::new(),
            written_images: BTreeSet::new(),
            next_image_num: 1,
        })
    }

    /// Set a document info entry (e.g. "Creator", "Title").
    pub fn set_info(&mut self, key: &str, value: &str) -> &mut Self {
        self.info.push((key.to_string(), value.to_string()));
        self
    }

    /// Enable or disable FlateDecode compression for stream objects.
    /// When enabled, page content, embedded fonts, and ToUnicode CMaps
    /// are compressed, typically reducing file size by 50-80%.
    /// Disabled by default.
    pub fn set_compression(&mut self, enabled: bool) -> &mut Self {
        self.compress = enabled;
        self
    }

    /// Load a TrueType font from a file path.
    /// Returns a FontRef that can be used in TextStyle.
    pub fn load_font_file<P: AsRef<Path>>(&mut self, path: P) -> Result<FontRef, String> {
        let data =
            std::fs::read(path.as_ref()).map_err(|e| format!("Failed to read font file: {}", e))?;
        self.load_font_bytes(data)
    }

    /// Load a TrueType font from raw bytes.
    /// Returns a FontRef that can be used in TextStyle.
    pub fn load_font_bytes(&mut self, data: Vec<u8>) -> Result<FontRef, String> {
        let font_num = self.next_font_num;
        self.next_font_num += 1;
        let font = TrueTypeFont::from_bytes(data, font_num)?;
        let idx = self.truetype_fonts.len();
        self.truetype_fonts.push(font);
        Ok(FontRef::TrueType(TrueTypeFontId(idx)))
    }

    /// Returns the number of completed pages (pages for which `end_page` has been called).
    pub fn page_count(&self) -> usize {
        self.page_records.len()
    }

    /// Begin a new page with the given dimensions in points.
    /// If a page is currently open, it is automatically closed.
    pub fn begin_page(&mut self, width: f64, height: f64) -> &mut Self {
        if self.current_page.is_some() {
            let _ = self.end_page();
        }
        self.current_page = Some(PageBuilder {
            width,
            height,
            content_ops: Vec::new(),
            used_fonts: BTreeSet::new(),
            used_truetype_fonts: BTreeSet::new(),
            used_images: BTreeSet::new(),
            overlay_for: None,
        });
        self
    }

    /// Open a completed page for editing (1-indexed).
    ///
    /// Used for adding overlay content such as page numbers ("Page X of Y")
    /// after all pages have been written. The overlay content is written as
    /// an additional content stream appended to the page's `/Contents` array.
    ///
    /// If a page is currently open, it is automatically closed first.
    ///
    /// Returns an error if `page_num` is out of range.
    pub fn open_page(&mut self, page_num: usize) -> io::Result<()> {
        if page_num == 0 || page_num > self.page_records.len() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                format!(
                    "open_page: page_num {} out of range (1..={})",
                    page_num,
                    self.page_records.len()
                ),
            ));
        }

        if self.current_page.is_some() {
            self.end_page()?;
        }

        let idx = page_num - 1;
        let width = self.page_records[idx].width;
        let height = self.page_records[idx].height;

        self.current_page = Some(PageBuilder {
            width,
            height,
            content_ops: Vec::new(),
            used_fonts: BTreeSet::new(),
            used_truetype_fonts: BTreeSet::new(),
            used_images: BTreeSet::new(),
            overlay_for: Some(idx),
        });

        Ok(())
    }

    /// Place text at position (x, y) using default 12pt Helvetica.
    /// Coordinates use PDF's default bottom-left origin.
    pub fn place_text(&mut self, text: &str, x: f64, y: f64) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("place_text called with no open page");
        page.used_fonts.insert(BuiltinFont::Helvetica);
        let escaped = crate::writer::escape_pdf_string(text);
        let ops = format!(
            "BT\n/F1 12 Tf\n{} {} Td\n({}) Tj\nET\n",
            format_coord(x),
            format_coord(y),
            escaped,
        );
        page.content_ops.extend_from_slice(ops.as_bytes());
        self
    }

    /// Place text at position (x, y) with the given style.
    /// Coordinates use PDF's default bottom-left origin.
    pub fn place_text_styled(
        &mut self,
        text: &str,
        x: f64,
        y: f64,
        style: &TextStyle,
    ) -> &mut Self {
        // Encode text before borrowing page mutably
        let (font_name, text_op) = match style.font {
            FontRef::Builtin(b) => {
                let escaped = crate::writer::escape_pdf_string(text);
                (b.pdf_name().to_string(), format!("({}) Tj", escaped))
            }
            FontRef::TrueType(id) => {
                let font = &mut self.truetype_fonts[id.0];
                let hex = font.encode_text_hex(text);
                (font.pdf_name.clone(), format!("{} Tj", hex))
            }
        };

        let page = self
            .current_page
            .as_mut()
            .expect("place_text_styled called with no open page");

        match style.font {
            FontRef::Builtin(b) => {
                page.used_fonts.insert(b);
            }
            FontRef::TrueType(id) => {
                page.used_truetype_fonts.insert(id.0);
            }
        }

        let ops = format!(
            "BT\n/{} {} Tf\n{} {} Td\n{}\nET\n",
            font_name,
            format_coord(style.font_size),
            format_coord(x),
            format_coord(y),
            text_op,
        );
        page.content_ops.extend_from_slice(ops.as_bytes());
        self
    }

    /// Fit a TextFlow into a bounding rectangle on the current
    /// page. The flow's cursor advances so subsequent calls
    /// continue where it left off (for multi-page flow).
    pub fn fit_textflow(&mut self, flow: &mut TextFlow, rect: &Rect) -> io::Result<FitResult> {
        let (ops, result, used_fonts) = flow.generate_content_ops(rect, &mut self.truetype_fonts);

        let page = self
            .current_page
            .as_mut()
            .expect("fit_textflow called with no open page");
        page.content_ops.extend_from_slice(&ops);
        page.used_fonts.extend(used_fonts.builtin);
        page.used_truetype_fonts.extend(used_fonts.truetype);
        Ok(result)
    }

    /// Place a single table row on the current page.
    ///
    /// `cursor` tracks the current Y position within the page. Pass the same
    /// cursor to successive calls; call `cursor.reset()` when starting a new page.
    ///
    /// Returns:
    /// - `Stop`     — row placed; advance to the next row.
    /// - `BoxFull`  — page full; end the page, begin a new one, reset the cursor, retry.
    /// - `BoxEmpty` — rect too small for this row even from the top; skip or abort.
    pub fn fit_row(
        &mut self,
        table: &Table,
        row: &Row,
        cursor: &mut TableCursor,
    ) -> io::Result<FitResult> {
        let total_span: usize = row.cells.iter().map(|c| c.col_span.max(1)).sum();
        if total_span != table.columns.len() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                format!(
                    "row col_span sum ({}) must equal table column count ({})",
                    total_span,
                    table.columns.len()
                ),
            ));
        }

        let (ops, result, used_fonts) =
            table.generate_row_ops(row, cursor, &mut self.truetype_fonts);

        let page = self
            .current_page
            .as_mut()
            .expect("fit_row called with no open page");
        page.content_ops.extend_from_slice(&ops);
        page.used_fonts.extend(used_fonts.builtin);
        page.used_truetype_fonts.extend(used_fonts.truetype);
        Ok(result)
    }

    // -------------------------------------------------------
    // Image operations
    // -------------------------------------------------------

    /// Load an image from a file path.
    /// Returns an ImageId that can be used with `place_image`.
    pub fn load_image_file<P: AsRef<Path>>(&mut self, path: P) -> Result<ImageId, String> {
        let data = std::fs::read(path.as_ref())
            .map_err(|e| format!("Failed to read image file: {}", e))?;
        self.load_image_bytes(data)
    }

    /// Load an image from raw bytes (JPEG or PNG).
    /// Returns an ImageId that can be used with `place_image`.
    pub fn load_image_bytes(&mut self, data: Vec<u8>) -> Result<ImageId, String> {
        let image_data = images::load_image(data)?;
        let idx = self.images.len();
        self.images.push(image_data);
        Ok(ImageId(idx))
    }

    /// Place an image on the current page within the given bounding rect.
    pub fn place_image(&mut self, image: &ImageId, rect: &Rect, fit: ImageFit) -> &mut Self {
        let idx = image.0;
        let img = &self.images[idx];
        let page_height = self
            .current_page
            .as_ref()
            .expect("place_image called with no open page")
            .height;

        let placement = images::calculate_placement(img.width, img.height, rect, fit, page_height);

        self.ensure_image_obj_ids(idx);
        let pdf_name = self.image_obj_ids[&idx].pdf_name.clone();

        let page = self
            .current_page
            .as_mut()
            .expect("place_image called with no open page");
        page.used_images.insert(idx);

        // Build content stream operators
        let mut ops = String::new();
        ops.push_str("q\n");

        // Clipping (for Fill mode)
        if let Some(clip) = &placement.clip {
            ops.push_str(&format!(
                "{} {} {} {} re W n\n",
                format_coord(clip.x),
                format_coord(clip.y),
                format_coord(clip.width),
                format_coord(clip.height),
            ));
        }

        // Transformation matrix: scale and position
        // cm matrix: [width 0 0 height x y]
        ops.push_str(&format!(
            "{} 0 0 {} {} {} cm\n",
            format_coord(placement.width),
            format_coord(placement.height),
            format_coord(placement.x),
            format_coord(placement.y),
        ));

        // Paint the image
        ops.push_str(&format!("/{} Do\n", pdf_name));
        ops.push_str("Q\n");

        page.content_ops.extend_from_slice(ops.as_bytes());
        self
    }

    /// Pre-allocate ObjIds for an image if not yet done.
    fn ensure_image_obj_ids(&mut self, idx: usize) {
        if self.image_obj_ids.contains_key(&idx) {
            return;
        }
        let xobject = ObjId(self.next_obj_num, 0);
        self.next_obj_num += 1;

        let smask = if self.images[idx].smask_data.is_some() {
            let id = ObjId(self.next_obj_num, 0);
            self.next_obj_num += 1;
            Some(id)
        } else {
            None
        };

        let pdf_name = format!("Im{}", self.next_image_num);
        self.next_image_num += 1;

        self.image_obj_ids.insert(
            idx,
            ImageObjIds {
                xobject,
                smask,
                pdf_name,
            },
        );
    }

    /// Write the image XObject stream(s) for the given image index.
    fn write_image_xobject(&mut self, idx: usize) -> io::Result<()> {
        if self.written_images.contains(&idx) {
            return Ok(());
        }

        let img = &self.images[idx];
        let obj_ids = &self.image_obj_ids[&idx];
        let xobject_id = obj_ids.xobject;
        let smask_id = obj_ids.smask;

        // Write SMask XObject first if alpha data exists
        if let (Some(smask_obj_id), Some(smask_data)) = (smask_id, img.smask_data.as_ref()) {
            let smask_stream = self.make_stream(
                vec![
                    ("Type", PdfObject::name("XObject")),
                    ("Subtype", PdfObject::name("Image")),
                    ("Width", PdfObject::Integer(img.width as i64)),
                    ("Height", PdfObject::Integer(img.height as i64)),
                    ("ColorSpace", PdfObject::name("DeviceGray")),
                    ("BitsPerComponent", PdfObject::Integer(8)),
                ],
                smask_data.clone(),
            );
            self.writer.write_object(smask_obj_id, &smask_stream)?;
        }

        // Build image XObject dict entries
        let mut entries: Vec<(&str, PdfObject)> = vec![
            ("Type", PdfObject::name("XObject")),
            ("Subtype", PdfObject::name("Image")),
            ("Width", PdfObject::Integer(img.width as i64)),
            ("Height", PdfObject::Integer(img.height as i64)),
            ("ColorSpace", PdfObject::name(img.color_space.pdf_name())),
            (
                "BitsPerComponent",
                PdfObject::Integer(img.bits_per_component as i64),
            ),
        ];

        if let Some(smask_obj_id) = smask_id {
            entries.push(("SMask", PdfObject::Reference(smask_obj_id)));
        }

        // For JPEG: embed raw data with DCTDecode, never double-compress
        // For PNG (decoded pixels): use make_stream for optional FlateDecode
        let image_obj = match img.format {
            ImageFormat::Jpeg => {
                entries.push(("Filter", PdfObject::name("DCTDecode")));
                PdfObject::stream(entries, img.data.clone())
            }
            ImageFormat::Png => self.make_stream(entries, img.data.clone()),
        };

        self.writer.write_object(xobject_id, &image_obj)?;
        self.written_images.insert(idx);
        Ok(())
    }

    // -------------------------------------------------------
    // Graphics operations
    // -------------------------------------------------------

    /// Set the stroke color (PDF `RG` operator).
    pub fn set_stroke_color(&mut self, color: Color) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("set_stroke_color called with no open page");
        let ops = format!(
            "{} {} {} RG\n",
            format_coord(color.r),
            format_coord(color.g),
            format_coord(color.b),
        );
        page.content_ops.extend_from_slice(ops.as_bytes());
        self
    }

    /// Set the fill color (PDF `rg` operator).
    pub fn set_fill_color(&mut self, color: Color) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("set_fill_color called with no open page");
        let ops = format!(
            "{} {} {} rg\n",
            format_coord(color.r),
            format_coord(color.g),
            format_coord(color.b),
        );
        page.content_ops.extend_from_slice(ops.as_bytes());
        self
    }

    /// Set the line width (PDF `w` operator).
    pub fn set_line_width(&mut self, width: f64) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("set_line_width called with no open page");
        let ops = format!("{} w\n", format_coord(width));
        page.content_ops.extend_from_slice(ops.as_bytes());
        self
    }

    /// Move to a point without drawing (PDF `m` operator).
    pub fn move_to(&mut self, x: f64, y: f64) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("move_to called with no open page");
        let ops = format!("{} {} m\n", format_coord(x), format_coord(y));
        page.content_ops.extend_from_slice(ops.as_bytes());
        self
    }

    /// Draw a line from the current point (PDF `l` operator).
    pub fn line_to(&mut self, x: f64, y: f64) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("line_to called with no open page");
        let ops = format!("{} {} l\n", format_coord(x), format_coord(y));
        page.content_ops.extend_from_slice(ops.as_bytes());
        self
    }

    /// Append a rectangle to the path (PDF `re` operator).
    pub fn rect(&mut self, x: f64, y: f64, width: f64, height: f64) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("rect called with no open page");
        let ops = format!(
            "{} {} {} {} re\n",
            format_coord(x),
            format_coord(y),
            format_coord(width),
            format_coord(height),
        );
        page.content_ops.extend_from_slice(ops.as_bytes());
        self
    }

    /// Close the current subpath (PDF `h` operator).
    pub fn close_path(&mut self) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("close_path called with no open page");
        page.content_ops.extend_from_slice(b"h\n");
        self
    }

    /// Stroke the current path (PDF `S` operator).
    pub fn stroke(&mut self) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("stroke called with no open page");
        page.content_ops.extend_from_slice(b"S\n");
        self
    }

    /// Fill the current path (PDF `f` operator).
    pub fn fill(&mut self) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("fill called with no open page");
        page.content_ops.extend_from_slice(b"f\n");
        self
    }

    /// Fill and stroke the current path (PDF `B` operator).
    pub fn fill_stroke(&mut self) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("fill_stroke called with no open page");
        page.content_ops.extend_from_slice(b"B\n");
        self
    }

    /// Save the graphics state (PDF `q` operator).
    pub fn save_state(&mut self) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("save_state called with no open page");
        page.content_ops.extend_from_slice(b"q\n");
        self
    }

    /// Restore the graphics state (PDF `Q` operator).
    pub fn restore_state(&mut self) -> &mut Self {
        let page = self
            .current_page
            .as_mut()
            .expect("restore_state called with no open page");
        page.content_ops.extend_from_slice(b"Q\n");
        self
    }

    /// Build a stream object, optionally compressing the data with FlateDecode.
    fn make_stream(&self, mut dict_entries: Vec<(&str, PdfObject)>, data: Vec<u8>) -> PdfObject {
        if self.compress {
            let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
            encoder.write_all(&data).expect("flate2 in-memory write");
            let compressed = encoder.finish().expect("flate2 finish");
            dict_entries.push(("Filter", PdfObject::name("FlateDecode")));
            PdfObject::stream(dict_entries, compressed)
        } else {
            PdfObject::stream(dict_entries, data)
        }
    }

    /// Ensure a builtin font's dictionary object has been written.
    fn ensure_font_written(&mut self, font: BuiltinFont) -> io::Result<ObjId> {
        if let Some(&id) = self.font_obj_ids.get(&font) {
            return Ok(id);
        }
        let id = ObjId(self.next_obj_num, 0);
        self.next_obj_num += 1;
        let obj = PdfObject::dict(vec![
            ("Type", PdfObject::name("Font")),
            ("Subtype", PdfObject::name("Type1")),
            ("BaseFont", PdfObject::name(font.pdf_base_name())),
        ]);
        self.writer.write_object(id, &obj)?;
        self.font_obj_ids.insert(font, id);
        Ok(id)
    }

    /// Pre-allocate ObjIds for a TrueType font if not yet done.
    fn ensure_tt_font_obj_ids(&mut self, idx: usize) -> &TrueTypeFontObjIds {
        if !self.truetype_font_obj_ids.contains_key(&idx) {
            let type0 = ObjId(self.next_obj_num, 0);
            self.next_obj_num += 1;
            let cid_font = ObjId(self.next_obj_num, 0);
            self.next_obj_num += 1;
            let descriptor = ObjId(self.next_obj_num, 0);
            self.next_obj_num += 1;
            let font_file = ObjId(self.next_obj_num, 0);
            self.next_obj_num += 1;
            let tounicode = ObjId(self.next_obj_num, 0);
            self.next_obj_num += 1;
            self.truetype_font_obj_ids.insert(
                idx,
                TrueTypeFontObjIds {
                    type0,
                    cid_font,
                    descriptor,
                    font_file,
                    tounicode,
                },
            );
        }
        &self.truetype_font_obj_ids[&idx]
    }

    /// End the current page. Writes the content stream to the writer
    /// and frees page content from memory. The page dictionary is
    /// deferred until `end_document()` so overlay streams can be added.
    pub fn end_page(&mut self) -> io::Result<()> {
        let page = self
            .current_page
            .take()
            .expect("end_page called with no open page");

        // Write builtin font objects for any not yet written
        for &font in &page.used_fonts {
            self.ensure_font_written(font)?;
        }

        // Pre-allocate ObjIds for TrueType fonts used on this page
        for &idx in &page.used_truetype_fonts {
            self.ensure_tt_font_obj_ids(idx);
        }

        // Write image XObjects for images used on this page
        let used_images: Vec<usize> = page.used_images.iter().copied().collect();
        for idx in &used_images {
            self.write_image_xobject(*idx)?;
        }

        let content_id = ObjId(self.next_obj_num, 0);
        self.next_obj_num += 1;

        // Write content stream immediately (keeps memory usage low)
        let content_stream = self.make_stream(vec![], page.content_ops);
        self.writer.write_object(content_id, &content_stream)?;

        match page.overlay_for {
            None => {
                // New page: pre-allocate the page dict ObjId and store the record.
                // The page dictionary itself is written in write_page_dicts().
                let page_id = ObjId(self.next_obj_num, 0);
                self.next_obj_num += 1;

                self.page_records.push(PageRecord {
                    obj_id: page_id,
                    content_ids: vec![content_id],
                    width: page.width,
                    height: page.height,
                    used_fonts: page.used_fonts,
                    used_truetype_fonts: page.used_truetype_fonts,
                    used_images: page.used_images,
                });
            }
            Some(idx) => {
                // Overlay: append content stream to existing page record.
                let record = &mut self.page_records[idx];
                record.content_ids.push(content_id);
                record.used_fonts.extend(page.used_fonts);
                record.used_truetype_fonts.extend(page.used_truetype_fonts);
                record.used_images.extend(page.used_images);
            }
        }

        Ok(())
    }

    /// Build the font resource dictionary for a page.
    fn build_font_dict(&self, used_fonts: &[BuiltinFont], used_truetype: &[usize]) -> PdfObject {
        let mut entries: Vec<(String, PdfObject)> = used_fonts
            .iter()
            .map(|f| {
                (
                    f.pdf_name().to_string(),
                    PdfObject::Reference(self.font_obj_ids[f]),
                )
            })
            .collect();

        for &idx in used_truetype {
            let name = self.truetype_fonts[idx].pdf_name.clone();
            let type0_id = self.truetype_font_obj_ids[&idx].type0;
            entries.push((name, PdfObject::Reference(type0_id)));
        }

        PdfObject::Dictionary(entries)
    }

    /// Build the resource dictionary for a page.
    fn build_resource_dict(
        &self,
        used_fonts: &[BuiltinFont],
        used_truetype: &[usize],
        used_images: &[usize],
    ) -> PdfObject {
        let font_dict = self.build_font_dict(used_fonts, used_truetype);

        let xobject_entries: Vec<(String, PdfObject)> = used_images
            .iter()
            .filter_map(|idx| {
                self.image_obj_ids
                    .get(idx)
                    .map(|ids| (ids.pdf_name.clone(), PdfObject::Reference(ids.xobject)))
            })
            .collect();

        let mut resource_entries: Vec<(String, PdfObject)> = vec![("Font".to_string(), font_dict)];
        if !xobject_entries.is_empty() {
            resource_entries.push((
                "XObject".to_string(),
                PdfObject::Dictionary(xobject_entries),
            ));
        }

        PdfObject::Dictionary(resource_entries)
    }

    /// Build the `/Contents` entry: single reference for one stream, array for multiple.
    fn build_contents(content_ids: &[ObjId]) -> PdfObject {
        if content_ids.len() == 1 {
            PdfObject::Reference(content_ids[0])
        } else {
            PdfObject::array(
                content_ids
                    .iter()
                    .map(|id| PdfObject::Reference(*id))
                    .collect(),
            )
        }
    }

    /// Write page dictionaries for all pages. Called from `end_document()`
    /// after all content streams (including overlays) have been written.
    fn write_page_dicts(&mut self) -> io::Result<()> {
        for i in 0..self.page_records.len() {
            // Copy out page data to release the borrow before writing
            let obj_id = self.page_records[i].obj_id;
            let content_ids: Vec<ObjId> =
                self.page_records[i].content_ids.iter().copied().collect();
            let width = self.page_records[i].width;
            let height = self.page_records[i].height;
            let used_fonts: Vec<BuiltinFont> =
                self.page_records[i].used_fonts.iter().copied().collect();
            let used_truetype: Vec<usize> = self.page_records[i]
                .used_truetype_fonts
                .iter()
                .copied()
                .collect();
            let used_images: Vec<usize> =
                self.page_records[i].used_images.iter().copied().collect();

            let resources = self.build_resource_dict(&used_fonts, &used_truetype, &used_images);
            let contents = Self::build_contents(&content_ids);

            let page_dict = PdfObject::dict(vec![
                ("Type", PdfObject::name("Page")),
                ("Parent", PdfObject::Reference(PAGES_OBJ)),
                (
                    "MediaBox",
                    PdfObject::array(vec![
                        PdfObject::Integer(0),
                        PdfObject::Integer(0),
                        PdfObject::Real(width),
                        PdfObject::Real(height),
                    ]),
                ),
                ("Contents", contents),
                ("Resources", resources),
            ]);
            self.writer.write_object(obj_id, &page_dict)?;
        }
        Ok(())
    }

    /// Write all TrueType font objects. Called during
    /// end_document, after all pages have been written.
    fn write_truetype_fonts(&mut self) -> io::Result<()> {
        let indices: Vec<usize> = self.truetype_font_obj_ids.keys().copied().collect();

        for idx in indices {
            let obj_ids_type0 = self.truetype_font_obj_ids[&idx].type0;
            let obj_ids_cid = self.truetype_font_obj_ids[&idx].cid_font;
            let obj_ids_desc = self.truetype_font_obj_ids[&idx].descriptor;
            let obj_ids_file = self.truetype_font_obj_ids[&idx].font_file;
            let obj_ids_tounicode = self.truetype_font_obj_ids[&idx].tounicode;

            let font = &self.truetype_fonts[idx];

            // 1. FontFile2 stream (raw .ttf data)
            let original_len = font.font_data.len() as i64;
            let font_file_stream = self.make_stream(
                vec![("Length1", PdfObject::Integer(original_len))],
                font.font_data.clone(),
            );
            self.writer.write_object(obj_ids_file, &font_file_stream)?;

            // 2. FontDescriptor (values scaled to PDF units: 1/1000)
            let descriptor = PdfObject::dict(vec![
                ("Type", PdfObject::name("FontDescriptor")),
                ("FontName", PdfObject::name(&font.postscript_name)),
                ("Flags", PdfObject::Integer(font.flags as i64)),
                (
                    "FontBBox",
                    PdfObject::array(vec![
                        PdfObject::Integer(font.scale_to_pdf(font.bbox[0])),
                        PdfObject::Integer(font.scale_to_pdf(font.bbox[1])),
                        PdfObject::Integer(font.scale_to_pdf(font.bbox[2])),
                        PdfObject::Integer(font.scale_to_pdf(font.bbox[3])),
                    ]),
                ),
                ("ItalicAngle", PdfObject::Real(font.italic_angle)),
                ("Ascent", PdfObject::Integer(font.scale_to_pdf(font.ascent))),
                (
                    "Descent",
                    PdfObject::Integer(font.scale_to_pdf(font.descent)),
                ),
                (
                    "CapHeight",
                    PdfObject::Integer(font.scale_to_pdf(font.cap_height)),
                ),
                ("StemV", PdfObject::Integer(font.scale_to_pdf(font.stem_v))),
                ("FontFile2", PdfObject::Reference(obj_ids_file)),
            ]);
            self.writer.write_object(obj_ids_desc, &descriptor)?;

            // 3. CIDFontType2
            let w_array = font.build_w_array();
            let cid_font = PdfObject::dict(vec![
                ("Type", PdfObject::name("Font")),
                ("Subtype", PdfObject::name("CIDFontType2")),
                ("BaseFont", PdfObject::name(&font.postscript_name)),
                (
                    "CIDSystemInfo",
                    PdfObject::dict(vec![
                        ("Registry", PdfObject::literal_string("Adobe")),
                        ("Ordering", PdfObject::literal_string("Identity")),
                        ("Supplement", PdfObject::Integer(0)),
                    ]),
                ),
                ("FontDescriptor", PdfObject::Reference(obj_ids_desc)),
                ("DW", PdfObject::Integer(font.default_width_pdf())),
                ("W", PdfObject::Array(w_array)),
            ]);
            self.writer.write_object(obj_ids_cid, &cid_font)?;

            // 4. ToUnicode CMap stream
            let tounicode_data = font.build_tounicode_cmap();
            let tounicode = self.make_stream(vec![], tounicode_data);
            self.writer.write_object(obj_ids_tounicode, &tounicode)?;

            // 5. Type0 font (top-level)
            let type0 = PdfObject::dict(vec![
                ("Type", PdfObject::name("Font")),
                ("Subtype", PdfObject::name("Type0")),
                ("BaseFont", PdfObject::name(&font.postscript_name)),
                ("Encoding", PdfObject::name("Identity-H")),
                (
                    "DescendantFonts",
                    PdfObject::array(vec![PdfObject::Reference(obj_ids_cid)]),
                ),
                ("ToUnicode", PdfObject::Reference(obj_ids_tounicode)),
            ]);
            self.writer.write_object(obj_ids_type0, &type0)?;
        }

        Ok(())
    }

    /// Finish the document. Writes page dictionaries, the catalog, pages tree,
    /// info dictionary, xref table, and trailer.
    /// Consumes self -- no further operations are possible.
    pub fn end_document(mut self) -> io::Result<W> {
        // Auto-close any open page
        if self.current_page.is_some() {
            self.end_page()?;
        }

        // Write page dictionaries (deferred so overlays can be accumulated first)
        self.write_page_dicts()?;

        // Write TrueType font objects (deferred until now)
        self.write_truetype_fonts()?;

        // Write info dictionary if any entries exist
        let info_id = if !self.info.is_empty() {
            let id = ObjId(self.next_obj_num, 0);
            self.next_obj_num += 1;
            let entries: Vec<(&str, PdfObject)> = self
                .info
                .iter()
                .map(|(k, v)| (k.as_str(), PdfObject::literal_string(v)))
                .collect();
            let info_obj = PdfObject::dict(entries);
            self.writer.write_object(id, &info_obj)?;
            Some(id)
        } else {
            None
        };

        // Write pages tree (obj 2)
        let kids: Vec<PdfObject> = self
            .page_records
            .iter()
            .map(|r| PdfObject::Reference(r.obj_id))
            .collect();
        let page_count = self.page_records.len() as i64;
        let pages = PdfObject::dict(vec![
            ("Type", PdfObject::name("Pages")),
            ("Kids", PdfObject::Array(kids)),
            ("Count", PdfObject::Integer(page_count)),
        ]);
        self.writer.write_object(PAGES_OBJ, &pages)?;

        // Write catalog (obj 1)
        let catalog = PdfObject::dict(vec![
            ("Type", PdfObject::name("Catalog")),
            ("Pages", PdfObject::Reference(PAGES_OBJ)),
        ]);
        self.writer.write_object(CATALOG_OBJ, &catalog)?;

        // Write xref and trailer
        self.writer.write_xref_and_trailer(CATALOG_OBJ, info_id)?;

        Ok(self.writer.into_inner())
    }
}

/// Format a coordinate value for PDF content streams.
pub(crate) fn format_coord(v: f64) -> String {
    if v == v.floor() && v.abs() < 1e15 {
        format!("{}", v as i64)
    } else {
        let s = format!("{:.4}", v);
        let s = s.trim_end_matches('0');
        let s = s.trim_end_matches('.');
        s.to_string()
    }
}