Django 中的 ReportLab [英] ReportLab in Django

查看:97
本文介绍了Django 中的 ReportLab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Django 中使用 ReportLab 制作条形图.我能够生成pdf,但它保存在我的根目录中.相反,我希望 PDF 应该在浏览器本身中打开.我该怎么做?

I'm making a bar graph using ReportLab in Django. I'm able to get the pdf generated, but it is saved in my root directory. Instead I want that the PDF should opened in the browser itself. How can I do it?

from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics import renderPDF
from django.http import HttpResponse

def generate_report(request):
    drawing = Drawing(400, 200)
    data = [
    (13, 5, 20, 22, 37, 45, 19, 4)
    ]
    bc = VerticalBarChart()
    bc.x = 50
    bc.y = 50
    bc.height = 125
    bc.width = 300
    bc.data = data
    #bc.strokeColor = colors.black
    bc.valueAxis.valueMin = 0
    bc.valueAxis.valueMax = 50
    bc.valueAxis.valueStep = 10
    bc.categoryAxis.categoryNames = ['Jan-99','Feb-99','Mar-99',
    'Apr-99','May-99','Jun-99','Jul-99','Aug-99']
    drawing.add(bc)

    # this returns None, but the file is saved in the directory.
    return HttpResponse(renderPDF.drawToFile(drawing, 'example.pdf', 'lineplot with dates')) 

编辑

def generate_report(request):
    # Create the HttpResponse object with the appropriate PDF headers.
    ctx = { "power_energy_update_interval" : gv.power_energy_update_interval, \
            "comparison_graph_update_interval" : gv.comparison_graph_update_interval, \
            "hourly_update_interval" : gv.hourly_update_interval
          }

    if request.is_ajax():
        if request.POST.has_key('start_date'):
            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

    return render(request, 'generate_report/reports.html', ctx) 

在 AJAX 请求响应中尝试时,它不返回响应.为什么?

When trying inside the AJAX request response, it does not returns the response. Why?

推荐答案

尝试:

# code...
page = renderPDF.drawToFile(drawing, 'example.pdf', 'lineplot with dates')
response = HttpResponse(page, mimetype='application/pdf')

我正在使用 reportlab.pdfgen.canvas 并且效果很好.
请记住,在将文件识别为 PDF 后在浏览器中打开文件是客户端的浏览器(和/或操作系统)工作.

I'm using reportlab.pdfgen.canvas and it works great.
Keep in mind that it's client's browser (and/or OS) job to open your file in browser after recognizing it as a PDF.


这就是我在某些 Web2Print 解决方案上显示发票的方式:


That's how I present invoices on some Web2Print solution:

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

def invoice_to_response(request, invoice):
    response = HttpResponse(mimetype='application/pdf')
    p = canvas.Canvas(response, pagesize='A4', pageCompression=0)
    # here I draw on 'p' like p.setFillColor(black)
    p.showPage()
    p.save()
    return response

这篇关于Django 中的 ReportLab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆