Sha256: c4a8f72b5943f084500cb228e92008eb6d758a0f94b799aefd35a4289f9c107e

Contents?: true

Size: 1.04 KB

Versions: 53

Compression:

Stored size: 1.04 KB

Contents

require 'pdf-reader'
require 'ndr_support/safe_file'
require_relative 'registry'

module NdrImport
  # This is one of a collection of file handlers that deal with individual formats of data.
  # They can be instantiated directly or via the factory method Registry.tables
  module File
    # This class is a PDF file handler that returns a single table.
    class Pdf < Base
      private

      def rows(&block)
        return enum_for(:rows) unless block

        reader = PDF::Reader.new(SafeFile.safepath_to_string(@filename))

        reader.pages.each do |page|
          process_page(page, &block)
        end

      rescue NoMethodError
        raise "Failed to read #{SafeFile.basename(@filename)} as a PDF"
      end

      def process_page(page, &block)
        page.text.split("\n").each do |line|
          block.call(line)
        end
      rescue => e
        raise "Invalid format on page #{page.number} of #{SafeFile.basename(@filename)} " \
              "[#{e.class}: #{e.message}]"
      end
    end

    Registry.register(Pdf, 'pdf')
  end
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
ndr_import-4.1.1 lib/ndr_import/file/pdf.rb
ndr_import-4.1.0 lib/ndr_import/file/pdf.rb
ndr_import-4.0.1 lib/ndr_import/file/pdf.rb
ndr_import-4.0.0 lib/ndr_import/file/pdf.rb
ndr_import-3.1.6 lib/ndr_import/file/pdf.rb
ndr_import-3.1.5 lib/ndr_import/file/pdf.rb
ndr_import-3.1.4 lib/ndr_import/file/pdf.rb
ndr_import-3.1.3 lib/ndr_import/file/pdf.rb
ndr_import-3.1.2 lib/ndr_import/file/pdf.rb
ndr_import-3.1.1 lib/ndr_import/file/pdf.rb
ndr_import-3.1.0 lib/ndr_import/file/pdf.rb
ndr_import-3.0.1 lib/ndr_import/file/pdf.rb
ndr_import-3.0.0 lib/ndr_import/file/pdf.rb