Sha256: 560339177ff82050da56b64f5948b629ff400bb28e61a1690d6322c4b1766ea8

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# encoding: UTF-8
module Grim
  class Pdf
    include Enumerable

    attr_reader :path

    # 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").
    #
    def initialize(path, options = {})
      raise Grim::PdfNotFound unless File.exist?(path)
      @path = path
      @pdftotext_path = options[:pdftotext_path] || 'pdftotext'
    end

    # 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.
    #
    def count
      @count ||= begin
        Grim.processor.count(@path)
      end
    end

    # 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.
    #
    def [](index)
      raise Grim::PageNotFound unless index >= 0 && index < count
      Grim::Page.new(self, index, pdftotext_path: @pdftotext_path)
    end

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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grim-1.3.5 lib/grim/pdf.rb