Class: Tabula::ObjectExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/tabula/pdf/object_extractor.rb

Overview

Extracts content from PDF documents. Wraps pdf-reader and provides access to pages with text and rulings.

Defined Under Namespace

Classes: RulingReceiver

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, password: nil) ⇒ ObjectExtractor

Returns a new instance of ObjectExtractor.

Parameters:

  • path (String) —

    path to PDF file

  • password (String, nil) (defaults to: nil) —

    password for encrypted PDFs



31
32
33
34
35
36
# File 'lib/tabula/pdf/object_extractor.rb', line 31

def initialize(path, password: nil)
  @path = path
  @password = password
  @pdf_reader = open_pdf
  @closed = false
end

Instance Attribute Details

#pdf_reader ⇒ Object (readonly)

Returns the value of attribute pdf_reader.



9
10
11
# File 'lib/tabula/pdf/object_extractor.rb', line 9

def pdf_reader
  @pdf_reader
end

Class Method Details

.open(path, password: nil) {|ObjectExtractor| ... } ⇒ ObjectExtractor, Object

Open a PDF file for extraction

Parameters:

  • path (String) —

    path to PDF file

  • password (String, nil) (defaults to: nil) —

    password for encrypted PDFs

Yields:

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tabula/pdf/object_extractor.rb', line 16

def self.open(path, password: nil, &block)
  extractor = new(path, password: password)
  if block
    begin
      yield extractor
    ensure
      extractor.close
    end
  else
    extractor
  end
end

Instance Method Details

#close ⇒ Object

Close the PDF



74
75
76
# File 'lib/tabula/pdf/object_extractor.rb', line 74

def close
  @closed = true
end

#closed? ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/tabula/pdf/object_extractor.rb', line 78

def closed?
  @closed
end

#extract ⇒ PageIterator

Extract all pages

Returns:



50
51
52
# File 'lib/tabula/pdf/object_extractor.rb', line 50

def extract
  extract_pages(1..page_count)
end

#extract_page(page_number) ⇒ Page

Extract a specific page

Parameters:

  • page_number (Integer) —

    page number (1-indexed)

Returns:

  • (Page) —

    extracted page



41
42
43
44
45
46
# File 'lib/tabula/pdf/object_extractor.rb', line 41

def extract_page(page_number)
  validate_page_number(page_number)

  pdf_page = @pdf_reader.pages[page_number - 1]
  process_page(pdf_page, page_number)
end

#extract_pages(pages) ⇒ PageIterator

Extract specific pages

Parameters:

  • pages (Range, Array<Integer>) —

    page numbers to extract

Returns:



57
58
59
# File 'lib/tabula/pdf/object_extractor.rb', line 57

def extract_pages(pages)
  PageIterator.new(self, pages.to_a)
end

#page_count ⇒ Integer

Get page count

Returns:

  • (Integer)


63
64
65
# File 'lib/tabula/pdf/object_extractor.rb', line 63

def page_count
  @pdf_reader.page_count
end

#pages ⇒ Enumerator

Get pages iterator

Returns:

  • (Enumerator)


69
70
71
# File 'lib/tabula/pdf/object_extractor.rb', line 69

def pages
  (1..page_count).lazy.map { |n| extract_page(n) }
end