PDF 模板未与 pdftk 正确合并数据

PDF template not merging data properly with pdftk

我正在使用 pdftk 编辑 PDF 模板

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

首先,我将数据写入 .xfdf 文件

    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()

我获取模板并如上所示,将数据从 xfdf 写入 pdf,但由于某种原因,只有 ime 被写入。

使用一些基本的条件逻辑获取模板,如下所示:

        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"

我正在写这个数据

    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

一张纸条。 predavanjaradioniceizlet 是列表。 我试过打印 tpl,它显示所有数据都已正确添加到方案中。

原来问题是变量的命名,因为它们与 acroform PDF 中的字段名称不匹配。所以解决方案是重命名代码中的变量以匹配字段名称。