zoukankan      html  css  js  c++  java
  • django 生成复杂的 PDF 文件(数据较多时)

    如果您在创建一个复杂的 PDF 文档(或者任何较大的数据块),请使用 cStringIO 库存放临时生成的 PDF 文件。 cStringIO 提供了一个用 C 编写的类似文件对象的接口,从而可以使系统的效率最高

    from cStringIO import StringIO
    from reportlab.pdfgen import canvas
    from django.http import HttpResponse
    
    def hello_pdf(request):
        # Create the HttpResponse object with the appropriate PDF headers.
        response = HttpResponse(mimetype='application/pdf')
        response['Content-Disposition'] = 'attachment; filename=hello.pdf'
    
        temp = StringIO()
    
        # Create the PDF object, using the StringIO object as its "file."
        p = canvas.Canvas(temp)
    
        # 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 StringIO buffer and write it to the response.
        response.write(temp.getvalue())
        return response
  • 相关阅读:
    序列化
    执行mysql脚本
    MinGW-notepad++开发c/c++程序
    MySql免安装版配置方法
    Wamp 简单使用方法
    [锋利JQ]-图片提示效果
    [锋利的JQ]-超链接提示效果
    PHPcms 系统简单使用
    NC帮助文档网址
    NC的开发模型
  • 原文地址:https://www.cnblogs.com/dengyg200891/p/5401660.html
Copyright © 2011-2022 走看看