[reportlab-users] Retrieving Form Values from a ReportLab PDF

Mike Romey mikeromey at gmail.com
Thu Aug 27 15:40:39 EDT 2020


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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist2.pair.net/pipermail/reportlab-users/attachments/20200827/4cc4b71b/attachment.html>


More information about the reportlab-users mailing list