Class: Grim::Pdf

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/grim/pdf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Pdf

Raises an error if pdf not found and sets some instance variables if pdf is found.

path - A String or Path to the pdf options - A Hash of options.

:pdftotext_path - The String path of where to find the pdftotext
                  binary to use when extracting text
                  (default: "pdftotext").

Raises:



17
18
19
20
21
# File 'lib/grim/pdf.rb', line 17

def initialize(path, options = {})
  raise Grim::PdfNotFound unless File.exist?(path)
  @path = path
  @pdftotext_path = options[:pdftotext_path] || 'pdftotext'
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/grim/pdf.rb', line 6

def path
  @path
end

Instance Method Details

#[](index) ⇒ Object

Creates an instance Grim::Page for the index passed in.

index - accepts Integer for position in array

For example:

pdf[4]  # returns 5th page

Returns an instance of Grim::Page.

Raises:



49
50
51
52
# File 'lib/grim/pdf.rb', line 49

def [](index)
  raise Grim::PageNotFound unless index >= 0 && index < count
  Grim::Page.new(self, index, pdftotext_path: @pdftotext_path)
end

#countObject

Shells out to ghostscript to read the pdf with the pdf_info.ps script as a filter, returning the number of pages in the pdf as an integer.

For example:

pdf.count
# => 4

Returns an Integer.



33
34
35
36
37
# File 'lib/grim/pdf.rb', line 33

def count
  @count ||= begin
    Grim.processor.count(@path)
  end
end

#eachObject



54
55
56
57
58
# File 'lib/grim/pdf.rb', line 54

def each
  (0..(count-1)).each do |index|
    yield Grim::Page.new(self, index, pdftotext_path: @pdftotext_path)
  end
end