【问题标题】:How to fill PDF form in Django/Python?如何在 Django/Python 中填写 PDF 表单?
【发布时间】:2016-11-10 16:43:32
【问题描述】:

我正在尝试用数据库数据填充预制的 pdf 表单并将其展平。例如,如果用户输入一个名为“name”的字段,则应将其放在 pdf 表单的 name 字段中。

【问题讨论】:

  • 你取得了多大的成就?你能显示一些代码吗?

标签: python django forms pdf


【解决方案1】:

您可以将reportlab 库与 Django 视图结合使用:

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

def some_view(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.")

    # 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

Django 文档:https://docs.djangoproject.com/en/1.9/howto/outputting-pdf/

Reportlab 文档:http://www.reportlab.com/documentation/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-26
    • 2012-02-26
    • 2010-12-25
    • 2018-07-03
    • 1970-01-01
    • 2012-07-26
    • 2022-06-29
    相关资源
    最近更新 更多