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

Robin Becker robin at reportlab.com
Fri Aug 28 04:48:06 EDT 2020


Hi Mike,

I removed the editing parts of your test so creating script 1 below

I ran this in python27 and/or python3. I then opened mikeromey-0.pdf using evince (a linux pdf reader).

I edited 3 values to  'Hello' 'Cruel' 'World' 'Save Me'. was a bit hard as the field sizes need adjusting somewhat.

I used evince save as option to save a copy of the edited file 'mikeromey-1.pdf'.

If I use evince to re-open mikeromey-1.pdf then the edited values are still present. It follows that editing of the 
reportlab fields can take place and the values persist in the edited file.

In your example code it seems you have left out the place where you edit the file. If I run script2 after my evince 
edit and save I see this output

$ python mikeromey2.py
filename='mikeromey-0.pdf'
{'/DV': 'Value 0', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 0', '/V': 'Value 0'}
{'/DV': 'Value 1', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 1', '/V': 'Value 1'}
{'/DV': 'Value 2', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 2', '/V': 'Value 2'}
{'/DV': 'Value 3', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 3', '/V': 'Value 3'}
{'/DV': 'Value 4', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 4', '/V': 'Value 4'}
{'/DV': 'Value 5', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 5', '/V': 'Value 5'}
{'/DV': 'Value 6', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 6', '/V': 'Value 6'}
{'/DV': 'Value 7', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 7', '/V': 'Value 7'}
{'/DV': 'Value 8', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 8', '/V': 'Value 8'}
{'/DV': 'Value 9', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 9', '/V': 'Value 9'}

filename='mikeromey-1.pdf'
{'/DV': 'Value 0', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 0', '/V': 'Hello'}
{'/DV': 'Value 1', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 1', '/V': 'Cruel'}
{'/DV': 'Value 2', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 2', '/V': 'world'}
{'/DV': 'Value 3', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 3', '/V': 'Value 3'}
{'/DV': 'Value 4', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 4', '/V': 'Value 4'}
{'/DV': 'Value 5', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 5', '/V': 'Value 5'}
{'/DV': 'Value 6', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 6', '/V': 'Value 6'}
{'/DV': 'Value 7', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 7', '/V': 'Value 7'}
{'/DV': 'Value 8', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 8', '/V': 'Value 8'}
{'/DV': 'Value 9', '/FT': '/Tx', '/Ff': 0, '/T': 'Key 9', '/V': 'Save Me'}


so the PyPDF2 code does pick up the original and edited field values.


############################# script 1
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])

if __name__=='__main__':
     ORIGINAL_FILE = "mikeromey-0.pdf"
     createExamplePDFFormFile(ORIGINAL_FILE)



############################# script 2
if __name__=='__main__':
     ORIGINAL_FILE = "mikeromey-0.pdf"
     from pprint import pprint
     from rlextra.thirdparty import PyPDF2
     class readExamplePDFFormFile():
         def __init__(self, filename):
             f = PyPDF2.PdfFileReader(filename)
             data = f.getFields()
             for key, value in data.items():
                 pprint(value)

     readExamplePDFFormFile(ORIGINAL_FILE)
     EDITED_FILE = "mikeromey-1.pdf"
     readExamplePDFFormFile(EDITED_FILE)

-- 
Robin Becker


More information about the reportlab-users mailing list