Class: FillablePDF

Inherits:
Object
  • Object
show all
Defined in:
lib/fillable-pdf.rb,
lib/fillable-pdf/version.rb

Constant Summary collapse

BYTE_STREAM =

required Java imports

Rjb.import 'com.itextpdf.io.source.ByteArrayOutputStream'
PDF_READER =
Rjb.import 'com.itextpdf.kernel.pdf.PdfReader'
PDF_WRITER =
Rjb.import 'com.itextpdf.kernel.pdf.PdfWriter'
PDF_DOCUMENT =
Rjb.import 'com.itextpdf.kernel.pdf.PdfDocument'
PDF_ACRO_FORM =
Rjb.import 'com.itextpdf.forms.PdfAcroForm'
PDF_FORM_FIELD =
Rjb.import 'com.itextpdf.forms.fields.PdfFormField'
VERSION =
'0.8.0'

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ FillablePDF

Opens a given fillable-pdf PDF file and prepares it for modification.

@param [String|Symbol] file_path the name of the PDF file or file path

Raises:

  • (IOError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fillable-pdf.rb', line 19

def initialize(file_path)
  raise IOError, "File at `#{file_path}' is not found" unless File.exist?(file_path)
  @file_path = file_path
  begin
    @byte_stream = BYTE_STREAM.new
    @pdf_reader = PDF_READER.new @file_path
    @pdf_writer = PDF_WRITER.new @byte_stream
    @pdf_doc = PDF_DOCUMENT.new @pdf_reader, @pdf_writer
    @pdf_form = PDF_ACRO_FORM.getAcroForm(@pdf_doc, true)
    @form_fields = @pdf_form.getFormFields
  rescue StandardError => ex
    raise "#{ex.message} (input file may be corrupt, incompatible, or may not have any forms)"
  end
end

Instance Method Details

#any_fields?Boolean

Determines whether the form has any fields.

@return true if form has fields, false otherwise

Returns:

  • (Boolean)


39
40
41
# File 'lib/fillable-pdf.rb', line 39

def any_fields?
  num_fields.positive?
end

#closeBoolean

Closes the PDF document discarding all unsaved changes.

Returns:

  • (Boolean)

    true if document is closed, false otherwise



183
184
185
186
# File 'lib/fillable-pdf.rb', line 183

def close
  @pdf_doc.close
  @pdf_doc.isClosed
end

#field(key) ⇒ Object

Retrieves the value of a field given its unique field name.

@param [String|Symbol] key the field name

@return the value of the field


59
60
61
62
63
# File 'lib/fillable-pdf.rb', line 59

def field(key)
  pdf_field(key).getValueAsString
rescue NoMethodError
  raise "unknown key name `#{key}'"
end

#field_type(key) ⇒ Object

Retrieves the numeric type of a field given its unique field name.

@param [String|Symbol] key the field name

@return the type of the field


72
73
74
# File 'lib/fillable-pdf.rb', line 72

def field_type(key)
  pdf_field(key).getFormType.toString
end

#fieldsObject

Retrieves a hash of all fields and their values.

@return the hash of field keys and values


81
82
83
84
85
86
87
88
89
# File 'lib/fillable-pdf.rb', line 81

def fields
  iterator = @form_fields.keySet.iterator
  map = {}
  while iterator.hasNext
    key = iterator.next.toString
    map[key.to_sym] = field(key)
  end
  map
end

#namesObject

Returns a list of all field keys used in the document.

@return array of field names


134
135
136
137
138
139
# File 'lib/fillable-pdf.rb', line 134

def names
  iterator = @form_fields.keySet.iterator
  set = []
  set << iterator.next.toString.to_sym while iterator.hasNext
  set
end

#num_fieldsObject

Returns the total number of fillable form fields.

@return the number of fields


48
49
50
# File 'lib/fillable-pdf.rb', line 48

def num_fields
  @form_fields.size
end

#remove_field(key) ⇒ Object

Removes a field from the document given its unique field name.

@param [String|Symbol] key the field name


125
126
127
# File 'lib/fillable-pdf.rb', line 125

def remove_field(key)
  @pdf_form.removeField(key.to_s)
end

#rename_field(old_key, new_key) ⇒ Object

Renames a field given its unique field name and the new field name.

@param [String|Symbol] old_key the field name
@param [String|Symbol] new_key the field name


116
117
118
# File 'lib/fillable-pdf.rb', line 116

def rename_field(old_key, new_key)
  pdf_field(old_key).setFieldName(new_key.to_s)
end

#save(flatten: false) ⇒ Object

Overwrites the previously opened PDF document and flattens it if requested.

@param [bool] flatten true if PDF should be flattened, false otherwise


158
159
160
161
162
# File 'lib/fillable-pdf.rb', line 158

def save(flatten: false)
  tmp_file = SecureRandom.uuid
  save_as(tmp_file, flatten: flatten)
  File.rename tmp_file, @file_path
end

#save_as(file_path, flatten: false) ⇒ Object

Saves the filled out PDF document in a given path and flattens it if requested.

@param [String] file_path the name of the PDF file or file path
@param [Hash] flatten: true if PDF should be flattened, false otherwise


170
171
172
173
174
175
176
# File 'lib/fillable-pdf.rb', line 170

def save_as(file_path, flatten: false)
  if @file_path == file_path
    save(flatten: flatten)
  else
    File.open(file_path, 'wb') { |f| f.write(finalize(flatten: flatten)) && f.close }
  end
end

#set_field(key, value) ⇒ Object

Sets the value of a field given its unique field name and value.

@param [String|Symbol] key the field name
@param [String|Symbol] value the field value


97
98
99
# File 'lib/fillable-pdf.rb', line 97

def set_field(key, value)
  pdf_field(key).setValue(value.to_s)
end

#set_fields(fields) ⇒ Object

Sets the values of multiple fields given a set of unique field names and values.

@param [Hash] fields the set of field names and values


106
107
108
# File 'lib/fillable-pdf.rb', line 106

def set_fields(fields)
  fields.each { |key, value| set_field key, value }
end

#valuesObject

Returns a list of all field values used in the document.

@return array of field values


146
147
148
149
150
151
# File 'lib/fillable-pdf.rb', line 146

def values
  iterator = @form_fields.keySet.iterator
  set = []
  set << field(iterator.next.toString) while iterator.hasNext
  set
end