Search code examples
pythonpdfpdftkxfdf

PDF template not merging data properly with pdftk


I'm editing a PDF template with using pdftk

        command = ("pdftk " + '"' +
               template + '"' +
               " fill_form " + '"' +
               pathUser + user['mail'] + ".xfdf" + '"' +
               " output " + '"' +
               pathUser + user['mail'] + ".pdf" + '"' +
               " need_appearances")
        command = command.replace('/', '\\')
        os.system(command)

First I'm writing my data in a .xfdf file

    for key, value in user.items():
        print(key, value)
        fields.append(u"""<field name="%s"><value>%s</value></field>""" % (key, value))
    tpl = u"""<?xml version="1.0" encoding="UTF-8"?>
            <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
                <fields>
                    %s
                </fields>
            </xfdf>""" % "\n".join(fields)


    f = open(pathUser + user['mail'] + '.xfdf', 'wb')
    f.write(tpl.encode("utf-8"))
    f.close()

I fetch the template and as shown above, write the data from the xfdf to pdf but for some reason, only the ime gets written.

Templates get fetched using some basic conditional logic as shown below:

        for item in user['predavanja']:
            user[acthead + str(actn)] = item
            actn += 1
        for item in user['radionice']:
            user[acthead + str(actn)] = item
            actn += 1
        for item in user['izlet']:
            user[acthead + str(actn)] = item
            actn += 1
        print(actn)

        templates = {}
        templates['0'] = "Template/2019/certificate_2019.pdf"
        templates['5'] = "Template/2019/certificate_2019_5.pdf"
        templates['10'] = "Template/2019/certificate_2019_10.pdf"
        templates['15'] = "Template/2019/certificate_2019_15.pdf"
        templates['20'] = "Template/2019/certificate_2019_20.pdf"
        templates['25'] = "Template/2019/certificate_2019_25.pdf"
        templates['30'] = "Template/2019/certificate_2019_30.pdf"
        templates['35'] = "Template/2019/certificate_2019_35.pdf"
        templates['40'] = "Template/2019/certificate_2019_40.pdf"
        templates['45'] = "Template/2019/certificate_2019_45.pdf"
        templates['50'] = "Template/2019/certificate_2019_50.pdf"

I'm writing this data

    user['id'] = data['recommendations'][0]['role_in_team']['user']['id']
    user['ime'] = data['recommendations'][0]['role_in_team']['user']['first_name']
    user['prezime'] = data['recommendations'][0]['role_in_team']['user']['last_name']
    user['tim'] = data['recommendations'][0]['role_in_team']['team']['short_name']
    user['mail'] = data['recommendations'][0]['role_in_team']['user']['estudent_email']
    user['puno_ime'] = (data['recommendations'][0]['role_in_team']['user']['first_name'] + ' ' +
                        data['recommendations'][0]['role_in_team']['user']['last_name'])
                user['predavanja'] = predavanja
    user['radionice'] = radionice
    user['izlet'] = izlet

One note. predavanja, radionice and izlet are lists. I've tried printing tpl which shows all the data being properly added to the scheme.


Solution

  • Turns out the issue was the naming of the variables since they didn't match the field names in the acroform PDF. So the solution was to rename the variables in the code to match the field names.