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

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

我想在 Django 中使用 reportLab 在 PDF 上创建一个 acroform。我已经安装了 reportLab 的包。 但是在实施 acroform 时我得到 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

我想创建如下所示的 PDF refer here

使用

p.acroForm.checkbox(..)

而不是

canvas.acroform.checkbox(..)

注意"acroForm"中的小驼峰式。