Class: Pdf::Components::Table

Inherits:
Pdf::Component show all
Defined in:
lib/pdf/components/table.rb

Instance Attribute Summary

Attributes inherited from Pdf::Component

#pdf

Instance Method Summary collapse

Methods inherited from Pdf::Component

#initialize

Constructor Details

This class inherits a constructor from Pdf::Component

Instance Method Details

#render(headers:, rows:, widths: nil, **_options) ⇒ Object



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
# File 'lib/pdf/components/table.rb', line 6

def render(headers:, rows:, widths: nil, **_options)
  return if rows.empty? && headers.empty?

  data = [headers] + rows

  opts = {
    header: true,
    width: bounds.width,
    cell_style: { borders: [:top, :bottom, :left, :right], padding: [4, 6], size: 8 }
  }

  # Only set column_widths if explicit numeric values (not percentages)
  # For percentages, let prawn auto-distribute based on content
  if widths && !widths.any? { |w| w.is_a?(String) && w.end_with?("%") }
    opts[:column_widths] = calculate_widths(widths)
  end

  @pdf.table(data, **opts) do
    row(0).background_color = "333333"
    row(0).text_color = "FFFFFF"
    row(0).font_style = :bold

    (1..row_length - 1).each do |i|
      row(i).background_color = i.odd? ? "F5F5F5" : "FFFFFF"
    end
  end

  move_down 15
end