class PdfForms::FdfHex

Map keys and values to Adobe's FDF format.

This is a variation of the original Fdf data format, values are encoded in UTF16 hexadesimal notation to improve compatibility with non ascii charsets.

Information about hexadesimal FDF values was found here:

stackoverflow.com/questions/6047970/weird-characters-when-filling-pdf-with-pdftk

Private Instance Methods

encode_data(fdf) click to toggle source

Fdf implementation encodes to ISO-8859-15 which we do not want here.

# File lib/pdf_forms/fdf_hex.rb, line 35
def encode_data(fdf)
  fdf
end
encode_many(values) click to toggle source
# File lib/pdf_forms/fdf_hex.rb, line 22
def encode_many(values)
  "[#{values.map { |v| encode_value_as_hex(v) }.join}]"
end
encode_value_as_hex(value) click to toggle source
# File lib/pdf_forms/fdf_hex.rb, line 26
def encode_value_as_hex(value)
  value = value.to_s
  utf_16 = value.encode('UTF-16BE', :invalid => :replace, :undef => :replace)
  hex = utf_16.unpack('H*').first
  hex.force_encoding 'ASCII-8BIT' # jruby
  '<FEFF' + hex.upcase + '>'
end
field(key, value) click to toggle source
# File lib/pdf_forms/fdf_hex.rb, line 16
def field(key, value)
  "<</T(#{key})/V" +
    (Array === value ? encode_many(value) : encode_value_as_hex(value)) +
    ">>\n"
end