[reportlab-users] Problem with pdfform (textField)

Robin Becker robin at reportlab.com
Fri Jan 22 06:20:34 EST 2010


On 22/01/2010 10:57, Arnaud Fontaine wrote:

> Hello,

>

> I'm trying to generate a PDF document containing Tables where some cells

> are PDF form (textField only). Here is the code (based on some code from

> xhtml2pdf project):

>

> from reportlab.platypus import Table, SimpleDocTemplate, Flowable

> from reportlab.pdfbase import pdfform

>

> class PDFFormTextField(Flowable):

> def __init__(self, name, width=10, height=10):

> self.name = name

> self.width = width

> self.height = height

>

> def wrap(self, *args):

> return (self.width, self.height)

>

> def draw(self):

> self.canv.saveState()

>

> pdfform.textFieldRelative(self.canv, self.name, 0, 0, self.width,

> self.height, value='foo')

> self.canv.restoreState()

>

> doc = SimpleDocTemplate("test2.pdf")

> t = Table([['col1', PDFFormTextField('123333')]])

> doc.build([t])

>

> However, this code produces an invalid document with the form object

> itself instead of the generated form:

>

> % 'Annot.NUMBER1': class PDFPattern

> 4 0 obj

> <reportlab.pdfbase.pdfpattern.PDFPattern instance at 0x85c040c>

> [...]

> % 'R6': class PDFCatalog

> 6 0 obj

> % Document Root

> << /AcroForm<reportlab.pdfbase.pdfform.AcroForm instance at 0x85c030c>

>

> On a given element, format() function (pdfbase/pdfdoc.py) calls the

> format() method of the element only if element object has an attribute

> __PDFObject__. However, this is not the case for PDFPattern and Acroform

> classes. I have attached to this email a short patch which works fine for

> me. Hope that's ok.

>

> Regards,

> Arnaud Fontaine

........
Thanks for the patch Arnaud,

I changed the way that pdfbase recognizes the formattable classes to be more
explicit a while ago and these must have been left out of that change.
--
Robin Becker


More information about the reportlab-users mailing list