class FillablePDF

Constants

BYTE_STREAM

required Java imports

PDF_ACRO_FORM
PDF_DOCUMENT
PDF_FORM_FIELD
PDF_READER
PDF_WRITER
VERSION

Public Class Methods

new(file_path) click to toggle source

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
# File lib/fillable-pdf.rb, line 20
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 => e
    raise "#{e.message} (input file may be corrupt, incompatible, or may not have any forms)"
  end
end

Public Instance Methods

any_fields?() click to toggle source

Determines whether the form has any fields.

@return true if form has fields, false otherwise
# File lib/fillable-pdf.rb, line 40
def any_fields?
  num_fields.positive?
end
close() click to toggle source

Closes the PDF document discarding all unsaved changes.

@return [Boolean] true if document is closed, false otherwise

# File lib/fillable-pdf.rb, line 184
def close
  @pdf_doc.close
  @pdf_doc.isClosed
end
field(key) click to toggle source

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

@param [String|Symbol] key the field name

@return the value of the field
# File lib/fillable-pdf.rb, line 60
def field(key)
  pdf_field(key).getValueAsString
rescue NoMethodError
  raise "unknown key name `#{key}'"
end
field_type(key) click to toggle source

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
# File lib/fillable-pdf.rb, line 73
def field_type(key)
  pdf_field(key).getFormType.toString
end
fields() click to toggle source

Retrieves a hash of all fields and their values.

@return the hash of field keys and values
# File lib/fillable-pdf.rb, line 82
def fields
  iterator = @form_fields.keySet.iterator
  map = {}
  while iterator.hasNext
    key = iterator.next.toString
    map[key.to_sym] = field(key)
  end
  map
end
names() click to toggle source

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

@return array of field names
# File lib/fillable-pdf.rb, line 135
def names
  iterator = @form_fields.keySet.iterator
  set = []
  set << iterator.next.toString.to_sym while iterator.hasNext
  set
end
num_fields() click to toggle source

Returns the total number of fillable form fields.

@return the number of fields
# File lib/fillable-pdf.rb, line 49
def num_fields
  @form_fields.size
end
remove_field(key) click to toggle source

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

@param [String|Symbol] key the field name
# File lib/fillable-pdf.rb, line 126
def remove_field(key)
  @pdf_form.removeField(key.to_s)
end
rename_field(old_key, new_key) click to toggle source

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
# File lib/fillable-pdf.rb, line 117
def rename_field(old_key, new_key)
  pdf_field(old_key).setFieldName(new_key.to_s)
end
save(flatten: false) click to toggle source

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

@param [bool] flatten true if PDF should be flattened, false otherwise
# File lib/fillable-pdf.rb, line 159
def save(flatten: false)
  tmp_file = SecureRandom.uuid
  save_as(tmp_file, flatten: flatten)
  FileUtils.mv tmp_file, @file_path
end
save_as(file_path, flatten: false) click to toggle source

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
# File lib/fillable-pdf.rb, line 171
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) click to toggle source

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
# File lib/fillable-pdf.rb, line 98
def set_field(key, value)
  pdf_field(key).setValue(value.to_s)
end
set_fields(fields) click to toggle source

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
# File lib/fillable-pdf.rb, line 107
def set_fields(fields)
  fields.each { |key, value| set_field key, value }
end
values() click to toggle source

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

@return array of field values
# File lib/fillable-pdf.rb, line 147
def values
  iterator = @form_fields.keySet.iterator
  set = []
  set << field(iterator.next.toString) while iterator.hasNext
  set
end

Private Instance Methods

finalize(flatten: false) click to toggle source

Writes the contents of the modified fields to the previously opened PDF file.

@param [Hash] flatten: true if PDF should be flattened, false otherwise
# File lib/fillable-pdf.rb, line 196
def finalize(flatten: false)
  @pdf_form.flattenFields if flatten
  close
  @byte_stream.toByteArray
end
pdf_field(key) click to toggle source
# File lib/fillable-pdf.rb, line 202
def pdf_field(key)
  field = @form_fields.get(key.to_s)
  raise "unknown key name `#{key}'" if field.nil?
  field
end