benoitkugler / pdf

Golang high level PDF model
MIT License
22 stars 3 forks source link

Error on field name with a special character #8

Closed buhaytza2005 closed 7 months ago

buhaytza2005 commented 7 months ago

When attempting to fill in the attached file, everything works fine until it attempts to fill the 3.2 Companies' House Reg No field. I assume this is due to the ' in the field name but I can't figure out how to escape it. Is this something that is caused by the library or go?

Any advice would be welcome...

Error: unexpected value type for text field: formfill.FDFName
exit status 1

g02.pdf

benoitkugler commented 7 months ago

Hi ! Thank you for this report. I think the issue here is a type mismatch : a text field expects a FDFText value, not a FDFName.

I've added an example in 12af653 (see formfill/fdf_test.go); basically the code should be :

err = FillForm(&doc, FDFDict{Fields: []FDFField{
{
    T: "3", Kids: []FDFField{
        {
            T:      "2 Companies' House Reg No",
            Values: Values{V: FDFText("A sample number")},
        },
    }},
}}, true)

Could you confirm switching the type fixes the issue ?

buhaytza2005 commented 7 months ago

I have this helper function to create the fields:

func CreateFields(name string, value string) formfill.FDFField {
    if strings.Contains(name, "Yes") {
        fmt.Println("Creating field ", name, " with value ", value)
        return formfill.FDFField{T: name, Values: formfill.Values{V: formfill.FDFName(value)}}
    } else if strings.Contains(name, "No") {
        fmt.Println("Creating field ", name, " with value ", value)
        return formfill.FDFField{T: name, Values: formfill.Values{V: formfill.FDFName(value)}}
    } else if strings.Contains(name, "Tick") {
        fmt.Println("Creating field ", name, " with value ", value)
        return formfill.FDFField{T: name, Values: formfill.Values{V: formfill.FDFName(value)}}
    } else {
        fmt.Println("Creating field ", name, " with value ", value)
        return formfill.FDFField{T: name, Values: formfill.Values{V: formfill.FDFText(value)}}
    }
}

and one to the effect of going through a struct bringing the info across:

func MapData2ToFields(appData SecondApplicationData) []formfill.FDFField {                                                                                                                                                   
    var formFields []formfill.FDFField
    if appData.Retailer_name != "" {
        formFields = append(formFields, CreateFields("1 Retailer name", appData.Retailer_name))
    }

[...]
}

The fields were extracted using a different library fields.json

So the company number field was being set as:

if appData.Company_number != "" {
    formFields = append(formFields, CreateFields("3.2 Companies' House Reg No", appData.Company_number))
 }

I have tried to alter the code to:

func CreateFields(name string, value string) formfill.FDFField {
    if strings.Contains(name, "Yes") || strings.Contains(name, "No") || strings.Contains(name, "Tick") {
        // For checkboxes or similar fields where 'Yes', 'No', or 'Tick' are valid values
        fmt.Println("Creating checkbox/radio field ", name, " with value ", value)
        return formfill.FDFField{T: name, Values: formfill.Values{V: formfill.FDFName(value)}}
    } else if strings.Contains(name, "Company number") {
        // For the 'company number' field, which is a text field
        fmt.Println("Creating text field ", name, " with value ", value)
        return formfill.FDFField{
            T: "3",
            Kids: []formfill.FDFField{
                {
                    T: name,
                    Values: formfill.Values{V: formfill.FDFText(value)}, // Use FDFText for text fields
                },
            },
        }
    } else {
        // For other general text fields
        fmt.Println("Creating text field ", name, " with value ", value)
        return formfill.FDFField{T: name, Values: formfill.Values{V: formfill.FDFText(value)}}
    }
}

Still no joy. I am attaching my code below - sorry, it's txt as I cannot seem to be able to upload .go files - in case i am making some other silly mistake somewhere else. All the fields are being updated as intended, just the company number is proving to be a bit temperamental. my_code.txt

benoitkugler commented 7 months ago

If think the "No" at the end of "2 Companies' House Reg No" triggers the wrong case in your CreateFields "switch".

By the way, you may want to use doc.Catalog.Acroform and its Flatten method to get the actual field types (by type switching on the FT field of FormField )

buhaytza2005 commented 7 months ago

Thank you for your response. It was indeed the match on the "No" causing the issue.

I will look into the suggested implementation of matching on type in the future but I need to deliver a PoC this week so for now it will have to suffice.

Thanks again for the quick replies and the work done on this!

benoitkugler commented 7 months ago

Closing for now, feel free to re open if needed !