0

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')) 

EDIT

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) 

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

PythonEnthusiast
  • 14,299
  • 33
  • 103
  • 218

1 Answers1

1

Try:

# code...
page = renderPDF.drawToFile(drawing, 'example.pdf', 'lineplot with dates')
response = HttpResponse(page, mimetype='application/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.

EDIT:
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
Ramon Melo
  • 250
  • 2
  • 8
  • 19
McAbra
  • 2,212
  • 2
  • 18
  • 28