gettalong / hexapdf

Versatile PDF creation and manipulation for Ruby
https://hexapdf.gettalong.org
Other
1.21k stars 69 forks source link

BUG: each_field not listing all fields correctly #318

Closed loust333 closed 4 weeks ago

loust333 commented 1 month ago

Hello,

Just wanted to notify about a bug (I think) I was testing the salary certificate PDF, which is a weird PDF from the state. 160f-2024.pdf

The keys of each field starts with a Letter and a dot for example: "N.24"

Instead of manually entering each field name, I use the following method to create a map of all the fields:

def generate_hash_with_fields_and_dummy_values
    field_mapping = {}

    @pdf_origin.acro_form.each_field do |field|
      # puts field.inspect, field.class
      if field.class == HexaPDF::Type::AcroForm::ButtonField
        field_mapping[field[:T]] = true
      else
        field_mapping[field[:T]] = "'text'"
      end
    end

    File.open('field_mapping.txt', 'w') do |file|
      file.puts("{")
      field_mapping.each do |key, value|
        file.puts("'#{key}' => #{value},")
      end
      file.puts("}")
    end
  end

Amazing right ! 😁

In this use case, I still need to manually correct the field names. Because the key value is not "N.24" but it will be "24".

In my use case, it is not really a problem. We can still fill the field "N.24" and field "A.24" but to get the value. I'm not sure if it works correctly or if it is just the each_field that fails ? 🤔

gettalong commented 1 month ago

The name of an AcroForm field can be hierarchical and the separator used is... the dot :) So don't use field[:T] for accessing the name but use the convenience methods provided by HexaPDF, i.e. field.full_field_name.

loust333 commented 1 month ago

@gettalong thanks for the feedback. It's the first time I see the method. That will help a lot :)

As a suggestion, maybe the method should be included in the examples.

gettalong commented 4 weeks ago

I have added some general informational text and links to the AcroForm example that should one point into the right direction.