Retrieving Form Values from a ReportLab PDF
Mike Romey <[email protected]> Thu, 27 Aug 2020 12:40:39 -0700
| Newsgroups | gmane.comp.python.reportlab.user |
|---|---|
| Message-ID | <CAN8ip18QvThw2mJ4EidrGAvX3PksHC9OKccY2CTAhUQ_NdRPrw@mail.gmail.com> |
I am having some issues with ReportLab and forms. I created a simple
example to illustrate my issue. In this example, I am using a flowable to
insert a textFieldRelative object in a cell of a table. So far all of this
works as expected. However, when I open up the saved file and edit the
textFieldRelative object with new values and retrieve the value using
PyPDF2 and or pdfminer the edited form contains the original value, not the
edited form value. What am I missing?
import PyPDF2
from reportlab.platypus import Flowable, SimpleDocTemplate, Table
from reportlab.pdfbase import pdfform
from pprint import pprint
class formInputField(Flowable):
def __init__(self, key, value):
self.key = key
self.value = value
self.width = 0
self.height = 10
def wrap(self, *args):
self.width = args[0]
return (self.width, self.height)
def draw(self):
self.canv.saveState()
pdfform.textFieldRelative(
self.canv, self.key, 0, 0, 50, 10, self.value)
self.canv.restoreState()
class createExamplePDFFormFile():
def __init__(self, filename):
data = []
for i in range(10):
key = "Key %s" % (i)
value = "Value %s" % (i)
inputField = formInputField(key, value)
data.append([key, inputField])
dataTable = Table(data)
doc = SimpleDocTemplate(filename)
doc.build([dataTable])
class readExamplePDFFormFile():
def __init__(self, filename):
f = PyPDF2.PdfFileReader(filename)
data = f.getFields()
for key, value in data.items():
pprint(value)
ORIGINAL_FILE = "OriginalFile.pdf"
EDITED_FILE = "EditedFile.pdf"
createExamplePDFFormFile(ORIGINAL_FILE)
readExamplePDFFormFile(ORIGINAL_FILE)
readExamplePDFFormFile(EDITED_FILE)