Module: Prawn::Fillform

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

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"0.0.20"

Instance Method Summary collapse

Instance Method Details

#acroform_field_namesObject



181
182
183
184
185
186
187
188
189
# File 'lib/prawn-fillform.rb', line 181

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

#acroform_fieldsObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/prawn-fillform.rb', line 191

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
          acroform[page_number] << Button.new(dictionary)
        end
      end
    end
  end
  acroform
end

#fill_form_with(data = {}) ⇒ Object



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
244
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
# File 'lib/prawn-fillform.rb', line 217

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)

      value = data[page][field.name].fetch(:value) rescue nil
      if value.nil?
        value = data[field.name].fetch(:value) rescue nil
      end
      options = field_hash.fetch(:options) rescue nil
      options ||= {}

      if value
        value = value.to_s
        x_offset = options[:x_offset] || self.class.fillform_x_offset
        y_offset = options[:y_offset] || self.class.fillform_y_offset

        if field.type == :text
          fill_color options[:font_color] || field.font_color

          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,

                                # Default to the document font size if the field size is 0
                                :size => options[:font_size] || ((size = field.font_size) > 0.0 ? size : font_size),
                                :style => options[:font_style] || field.font_style
        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