Replies: 3 comments 3 replies
-
Thank you for opening a new discussion as a follow-up of #116 As suggested previously, could you provide some minimal (but fully functional) Djanjo app reproducing your problem, please ? With only 2 lines of Python code provided, I won't be able to help much... |
Beta Was this translation helpful? Give feedback.
-
You did not provide a fully functional Django app 😔 Still, I was able to build one. There is the code you want to use: from fpdf import FPDF
from django.http import HttpResponse
from django.shortcuts import render
def report(request):
...
return HttpResponse(bytes(pdf.output()), content_type='application/pdf') |
Beta Was this translation helpful? Give feedback.
-
Often times, this category of problem is solved by writing data to a BytesIO object instead of a file. This gives you a little bit more flexibility, as a BytesIO object generally behaves as a File object does, but its entirely stored in local memory. In this specific example, it doesn't matter too much because HttpResponse accepts a bytestring, so you can just use that method as @Lucas-C has described. If you want to use FileResponse, you would need to generate one of these BytesIO buffers: import io
from fpdf import FPDF
from django.http import FileResponse
def report(request):
...
buf = io.BytesIO(pdf.output(dest='S'))
buf.seek(io.SEEK_SET) # Moves the cursor back to the start of the file.
return FileResponse(buf, is_attachment=True, filename='Quiz Report.pdf') |
Beta Was this translation helpful? Give feedback.
-
I already following this https://python.plainenglish.io/generate-and-serve-pdf-files-with-django-e3efd9fde7bc
but there are some issue what if there are multiple user generate the PDF on same times the first user will get same like the second one if the filenames is same...
Is there anyway to make output not saved to server first but send to front end immediately?
Code I using now with saving to server first:
Code I already try without saving to server:
Result:
Second Result: (If I remove content_type)
Beta Was this translation helpful? Give feedback.
All reactions