I need to know how to show the records of a query in the database to a pdf document using python and reportlab.
The idea is to show a table in a pdf where the data generated by the query to the DB is included
I need to know how to show the records of a query in the database to a pdf document using python and reportlab.
The idea is to show a table in a pdf where the data generated by the query to the DB is included
This is a simple example:
import io
from django.http import FileResponse
from reportlab.pdfgen import canvas
def generate_report(request):
# create a file-like buffer to receive PDF data.
buffer = io.BytesIO()
# create the PDF object, using the buffer as its "file."
P = canvas.Canvas(buffer)
# Draw things on the PDF. Here's where the PDF generation happens.
# See 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()
# FileResponse sets the Content-Disposition header so that browsers
# present the option to save the file.
return FileResponse(buffer, as_attachment=True, filename="hello.pdf")
for more information watch this tutorial on youtube: link