I've been struggling with this problem for sometime now. The documentation is very poor, and there doesn't appear to be any examples.
This code here, works perfectly fine.
path = "C:/Users/YourName/Desktop/"
from reportlab.pdfgen.canvas import Canvas as can
def greet(c):
c.drawString(220, 700, "Reporting")
c = can(path + "first.pdf")
greet(c)
c.showPage()
c.save()
I'd like to generate a PDF that has an interactive checkbox you can tick, or a radio button, etc.
I tried with the following code, but keep getting an AttributeError
.
from reportlab.pdfgen.canvas import Canvas as can
def welcome(c):
import reportlab as rep
rep.pdfbase.acroform.AcroForm.checkbox(rep.pdfbase.acroform.AcroForm,
name='CB0',tooltip='Field CB0',
checked=True,
x=72,y=72+4*36,
buttonStyle='diamond',
borderStyle='bevelled',
borderWidth=2,
borderColor="red",
fillColor="green",
textColor="blue",
forceBorder=False)
c3 = can(path + "story.pdf")
welcome(c3)
c3.showPage()
c3.save()
I keep getting an AttributeError: property object has no attribute _doc
If I remove rep.pdfbase.acroform.AcroForm
from the checkbox
method, I'll get an error saying missing 1 positional argument required. It's for the "self" parameter.
Any help would be greatly appreciated.
In ReportLab acroForm
is a property of a canvas
instance (capitalization is wrong in the documentation or code). So you will need to call the associated methods like this:
c = canvas.Canvas("example.pdf")
c.acroForm.checkbox()
You may run into some compatibility issues with PDF readers. It worked well with Adobe Acrobat Reader however it failed to function in some of the other readers or rendered incorrectly.
Here is a complete working version of the example you gave:
from reportlab.pdfgen import canvas
from reportlab.lib.colors import blue, green, white
def welcome(c):
c.acroForm.checkbox(
checked=True,
buttonStyle='check',
shape='square',
fillColor=white,
borderColor=green,
textColor=blue,
borderWidth=1,
borderStyle='solid',
size=20,
x=100,
y=100,
tooltip="example tooltip",
name="example_checkbox",
annotationFlags='print',
fieldFlags='required',
forceBorder=True,
relative=False,
dashLen=3)
c3 = canvas.Canvas("story.pdf")
welcome(c3)
c3.showPage()
c3.save()