The file will not display until the whole file is loaded. How can i display the progress in browser?
from io import BytesIO
from reportlab.pdfgen import canvas
from django.http import HttpResponse
def some_view(request):
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
buffer = BytesIO()
# Create the PDF object, using the BytesIO object as its "file."
p = canvas.Canvas(buffer)
p.drawString(**, **, "Hello world.") # draw pdf, size > 10M
# Close the PDF object cleanly.
p.showPage()
p.save()
# Get the value of the BytesIO buffer and write it to the response.
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response
Well, in your example I think it would be pretty simple (and almost instantaneous). You would just print out/return a progress indicator for
drawString,showPage, etc. Assuming the actual PDF you’re generating is more involved, you could use thesetProgressCallBackmethod of theBaseDocTemplateclass. Of course, this would require that you use the reportlab platypus engine.There are a few methods/properties of note (see the comments), let’s say you have a custom template class which overrides
BaseDocTemplate:Here are some more values (you can see the code in
reportlab.platypus.doctemplate):