Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hierarchical fields #39

Open
muesliflyer opened this issue Mar 16, 2018 · 1 comment
Open

Hierarchical fields #39

muesliflyer opened this issue Mar 16, 2018 · 1 comment

Comments

@muesliflyer
Copy link

I needed to fill forms (e.g., US tax forms) with hierarchical fields (e.g., person1.address.city). This doesn't seem to be supported in fdfgen. So I modified the handle_data_strings function to handle inputs such as this:

{'person1': {
    'address': {
        'city': 'NewYork',
        'zip':  '12345'
    }
}}
def handle_data_strings(fdf_data_strings, fields_hidden, fields_readonly,
                        checkbox_checked_name):
    if isinstance(fdf_data_strings, dict):
        fdf_data_strings = fdf_data_strings.items()

    for (key, value) in fdf_data_strings:
        if value is True:
            value = b'/V' + FDFIdentifier(checkbox_checked_name).value
        elif value is False:
            value = b'/V' + FDFIdentifier('Off').value
        elif isinstance(value, FDFIdentifier):
            value = b'/V' + value.value
        elif isinstance(value, list) or isinstance(value, tuple) or isinstance(value, dict):
            value = \
                b'/Kids[\n' \
                + b''.join(handle_data_strings(value, fields_hidden, fields_readonly,
                                               checkbox_checked_name)) \
                + b']'
        else:
            value = b'/V' + b''.join([b'(', smart_encode_str(value), b')'])

        yield b''.join([
            b'<<',
            b'/T(',
            smart_encode_str(key),
            b')',
            value,
            handle_hidden(key, fields_hidden),
            b'',
            handle_readonly(key, fields_readonly),
            b'>>\n',
        ])

I tested it only briefly. So no guarantees. Feel free to integrate it into the code base.
An alternative is to use the somewhat simpler (IMHO) xfdf form data format (also supported by pdftk).

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>
<field name="person1">
	<field name="address">
		<field name="city">
			<value>NewYork</value>
		</field>
		<field name="zip">
			<value>12345</value>
		</field>
	</field>
</field>
</fields>
</xfdf>

I wrote a simple hand-coded Python function to generate that and used pdftk to fill the form. I can share the function is anyone is interested.

@muesliflyer
Copy link
Author

Here is the function that generates the xfdf format: generateXFDF.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant