oxidize-pdf 2.5.0

A pure Rust PDF generation and manipulation library with zero external dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
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
//! Regression tests for table writing — verify actual PDF output, not just struct construction.
//!
//! Every test in this file MUST:
//! 1. Generate a PDF
//! 2. Parse the output (content stream or re-parse)
//! 3. Verify the content is correct (positions, text, sizes)
//!
//! `assert!(result.is_ok())` alone is NOT acceptable.

use oxidize_pdf::advanced_tables::{
    AdvancedTableBuilder, AdvancedTableExt, CellData, TableRenderer,
};
use oxidize_pdf::text::{Table, TableCell};
use oxidize_pdf::writer::WriterConfig;
use oxidize_pdf::{Color, Document, Page, Result};
use regex::Regex;

/// Helper: generate PDF bytes from a table with compression disabled for inspection
fn render_table_to_bytes(table: &Table) -> Result<Vec<u8>> {
    let mut doc = Document::new();
    let mut page = Page::a4();
    page.add_table(table)?;
    doc.add_page(page);
    let config = WriterConfig {
        compress_streams: false,
        ..WriterConfig::default()
    };
    doc.to_bytes_with_config(config)
}

/// Helper: extract all text position operators from PDF bytes.
/// Matches both `x y Td` and `a b c d x y Tm` (extracts x,y from the last two args of Tm).
fn extract_text_positions(pdf_bytes: &[u8]) -> Vec<(f64, f64)> {
    let content = String::from_utf8_lossy(pdf_bytes);
    let mut positions = Vec::new();

    // Td: x y Td
    let re_td = Regex::new(r"(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+Td").unwrap();
    for cap in re_td.captures_iter(&content) {
        positions.push((cap[1].parse().unwrap(), cap[2].parse().unwrap()));
    }

    // Tm: a b c d e f Tm — e=x, f=y (last two numeric args before Tm)
    let re_tm =
        Regex::new(r"[-\d.]+\s+[-\d.]+\s+[-\d.]+\s+[-\d.]+\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+Tm")
            .unwrap();
    for cap in re_tm.captures_iter(&content) {
        positions.push((cap[1].parse().unwrap(), cap[2].parse().unwrap()));
    }

    positions
}

/// Helper: extract all `x y w h re` rectangle operators from PDF bytes
fn extract_rectangles(pdf_bytes: &[u8]) -> Vec<(f64, f64, f64, f64)> {
    let content = String::from_utf8_lossy(pdf_bytes);
    let re =
        Regex::new(r"(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+re").unwrap();
    re.captures_iter(&content)
        .map(|cap| {
            let x: f64 = cap[1].parse().unwrap();
            let y: f64 = cap[2].parse().unwrap();
            let w: f64 = cap[3].parse().unwrap();
            let h: f64 = cap[4].parse().unwrap();
            (x, y, w, h)
        })
        .collect()
}

/// Helper: extract all `(text) Tj` string operators from PDF bytes
fn extract_text_strings(pdf_bytes: &[u8]) -> Vec<String> {
    let content = String::from_utf8_lossy(pdf_bytes);
    let re = Regex::new(r"\(([^)]*)\)\s+Tj").unwrap();
    re.captures_iter(&content)
        .map(|cap| cap[1].to_string())
        .collect()
}

// =============================================================================
// P1: #172 — Row Order
// =============================================================================

#[test]
fn test_row_order_top_to_bottom() -> Result<()> {
    // Issue #172: rows added first should display first (at top)
    let mut table = Table::new(vec![200.0]);
    table.set_position(50.0, 700.0);

    table.add_row(vec!["Row A".to_string()])?;
    table.add_row(vec!["Row B".to_string()])?;
    table.add_row(vec!["Row C".to_string()])?;

    let pdf_bytes = render_table_to_bytes(&table)?;
    let positions = extract_text_positions(&pdf_bytes);
    let texts = extract_text_strings(&pdf_bytes);

    // Verify all 3 rows are present
    assert!(
        texts.contains(&"Row A".to_string()),
        "PDF should contain 'Row A', found: {:?}",
        texts
    );
    assert!(
        texts.contains(&"Row B".to_string()),
        "PDF should contain 'Row B'"
    );
    assert!(
        texts.contains(&"Row C".to_string()),
        "PDF should contain 'Row C'"
    );

    // Find Y positions for each row's text
    // In PDF, Y increases upward. So the first row (top) should have the HIGHEST Y.
    let row_a_idx = texts.iter().position(|t| t == "Row A").unwrap();
    let row_b_idx = texts.iter().position(|t| t == "Row B").unwrap();
    let row_c_idx = texts.iter().position(|t| t == "Row C").unwrap();

    let y_a = positions[row_a_idx].1;
    let y_b = positions[row_b_idx].1;
    let y_c = positions[row_c_idx].1;

    assert!(
        y_a > y_b,
        "Row A (y={y_a}) should be ABOVE Row B (y={y_b}) — first row added should be at top"
    );
    assert!(
        y_b > y_c,
        "Row B (y={y_b}) should be ABOVE Row C (y={y_c}) — rows should go top to bottom"
    );

    Ok(())
}

#[test]
fn test_row_order_cell_backgrounds_match_text_order() -> Result<()> {
    // Verify cell background rectangles also follow top-to-bottom order
    // Use GridStyle::None to avoid grid border rects mixing with background rects
    use oxidize_pdf::text::table::{GridStyle, TableOptions};

    let mut options = TableOptions::default();
    options.grid_style = GridStyle::None;

    let mut table = Table::new(vec![200.0]);
    table.set_options(options);
    table.set_position(50.0, 700.0);

    let mut cell_a = TableCell::new("Row A".to_string());
    cell_a.set_background_color(Color::rgb(1.0, 0.0, 0.0));
    let mut cell_b = TableCell::new("Row B".to_string());
    cell_b.set_background_color(Color::rgb(0.0, 1.0, 0.0));

    table.add_custom_row(vec![cell_a])?;
    table.add_custom_row(vec![cell_b])?;

    let pdf_bytes = render_table_to_bytes(&table)?;
    let rects = extract_rectangles(&pdf_bytes);

    // With GridStyle::None, only cell background rects should be present
    assert_eq!(
        rects.len(),
        2,
        "Should have exactly 2 rectangles for cell backgrounds, found {}",
        rects.len()
    );

    // Row A background rect (first) should have higher bottom-left Y than Row B (second)
    let rect_a_bottom_y = rects[0].1;
    let rect_b_bottom_y = rects[1].1;

    assert!(
        rect_a_bottom_y > rect_b_bottom_y,
        "Row A rect bottom (y={rect_a_bottom_y}) should be above Row B rect bottom (y={rect_b_bottom_y})"
    );

    Ok(())
}

// =============================================================================
// P2: #170 — Multi-line text (\n) in cells
// =============================================================================

#[test]
fn test_basic_table_multiline_text() -> Result<()> {
    // Issue #170: \n in cell text should produce multiple lines
    let mut table = Table::new(vec![200.0]);
    table.set_position(50.0, 700.0);

    table.add_row(vec!["Line1\nLine2".to_string()])?;

    let pdf_bytes = render_table_to_bytes(&table)?;
    let texts = extract_text_strings(&pdf_bytes);
    let positions = extract_text_positions(&pdf_bytes);

    // Should have 2 separate text operations (one per line)
    assert!(
        texts.contains(&"Line1".to_string()),
        "PDF should contain 'Line1', found: {:?}",
        texts
    );
    assert!(
        texts.contains(&"Line2".to_string()),
        "PDF should contain 'Line2', found: {:?}",
        texts
    );

    // Line1 should be above Line2 (higher Y)
    let idx1 = texts.iter().position(|t| t == "Line1").unwrap();
    let idx2 = texts.iter().position(|t| t == "Line2").unwrap();
    assert!(
        positions[idx1].1 > positions[idx2].1,
        "Line1 (y={}) should be above Line2 (y={})",
        positions[idx1].1,
        positions[idx2].1
    );

    Ok(())
}

#[test]
fn test_advanced_table_multiline_text() -> Result<()> {
    // Issue #170: AdvancedTable should also support \n with text_wrap (default true)
    let table = AdvancedTableBuilder::new()
        .add_column("Col1", 200.0)
        .add_row_cells(vec![CellData::new("Line1\nLine2")])
        .build()
        .map_err(|e| oxidize_pdf::error::PdfError::InvalidOperation(e.to_string()))?;

    let mut doc = Document::new();
    let mut page = Page::a4();
    page.add_advanced_table(&table, 50.0, 700.0)?;
    doc.add_page(page);

    let config = WriterConfig {
        compress_streams: false,
        ..WriterConfig::default()
    };
    let pdf_bytes = doc.to_bytes_with_config(config)?;
    let texts = extract_text_strings(&pdf_bytes);

    // Should have 2 separate text strings
    assert!(
        texts.contains(&"Line1".to_string()),
        "AdvancedTable PDF should contain 'Line1', found: {:?}",
        texts
    );
    assert!(
        texts.contains(&"Line2".to_string()),
        "AdvancedTable PDF should contain 'Line2', found: {:?}",
        texts
    );

    Ok(())
}

// =============================================================================
// P3: #171 — Per-row height in basic Table
// =============================================================================

#[test]
fn test_per_row_height() -> Result<()> {
    // Issue #171: each row should be able to have its own height
    use oxidize_pdf::text::table::{GridStyle, TableOptions};

    let mut options = TableOptions::default();
    options.grid_style = GridStyle::None;
    options.row_height = 0.0; // auto

    let mut table = Table::new(vec![200.0]);
    table.set_options(options);
    table.set_position(50.0, 700.0);

    // Row A: 30pt height
    let cell_a = TableCell::new("Row A".to_string());
    table.add_custom_row(vec![cell_a])?;
    table.set_last_row_height(30.0);

    // Row B: 50pt height
    let cell_b = TableCell::new("Row B".to_string());
    table.add_custom_row(vec![cell_b])?;
    table.set_last_row_height(50.0);

    // Row C: 20pt height
    let cell_c = TableCell::new("Row C".to_string());
    table.add_custom_row(vec![cell_c])?;
    table.set_last_row_height(20.0);

    let pdf_bytes = render_table_to_bytes(&table)?;
    let positions = extract_text_positions(&pdf_bytes);
    let texts = extract_text_strings(&pdf_bytes);

    let idx_a = texts.iter().position(|t| t == "Row A").unwrap();
    let idx_b = texts.iter().position(|t| t == "Row B").unwrap();
    let idx_c = texts.iter().position(|t| t == "Row C").unwrap();

    let y_a = positions[idx_a].1;
    let y_b = positions[idx_b].1;
    let y_c = positions[idx_c].1;

    // Row A is 30pt high, Row B is 50pt high
    // Gap between Row A text and Row B text should be approximately 30pt
    // Gap between Row B text and Row C text should be approximately 50pt
    let gap_ab = y_a - y_b;
    let gap_bc = y_b - y_c;

    assert!(
        (gap_ab - 30.0).abs() < 1.0,
        "Gap between Row A and B should be ~30pt (Row A height), got {gap_ab}"
    );
    assert!(
        (gap_bc - 50.0).abs() < 1.0,
        "Gap between Row B and C should be ~50pt (Row B height), got {gap_bc}"
    );

    // Total table height should be 30+50+20 = 100
    assert!(
        (table.get_height() - 100.0).abs() < 0.01,
        "Total height should be 100pt, got {}",
        table.get_height()
    );

    Ok(())
}

// =============================================================================
// P4: #163 — Colspan/Rowspan in AdvancedTable
// =============================================================================

#[test]
fn test_advanced_table_colspan_cell_positions() -> Result<()> {
    // Issue #163: colspan should correctly position cells
    // 4 columns, each 100pt wide. Row with [Cell(colspan=2), Cell, Cell]
    let table = AdvancedTableBuilder::new()
        .add_column("A", 100.0)
        .add_column("B", 100.0)
        .add_column("C", 100.0)
        .add_column("D", 100.0)
        .add_row_cells(vec![
            CellData::new("Span2").colspan(2),
            CellData::new("CellC"),
            CellData::new("CellD"),
        ])
        .build()
        .map_err(|e| oxidize_pdf::error::PdfError::InvalidOperation(e.to_string()))?;

    let mut doc = Document::new();
    let mut page = Page::a4();
    page.add_advanced_table(&table, 50.0, 700.0)?;
    doc.add_page(page);

    let config = WriterConfig {
        compress_streams: false,
        ..WriterConfig::default()
    };
    let pdf_bytes = doc.to_bytes_with_config(config)?;
    let texts = extract_text_strings(&pdf_bytes);
    let positions = extract_text_positions(&pdf_bytes);

    // All texts should be present
    assert!(
        texts.contains(&"Span2".to_string()),
        "Should contain 'Span2', found: {:?}",
        texts
    );
    assert!(
        texts.contains(&"CellC".to_string()),
        "Should contain 'CellC', found: {:?}",
        texts
    );
    assert!(
        texts.contains(&"CellD".to_string()),
        "Should contain 'CellD', found: {:?}",
        texts
    );

    // Get X positions (skip header row texts)
    let idx_span = texts.iter().position(|t| t == "Span2").unwrap();
    let idx_c = texts.iter().position(|t| t == "CellC").unwrap();
    let idx_d = texts.iter().position(|t| t == "CellD").unwrap();

    let x_span = positions[idx_span].0;
    let x_c = positions[idx_c].0;
    let x_d = positions[idx_d].0;

    // Span2 starts at col 0 (x ~ 50 + padding)
    // CellC starts at col 2 (x ~ 250 + padding), NOT col 1 (x ~ 150)
    // CellD starts at col 3 (x ~ 350 + padding)
    assert!(
        x_c > x_span + 150.0,
        "CellC (x={x_c}) should start at col 2 (~250), not col 1 (~150). Span2 at x={x_span}"
    );
    assert!(
        x_d > x_c + 50.0,
        "CellD (x={x_d}) should be after CellC (x={x_c})"
    );

    Ok(())
}

#[test]
fn test_advanced_table_rowspan_no_overlap() -> Result<()> {
    // Issue #163: rowspan cell should not be overwritten by next row
    // 2 columns. Row 0: [Cell(rowspan=2), Cell]. Row 1: [Cell] (only 1 cell, col 0 occupied)
    let table = AdvancedTableBuilder::new()
        .add_column("A", 150.0)
        .add_column("B", 150.0)
        .add_row_cells(vec![
            CellData::new("SpanDown").rowspan(2),
            CellData::new("R0B"),
        ])
        .add_row_cells(vec![
            CellData::new("R1B"), // Only one cell — col 0 is occupied by rowspan
        ])
        .build()
        .map_err(|e| oxidize_pdf::error::PdfError::InvalidOperation(e.to_string()))?;

    let mut doc = Document::new();
    let mut page = Page::a4();
    page.add_advanced_table(&table, 50.0, 700.0)?;
    doc.add_page(page);

    let config = WriterConfig {
        compress_streams: false,
        ..WriterConfig::default()
    };
    let pdf_bytes = doc.to_bytes_with_config(config)?;
    let texts = extract_text_strings(&pdf_bytes);

    // SpanDown should be rendered once
    assert!(
        texts.contains(&"SpanDown".to_string()),
        "Should contain 'SpanDown'"
    );
    assert!(texts.contains(&"R0B".to_string()), "Should contain 'R0B'");
    assert!(texts.contains(&"R1B".to_string()), "Should contain 'R1B'");

    // R1B should be in column B (second column), not column A
    let positions = extract_text_positions(&pdf_bytes);
    let idx_r1b = texts.iter().position(|t| t == "R1B").unwrap();
    let x_r1b = positions[idx_r1b].0;

    // Column B starts at x=200 (50 + 150), R1B should be near there
    assert!(
        x_r1b > 180.0,
        "R1B (x={x_r1b}) should be in column B (x~200), not column A (x~50)"
    );

    Ok(())
}

// =============================================================================
// P6: #162 — CJK text alignment verification
// =============================================================================

#[test]
fn test_cjk_center_alignment_in_table() -> Result<()> {
    // Issue #162: CJK text should be properly centered in table cells
    use oxidize_pdf::text::TextAlign;

    let mut table = Table::new(vec![200.0]);
    table.set_position(50.0, 700.0);

    // Row with center-aligned CJK text
    table.add_row_with_alignment(vec!["测试中文".to_string()], TextAlign::Center)?;
    // Row with center-aligned Latin text (as reference)
    table.add_row_with_alignment(vec!["Test".to_string()], TextAlign::Center)?;

    let pdf_bytes = render_table_to_bytes(&table)?;
    let texts = extract_text_strings(&pdf_bytes);
    let positions = extract_text_positions(&pdf_bytes);

    // Both texts should be present
    // Note: CJK text may be hex-encoded in PDF, so check for Latin text at minimum
    assert!(
        texts.contains(&"Test".to_string()),
        "Should contain 'Test', found: {:?}",
        texts
    );

    // If CJK text is rendered as literal string, check its X position
    // Both centered texts should have X > 50 (the cell start + padding)
    // and X < 250 (cell end). The center should be around 150.
    let test_idx = texts.iter().position(|t| t == "Test").unwrap();
    let test_x = positions[test_idx].0;

    // "Test" centered in 200pt cell should be around x=50+padding+(200-text_width)/2
    // With padding=5 and text_width~20pt, centered_x ~= 50+5+(190-20)/2 = 140
    assert!(
        test_x > 80.0 && test_x < 200.0,
        "Centered 'Test' (x={test_x}) should be roughly centered in 200pt cell starting at x=50"
    );

    Ok(())
}

// =============================================================================
// Guard: extract_text_positions must not silently return empty
// =============================================================================

#[test]
fn test_extract_text_positions_is_non_empty() -> Result<()> {
    // Guard against silent test pass if PDF operator format changes.
    let mut table = Table::new(vec![200.0]);
    table.set_position(50.0, 700.0);
    table.add_row(vec!["Guard".to_string()])?;

    let pdf_bytes = render_table_to_bytes(&table)?;
    let positions = extract_text_positions(&pdf_bytes);

    assert!(
        !positions.is_empty(),
        "extract_text_positions returned empty — regex may not match the PDF operator format"
    );

    Ok(())
}

// =============================================================================
// P6: #171 — AdvancedTable auto row height for multiline content
// =============================================================================

#[test]
fn test_advanced_table_auto_height_multiline() -> Result<()> {
    // Issue #171: calculate_table_height must account for multiline cell content
    // when auto_height is enabled (default).
    // 3 lines of text at 12pt × 1.2 line_height = 43.2pt + padding > 25pt default.
    let table = AdvancedTableBuilder::new()
        .add_column("Col1", 200.0)
        .add_column("Col2", 200.0)
        .add_row_cells(vec![
            CellData::new("Line1\nLine2\nLine3"),
            CellData::new("Single"),
        ])
        .build()
        .map_err(|e| oxidize_pdf::error::PdfError::InvalidOperation(e.to_string()))?;

    let renderer = TableRenderer::new();
    let calc_height = renderer.calculate_table_height(&table);

    // Header (30pt default) + data row.
    // Data row must be > 25pt default because the cell has 3 lines.
    // 3 lines × 12pt × 1.2 = 43.2pt + padding (~12pt) = ~55pt.
    // Total should be header(30) + row(~55) = ~85, NOT header(30) + row(25) = 55.
    assert!(
        calc_height > 60.0,
        "calculate_table_height should expand for multiline content, got {calc_height} \
         (expected > 60, default without expansion would be ~55)"
    );

    Ok(())
}

#[test]
fn test_advanced_table_auto_height_renders_all_lines() -> Result<()> {
    // Issue #171: when auto_height expands the row, all text lines must
    // be rendered within the cell bounds (not clipped).
    let table = AdvancedTableBuilder::new()
        .add_column("Content", 300.0)
        .add_row_cells(vec![CellData::new("Alpha\nBravo\nCharlie")])
        .build()
        .map_err(|e| oxidize_pdf::error::PdfError::InvalidOperation(e.to_string()))?;

    let mut doc = Document::new();
    let mut page = Page::a4();
    page.add_advanced_table(&table, 50.0, 700.0)?;
    doc.add_page(page);

    let config = WriterConfig {
        compress_streams: false,
        ..WriterConfig::default()
    };
    let pdf_bytes = doc.to_bytes_with_config(config)?;
    let texts = extract_text_strings(&pdf_bytes);

    assert!(
        texts.contains(&"Alpha".to_string()),
        "should contain 'Alpha', found: {:?}",
        texts
    );
    assert!(
        texts.contains(&"Bravo".to_string()),
        "should contain 'Bravo', found: {:?}",
        texts
    );
    assert!(
        texts.contains(&"Charlie".to_string()),
        "should contain 'Charlie', found: {:?}",
        texts
    );

    // All 3 lines should be at different Y positions, descending
    let positions = extract_text_positions(&pdf_bytes);
    let idx_a = texts.iter().position(|t| t == "Alpha").unwrap();
    let idx_b = texts.iter().position(|t| t == "Bravo").unwrap();
    let idx_c = texts.iter().position(|t| t == "Charlie").unwrap();

    assert!(
        positions[idx_a].1 > positions[idx_b].1,
        "Alpha (y={}) should be above Bravo (y={})",
        positions[idx_a].1,
        positions[idx_b].1
    );
    assert!(
        positions[idx_b].1 > positions[idx_c].1,
        "Bravo (y={}) should be above Charlie (y={})",
        positions[idx_b].1,
        positions[idx_c].1
    );

    Ok(())
}

#[test]
fn test_advanced_table_height_matches_render() -> Result<()> {
    // Issue #171: calculate_table_height and render_table must agree on total height.
    // render_table returns final_y, so rendered_height = start_y - final_y.
    let table = AdvancedTableBuilder::new()
        .add_column("A", 200.0)
        .add_column("B", 200.0)
        .add_row_cells(vec![
            CellData::new("One\nTwo\nThree\nFour"),
            CellData::new("Short"),
        ])
        .add_row_cells(vec![
            CellData::new("Single"),
            CellData::new("Also\nTwo lines"),
        ])
        .build()
        .map_err(|e| oxidize_pdf::error::PdfError::InvalidOperation(e.to_string()))?;

    let renderer = TableRenderer::new();
    let predicted = renderer.calculate_table_height(&table);

    let mut doc = Document::new();
    let mut page = Page::a4();
    let start_y = 700.0;
    let final_y = page.add_advanced_table(&table, 50.0, start_y)?;
    doc.add_page(page);

    let rendered_height = start_y - final_y;

    // predicted height should match rendered height within 3pt tolerance
    // (calculate_table_height includes a 2pt border buffer that render_table
    // accounts for differently via border drawing position)
    let diff = (predicted - rendered_height).abs();
    assert!(
        diff < 3.0,
        "calculate_table_height ({predicted:.1}) and actual render height ({rendered_height:.1}) \
         disagree by {diff:.1}pt (tolerance 3pt)"
    );

    Ok(())
}