Class: Iguvium::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/iguvium/table.rb

Overview

Represents single table from the [Iguvium::Page]:

  • table outer borders aka box,

  • set of detected horizontal and vertical lines to form table’s grid,

  • set of characters with its coordinates to fill the grid.

Additional functionality like an option to detect an open table grid at the end or at the beginning of the page will be added later

To render table into 2D text array, call #to_a

Instance Method Summary collapse

Constructor Details

#initialize(box, page) ⇒ Table

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Table.



15
16
17
18
19
20
21
# File 'lib/iguvium/table.rb', line 15

def initialize(box, page)
  @box = box
  @lines = page.lines
  @page = page
  grid
  heal
end

Instance Method Details

#to_a(newlines: false, phrases: true) ⇒ Array

Renders the table into an array of strings.

Newlines in PDF have usually no semantic value, and are replaced with spaces by default. Sometimes you may need to keep them; in this case use ‘newlines: true` option.

Parameters:

  • newlines (Boolean) (defaults to: false)

    keep newlines inside table cells, false by default

  • phrases (Boolean) (defaults to: true)

    keep phrases unsplit, true by default. Poor man’s merged cells workaround. Could break some tables , could fix some.

Returns:

  • (Array)

    2D array of strings (content of table’s cells)



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/iguvium/table.rb', line 34

def to_a(newlines: false, phrases: true)
  @to_a ||=
    grid[:rows]
    .reverse
    .map { |row|
      grid[:columns].map do |column|
        render(
          phrases ? words_inside(column, row) : chars_inside(column, row),
          newlines: newlines
        )
      end
    }
end