Я делаю гистограмму, используя ReportLab в Django. Я могу получить pdf-файл, но он сохраняется в моем корневом каталоге. Вместо этого я хочу, чтобы PDF-файл был открыт в самом браузере. Как мне это сделать?ReportLab в Django
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)
При попытке внутри ответа на запрос AJAX, он не возвращает ответ. Зачем?
Это было [предложено до] (http://stackoverflow.com/q/1156246/165103), пожалуйста, используйте поиск, прежде чем задавать новый вопрос. –
Это не решает мою проблему. Можете ли вы предоставить демонстрационную версию. Спасибо – PythonEnthusiast
Пожалуйста, ответьте на эту тему. http://stackoverflow.com/questions/20343382/pyrhon-reportlabs-save-2-different-graphs-in-2-different-pages. Спасибо – PythonEnthusiast