[reportlab-users] Problem generating PDF from Django/Python HTML Template

Lucas Cardona lucas at moleculenetworks.com
Tue Oct 10 11:31:24 EDT 2006


Hello, 

I have the following view in my Django app:
def print_pdf(request, language):
    ch = ContentHelper(language)
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; calgovcouncil_calendar.pdf'

    buffer = StringIO()

    # Create the PDF object, using the StringIO object as its "file."
    p = canvas.Canvas(buffer)

    # 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.")
    c = MyRequestContext(request, 
	       { 'content' : ch,
	          'language' : language,
	        }
	    )
	
    #challenge/dayview.html
    t = loader.get_template('information/24hrfitfacts.html')
    p.drawString(0,820, HttpResponse(t.render(c)))

    # Close the PDF object cleanly.
    p.showPage()
    p.save()

    # Get the value of the StringIO buffer and write it to the response.
    pdf = buffer.getvalue()
    buffer.close()
    response.write(pdf)
    return response

Now, this generates the PDF but the generated PDF doesn't contain the contents of the template but it only has one line:

n<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">n<

Any idea why this is happenning? I need the HTML to be generated dynamically as it contains information related to the user that is browsing the application.

Any help would be appreciated.

Thanks!



Lucas Cardona  




________________________________________________________________
Sent via the WebMail system at mail.moleculenetworks.com


 
                   


More information about the reportlab-users mailing list