inAttributeError:模块'reportlab.pdfgen.canvas'没有属性'acroform'

时间:2018-01-15 13:53:04

标签: python django pdf reportlab

我想使用reportLab在Django中创建一个关于PDF的acroform。我已经为reportLab安装了包。 但是在实施获取inAttributeError: module 'reportlab.pdfgen.canvas' has no attribute 'acroform'

的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

1 个答案:

答案 0 :(得分:2)

使用

p.acroForm.checkbox(..)

而不是

canvas.acroform.checkbox(..)

注意" acroForm"中的lowerCamelCase。