Search code examples
pythondjangopdfreportlab

inAttributeError: module 'reportlab.pdfgen.canvas' has no attribute 'acroform'


I want to create an acroform on PDF in Django by using reportLab. I have already installed the package for reportLab. But when implementing acroform im getting inAttributeError: module 'reportlab.pdfgen.canvas' has no attribute 'acroform'

from django.shortcuts import render
from django.http import HttpResponse

from io import BytesIO
from reportlab.pdfgen import canvas
from django.http import HttpResponse

def index(request):
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'

    buffer = BytesIO()

    # Create the PDF object, using the BytesIO object as its "file."
    p = canvas.Canvas(buffer)

    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")
    canvas.acroform.checkbox(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=True)

    # Close the PDF object cleanly.
    p.showPage()
    p.save()

    # Get the value of the BytesIO buffer and write it to the response.
    pdf = buffer.getvalue()
    buffer.close()
    response.write(pdf)
    return  response

I want to create a PDF like the following below refer here


Solution

  • Use

    p.acroForm.checkbox(..)
    

    instead of

    canvas.acroform.checkbox(..)
    

    Pay attention to the lowerCamelCase in "acroForm".