Module: Ruby::Fillform

Defined in:
lib/ruby-fillform.rb,
lib/ruby-fillform/version.rb

Defined Under Namespace

Modules: XYOffsets Classes: Button, Checkbox, Field, References, Text

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#acroform_field_namesObject



207
208
209
210
211
212
213
214
215
# File 'lib/ruby-fillform.rb', line 207

def acroform_field_names
  result = []
  acroform_fields.each do |page, fields|
    fields.each do |field|
      result << field.name
    end
  end
  result
end

#acroform_fieldsObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/ruby-fillform.rb', line 217

def acroform_fields
  acroform = {}
  state.pages.each_with_index do |page, i|
    annots = deref(page.dictionary.data[:Annots])
    page_number = "page_#{i+1}".to_sym
    acroform[page_number] = []
    if annots
      annots.map do |ref|
        dictionary = deref(ref)
        next unless deref(dictionary[:Type]) == :Annot and deref(dictionary[:Subtype]) == :Widget
        next unless (deref(dictionary[:FT]) == :Tx || deref(dictionary[:FT]) == :Btn)
        type = deref(dictionary[:FT]).to_sym
        case type
        when :Tx
          acroform[page_number] << Text.new(dictionary)
        when :Btn
          if deref(dictionary[:AP]).has_key?(:D) || deref(dictionary[:AP][:N]).has_key?(:Off)
            acroform[page_number] << Checkbox.new(dictionary)
          else
            acroform[page_number] << Button.new(dictionary)
          end
        end
      end
    end
  end
  acroform
end

#fill_form_with(data = {}) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/ruby-fillform.rb', line 245

def fill_form_with(data={})
  acroform_fields.each do |page, fields|
    fields.each do |field|
      number = page.to_s.split("_").last.to_i
      go_to_page(number)

      field_name_page = data[page][field.name]
      value = field_name_page.fetch(:value) rescue nil
      options = field_name_page.fetch(:options) rescue nil
      if value.nil?
        value = field_name_page.fetch(:value).to_s rescue nil
        options = field_name_page.fetch(:options) rescue nil
      end

      options ||= {}

      if value
        value = value.to_s
        x_offset = options[:x_offset] || 34
        y_offset = options[:y_offset] || 38
        if field.type == :text
          fill_color options[:font_color] || field.font_color

   font options[:font_face]
          text_box value, :at => [field.x - x_offset, field.y - y_offset],
                                :align => options[:align] || field.align,
                                :width => options[:width] || field.width,
                                :height => options[:height] || field.height,
                                :valign => options[:valign] || :center,
                                :size => options[:font_size] || ((size = field.font_size) > 0.0 ? size : font_size),
                                :style => options[:font_style] || field.font_style
        elsif field.type == :checkbox
          is_yes = (v = value.downcase) == "yes" || v == "1" || v == "true"
          formatted_text_box [{
              text: is_yes ? Checkbox::YES : Checkbox::NO,
              font: "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf",
              size: options[:font_size] || field.font_size,
              styles: options[:font_style] || [field.font_style]
            }],
            :at => [field.x - x_offset, field.y - y_offset],
            :width => options[:width] || field.width,
            :height => options[:height] || field.height
        elsif field.type == :button
          bounding_box([field.x + x_offset, field.y + y_offset], :width => field.width, :height => field.height) do
            if value =~ /http/
              image open(value), :position => options[:position] || :center,
                              :vposition => options[:vposition] || :center,
                              :fit => options[:fit] || [field.width, field.height]
            else
              image value, :position => options[:position] || :center,
                              :vposition => options[:vposition] || :center,
                              :fit => options[:fit] || [field.width, field.height]
            end
          end
        end
      end
    end
  end

  references = References.new(state)
  references.delete!

end