Class: GBDev::PDF::Page

Inherits:
Object
  • Object
show all
Includes:
Utils::PrivateMethods
Defined in:
lib/pdf_filler/page.rb

Overview

A page that represents a PDF page.

Constant Summary collapse

CHECK_BOX =
'Check Box'
COMBO_BOX =
'Combo Box'
LIST =
'List'
PUSH_BUTTON =
'Push Button'
RADIO_BUTTON =
'Radio Button'
SIGNATURE =
'Signature'
TEXT_FIELD =
'Text Field'
NONE =
'None'
UNKNOWN =
'?'

Instance Method Summary collapse

Methods included from Utils::PrivateMethods

#build_random_file_name, #build_random_string

Constructor Details

#initialize(template) ⇒ Page

String template : A path to the template file



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pdf_filler/page.rb', line 19

def initialize(template)
  @template = template
  
  @pdf_fields = {}
  
  @check_boxes = {}
  @images = {}
  @radio_buttons = {}
  @signature_fields = {}
  @text_fields = {}
  
  @pdf_reader = nil
end

Instance Method Details

#get_field_states(field_name) ⇒ Object

Returns an array of field states. For example if the field type is a list an array with all possible choices in the list will be returned.

  • field_name - The field name to get all the possible states for.



74
75
76
77
78
# File 'lib/pdf_filler/page.rb', line 74

def get_field_states(field_name)
  reader = PdfReader.new(@template)
  form = reader.getAcroFields()
  form.getAppearanceStates(field_name.to_s);
end

#get_pdf_fieldsObject

Returns a hash where the keys are the field names in the template PDF and the values are the field types.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pdf_filler/page.rb', line 34

def get_pdf_fields
  return @pdf_fields unless @pdf_fields.empty?
  
  reader = PdfReader.new(@template)
  form = reader.getAcroFields()
  fields = form.getFields()
  i = fields.keySet().iterator()
  
  while(i.hasNext())
    key = i.next()

    case(form.getFieldType(key))
      when AcroFields.FIELD_TYPE_CHECKBOX
        @pdf_fields[key.to_string().to_sym] = GBDev::PDF::Page::CHECK_BOX
      when AcroFields.FIELD_TYPE_COMBO
        @pdf_fields[key.to_string().to_sym] = GBDev::PDF::Page::COMBO_BOX
      when AcroFields.FIELD_TYPE_LIST 
        @pdf_fields[key.to_string().to_sym] = GBDev::PDF::Page::LIST
      when AcroFields.FIELD_TYPE_NONE 
        @pdf_fields[key.to_string().to_sym] = GBDev::PDF::Page::NONE
      when AcroFields.FIELD_TYPE_PUSHBUTTON
        @pdf_fields[key.to_string().to_sym] = GBDev::PDF::Page::PUSH_BUTTON
      when AcroFields.FIELD_TYPE_RADIOBUTTON
        @pdf_fields[key.to_string().to_sym] = GBDev::PDF::Page::RADIO_BUTTON
      when AcroFields.FIELD_TYPE_SIGNATURE
        @pdf_fields[key.to_string().to_sym] = GBDev::PDF::Page::SIGNATURE
      when AcroFields.FIELD_TYPE_TEXT
        @pdf_fields[key.to_string().to_sym] = GBDev::PDF::Page::TEXT_FIELD
      else 
        @pdf_fields[key.to_string().to_sym] = GBDev::PDF::Page::UNKNOWN
    end
  end
  
  return @pdf_fields
end

#map_to_object(object) ⇒ Object

Maps PDF fields to an ActiveRecord object. The ActiveRecord object must use the acts_as_pdf_db_mapper module.

  • object - An ActiveRecord object that uses the acts_as_pdf_db_mapper module.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/pdf_filler/page.rb', line 147

def map_to_object(object)
  if object.methods.include?('mapped_fields')
    fields = self.get_pdf_fields
    object.mapped_fields.each do |mapped_field|
      
      if mapped_field.class.to_s == 'Hash'
        key_value_pair = mapped_field.first.to_a.flatten
        key = key_value_pair[0]
        value = key_value_pair[1]
      else
        key = mapped_field
        value = mapped_field
      end

      case(fields[key])
        when GBDev::PDF::Page::CHECK_BOX
          self.set_checkbox(key, object.send(value))
        when GBDev::PDF::Page::COMBO_BOX
          #self.set_combobox(key, object.send(value))
        when GBDev::PDF::Page::LIST
          #self.set_list(key, object.send(value))
        when GBDev::PDF::Page::NONE
          #self.set_none(key, object.send(value))
        when GBDev::PDF::Page::PUSH_BUTTON
          #self.set_push_button(key, object.send(value))
        when GBDev::PDF::Page::RADIO_BUTTON
          self.set_radio_button(key, object.send(value))
        when GBDev::PDF::Page::SIGNATURE
          self.set_signature_field(key, object.send(value))
        when GBDev::PDF::Page::TEXT_FIELD
          self.set_text_field(key, object.send(value))
      end            
      
    end
  end
end

#save_to(filename) ⇒ Object

Renders the template with the given data and saves to the given filename path.

  • filename - A path to the file to be created.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/pdf_filler/page.rb', line 188

def save_to(filename)                      
  field_cache = HashMap.new
  reader = PdfReader.new(@template)
  stamper = PdfStamper.new( reader, FileOutputStream.new(filename) )
  form = stamper.getAcroFields()
  form.setFieldCache(field_cache)
  
  all_fields = {}
  all_fields.merge!(@check_boxes)
  all_fields.merge!(@radio_buttons)
  all_fields.merge!(@signature_fields)
  all_fields.merge!(@text_fields)

  all_fields.each do |field, value|
    form.setField(field.to_s, value.to_s)
  end        

  # TODO: do something with @images 

  stamper.setFormFlattening(true)
  stamper.close                    
end

#set_checkbox(key, value) ⇒ Object Also known as: checkbox

Sets a know checkbox in the PDF template.

  • key - A known checkbox in the PDF.

  • value - The checkbox to update.



85
86
87
# File 'lib/pdf_filler/page.rb', line 85

def set_checkbox(key, value)
  @check_boxes[key] = value
end

#set_image(key, value) ⇒ Object Also known as: image

NOT WORING YET Sets a known image area in the PDF template.

  • key - A known image in the PDF.

  • value - The image to apply to the know image area.



97
98
99
100
# File 'lib/pdf_filler/page.rb', line 97

def set_image(key, value)
  raise 'Image not working yet'
  @images[key] = value
end

#set_radio_button(key, value) ⇒ Object Also known as: radio_button

Sets a known radio button in the PDF template.

  • key - A known radio button group in the PDF.

  • value - The radio button group to update.



110
111
112
# File 'lib/pdf_filler/page.rb', line 110

def set_radio_button(key, value)
  @radio_buttons[key] = value
end

#set_signature_field(key, value) ⇒ Object Also known as: signature_field

NOT WORKING YET Sets a known signature field in the PDF template.

  • key - A known signature field in the PDF.

  • value - The value to apply to the know field.



123
124
125
126
# File 'lib/pdf_filler/page.rb', line 123

def set_signature_field(key, value)
  raise 'Signature field not working yet'
  @signature_fields[key] = value
end

#set_text_field(key, value) ⇒ Object Also known as: text_field

Sets a known text field in the PDF template.

  • key - A known text field in the PDF.

  • value - The value to apply to the know field.



136
137
138
# File 'lib/pdf_filler/page.rb', line 136

def set_text_field(key, value)
  @text_fields[key] = value
end

#to_bufferObject

Renders the template with the given data and returns that buffer.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/pdf_filler/page.rb', line 213

def to_buffer
  field_cache = HashMap.new
  reader = PdfReader.new(@template)
  baos = ByteArrayOutputStream.new 
  stamper = PdfStamper.new( reader, baos )
  form = stamper.getAcroFields()
  form.setFieldCache(field_cache)
  
  all_fields = {}
  all_fields.merge!(@check_boxes)
  all_fields.merge!(@radio_buttons)
  all_fields.merge!(@signature_fields)
  all_fields.merge!(@text_fields)

  all_fields.each do |field, value|
    form.setField(field.to_s, value.to_s)
  end        

  # TODO: do something with @images 

  stamper.setFormFlattening(true)
  stamper.close
  
  return baos.toByteArray()
end