Sha256: 1d5bd96079cc2f362d7c412efb5872c1590a00cf2ac12b7408feceb544e87777

Contents?: true

Size: 852 Bytes

Versions: 23

Compression:

Stored size: 852 Bytes

Contents

require 'pdf-reader'

module NdrImport
  # PDF AcroForm reader using the pdf-reader gem
  class AcroFormReader < ::PDF::Reader
    def fields_hash
      fields = {}
      fields_from(acroform[:Fields]).each do |field|
        field_name = field[:T]
        unless field[:Subtype] == :Widget || field.key?(:Kids)
          raise "Widgets or Radio boxes expected, found a #{field[:Subtype].inspect}"
        end
        raise "Non-unique column name #{field_name}" if fields.key?(field_name)
        fields[field_name] = field[:V]
      end
      fields
    end

    private

    def acroform
      @objects.deref(root[:AcroForm])
    end

    def fields_from(refs)
      Array(refs).flat_map do |ref|
        value = @objects[ref]
        # PDF has its own Hash class
        value.is_a?(::Hash) ? value : fields_from(value)
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
ndr_import-8.5.1 lib/ndr_import/acroform_reader.rb
ndr_import-8.5.0 lib/ndr_import/acroform_reader.rb
ndr_import-8.4.0 lib/ndr_import/acroform_reader.rb