1👍
It’s really two questions. How to generate pdf from data in the DB, and how to deliver it to a web client (browser).
In answer to the second, here’s a view I wrote earlier
from io import BytesIO
def pdfview( request, pk):
quote = get_object_or_404( Quote, pk=pk) # object to build pdf from
pdf = build_pdf( quote)
iobuf = BytesIO( bytearray(pdf.output( dest='S' ), encoding='latin-1'))
response = HttpResponse( iobuf, content_type='application/pdf')
response['Content-Disposition'
] = 'inline; filename={}.pdf'.format(quote.quotenumber)
# inline; will ask for an immediate display of the content
# attachment; will offer options to the user, including save without display
# exact details are browser-specific.
return response
As for build_pdf, I was using fpdf
from fpdf import FPDF
def build_pdf( quote):
pdf = FPDF()
# pdf.this( ...)
# pdf.that( ...)
# much later, when done
return pdf
Source:stackexchange.com