django生成文件txt、pdf(在生成 PDF 文件之前,需要安装 ReportLab 库)

from django.http import HttpResponse
def download_file(request):   
    
    # Text file  
    #response = HttpResponse(mimetype='text/plain')   #定义输出格式为txt                                
    #response['Content-Disposition'] = 'attachment; filename=my.txt'   #规定文件名字 
  
  
#给txt写入内容
#response.write("aa/n") #response.write("bb") # PDF file
#在生成 PDF 文件之前,需要安装 ReportLab 库
#http://code.djangoproject.com/svn/django/branches/0.95-bugfixes/docs/outputting_pdf.txt from reportlab.pdfgen import canvas response = HttpResponse(mimetype='application/pdf') response['Content-Disposition'] = 'attachment; filename=somefilename.pdf' p = canvas.Canvas(response) p.drawString(100, 100, "Hello world.") p.showPage() p.save() return response

 

posted @ 2016-04-17 17:49  Xiao|Deng  阅读(1038)  评论(0编辑  收藏  举报