博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django 生成复杂的 PDF 文件(数据较多时)
阅读量:7092 次
发布时间:2019-06-28

本文共 918 字,大约阅读时间需要 3 分钟。

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

from cStringIO import StringIOfrom reportlab.pdfgen import canvasfrom django.http import HttpResponsedef 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

 

转载地址:http://pbiql.baihongyu.com/

你可能感兴趣的文章
MySQL 5.6 的 GTIDs : 新复制协议和中断复制的新方法 【已翻译100%】
查看>>
国际电信联盟批准首个大数据标准
查看>>
Linus 爱 GPL,但不喜欢 GPL 诉讼
查看>>
《BackTrack 5 Cookbook中文版——渗透测试实用技巧荟萃》—第3章3.1节简介
查看>>
《SAP入门经典(第4版•修订版)》——2.2 ASAP和业务流程蓝图设计
查看>>
《深入剖析Nginx》——2.6 特殊应用逻辑的调试
查看>>
2017年度 OSC 源创会计划,技术干货精彩纷呈
查看>>
为什么说产品化是私有IaaS的唯一出路?
查看>>
阿里云联手学而思东家,给学习加点新元素
查看>>
C++语言基础 例程 案例:MyVector类的设计
查看>>
阿里云中间件产品ARMS公测 实时监控“一站式”解决
查看>>
shiro+ehcache缓存 和 验证码 和 记住我
查看>>
200多个js技巧代码(五)
查看>>
数据库设计原理:数据建模的三个阶段
查看>>
sendmail配置和学习
查看>>
MD5工具类
查看>>
邮件归档体现数据价值
查看>>
Executors 创建单例线程newSingleThreadExecutor
查看>>
Openstack(两控制节点+四计算节点)-4 模拟控制节点宕机
查看>>
Scribe快速安装方法
查看>>