Skip to content

Instantly share code, notes, and snippets.

@suspectpart
Created April 5, 2017 07:55
Show Gist options
  • Save suspectpart/84aa07cc0075fa2729a5d6282b9856f3 to your computer and use it in GitHub Desktop.
Save suspectpart/84aa07cc0075fa2729a5d6282b9856f3 to your computer and use it in GitHub Desktop.
FDF and XFDF generator to populate forms on a PDF file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class XFDF(object):
"""Generate XFDF files to populate PDF forms with data.
Form data on PDF files can be populated by using one of these formats:
- FDF (File Data Format)
- XFDF (XML Forms Data Format)
XFDF is an XML format and utf-8 by default. Prefer this to FDF if possible.
The generated XFDF file can be applied to an existig PDF file
with severaldifferent tools, e.g. pdftk.
"""
def __init__(self, checkbox_yes):
"""Initialize with a positive value for checkboxes(as it may vary)"""
self.fields = {}
self._checkbox_yes = checkbox_yes
def _footer(self):
footer = '\n</fields>\n' \
'<f href="test.pdf"/>\n' \
'</xfdf>'
return footer
def _header(self):
header = '<?xml version="1.0" encoding="UTF-8"?>\n' \
'<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">\n' \
'<fields>\n'
return header
def _fields(self):
field_str = '<field name="{0}">\n' \
'<value>{1}</value>\n' \
'</field>'
fields = [field_str.format(t, v) for t, v in self.fields.items()]
return "\n".join(fields)
def add_field(self, key, value):
self.fields[key] = value
def check(self, field):
self.fields[field] = self._checkbox_yes
def uncheck(self, field):
self.fields[field] = "Off"
def generate(self):
return self._header() + self._fields() + self._footer()
class FDF(object):
"""Generate FDF (File Data Format) files to populate PDF forms with data.
Form data on PDF files can be populated by using one of these formats:
- FDF (File Data Format)
- XFDF (XML Forms Data Format)
FDF is kind of dated, has an awkward syntax and features only
limited support for special characters. If possible, use XFDF.
The generated FDF file can be applied to an existig PDF file
with severaldifferent tools, e.g. pdftk.
"""
def __init__(self, checkbox_yes):
"""Initialize with a positive value for checkboxes(as it may vary)"""
self.fields = {}
self._checkbox_yes = checkbox_yes
def _footer(self):
footer = "] >> >>\n" \
"endobj\n" \
"trailer\n" \
"<</Root 1 0 R>>\n" \
"%%EOF"
return footer
def _header(self):
header = "%FDF-1.2 \n" \
"1 0 obj<</FDF<< /Fields[\n"
return header
def _fields(self):
field_str = "<< /T({0}) /V({1}) >>"
fields = [field_str.format(t, v) for t, v in self.fields.items()]
return "\n".join(fields)
def add_field(self, name, value):
"""Add a value for a named field"""
self.fields[name] = value
def check(self, field):
"""Check a form checkbox"""
self.fields[field] = self._checkbox_yes
def uncheck(self, field):
"""Uncheck a form checkbox"""
self.fields[field] = "Off"
def generate(self):
"""Generate FDF text from field definitions"""
return self._header() + self._fields() + self._footer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment