Django 2.1.3 文档-视图层-生成PDF

用Django输出PDF

本文档介绍了如何使用Django视图动态输出PDF文件。这是通过优秀的开源Python PDF库ReportLab 实现的。

动态生成PDF文件的优点是,您可以为不同目的创建自定义PDF,例如,针对不同用户或不同内容。

例如,Django在kusports.com上使用,为参与March Madness比赛的人们生成定制的,适合打印的NCAA锦标赛PDF文件。

安装ReportLab

ReportPab库可在PyPI上使用。一个用户指南(PDF文件)也可供下载。您可以使用以下 pip命令安装ReportLab:

 pip install reportlab

通过在Python交互式解释器中导入它来测试您的安装:

>>> import reportlab

如果该命令没有引发任何错误,则安装有效。

编写你的视图

使用Django动态生成PDF的关键是ReportLab API作用于类文件对象,而Django的FileResponse 对象接受类文件对象。

这是一个“Hello World”示例:

import io
from django.http import FileResponse
from reportlab.pdfgen import canvas

def some_view(request):
    # Create a file-like buffer to receive PDF data.
    buffer = io.BytesIO()

    # Create the PDF object, using the buffer 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, and we're done.
    p.showPage()
    p.save()

    # FileResponse sets the Content-Disposition header so that browsers
    # present the option to save the file.
    return FileResponse(buffer, as_attachment=True, filename='hello.pdf')

代码和注释应该是不言自明的,但有些事情值得一提:

  • 响应将 根据文件扩展名自动设置MIME类型为 application / pdf。这告诉浏览器该文档是PDF文件,而不是HTML文件或application/octet-stream的二进制内容。
  • 传递给FileResponse一个as_attachment=True属性时,它设置适当的Content-Disposition标题,并告诉Web浏览器弹出一个对话框,提示/确认如何处理文档,即使在机器上设置了默认值。如果as_attachment省略该参数,浏览器将使用它们已配置用于PDF的任何程序/插件来处理PDF。
  • 您可以提供任意filename参数。浏览器将在“另存为…”对话框中使用它。
  • 连接到ReportLab API非常简单:传递给canvas.Canvas的第一个冲区参数缓也可以提供给 FileResponse类。
  • 请注意,所有后续PDF生成方法都在PDF对象上调用(在本例中p),而不是buffer。
  • 最后,调用PDF文件上的showPage()和save()非常重要。

注解
ReportLab不是线程安全的。我们的一些用户报告了构建生成PDF的Django视图的奇怪问题,这些视图可以被许多人同时访问。

译者注:
上面的代码是Django 2.1提供的,本地测试暂时不能生成文档:
下面附上可以生成PDF文档的代码(Django 2.0提供):

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

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

    # 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, and we're done.
    p.showPage()
    p.save()
    return response

其他格式

请注意,这些示例中没有很多特定于PDF的内容 - 只是使用了reportlab的一点。您可以使用类似的技术生成任何可以找到Python库的任意格式。另请参阅 使用Django输出CSV以获取另一个示例以及在生成基于文本的格式时可以使用的一些技巧。

参见

Django Packages提供了有助于从Django生成PDF文件的一些软件包

猜你喜欢

转载自blog.csdn.net/lengfengyuyu/article/details/84558231