I am using reportlab to generate pdf. I have this function in django rest framework, in which I have a dictionary called data, I am doing a for to go through that data and show its key and value in the pdf, for this I do this:
@api_view()
def hello_world(request):
todos =
[
{
primer_apellido : Castro,
segundo_apellido : Mendieta,
primer_nombre : José,
segundo_nombre : Raúl,
genero : masculino,
lugar_de_nacimiento : Cuenca - Azuay,
fecha_de_nacimiento : 1967-10-20,
provincia : Loja,
canton : Cuenca,
ciudad : Loja,
parroquia : El Vecino,
direccion : Turunuma Alto. Urb. Elite. Casa A1.,
telefono_institucion : 13213213,
email_principal : [email protected]
}
]
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 the ReportLab documentation for the full list of functionality.
from reportlab.lib import colors
from reportlab.lib.units import inch
y = x = 0
dy = inch*3/4.0
dx = inch*5.5/5
w = h = dy/2
rdx = (dx-w)/2
rdy = h/5.0
texty = h+2*rdy
for todo in todos:
p.drawString(x+dx/2, y+texty, todo, todos[todo])
x = x+dx
y = y + dy
x = 0
# 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.
buffer.seek(0)
return FileResponse(buffer, as_attachment=True, filename='hello.pdf')
I use the drawString
reportlab function, but it only lets me do this p.drawString(x+dx/2, y+texty, todo)
so I can display only the key and not the key and value. How can I make the key and the value be displayed or if there is any other reportlab function to be able to do it. I hope you can help me. Thanks in advance
I was able to solve it this way:
Since the function
drawString
only allows three parameters.Simply concatenate strings in the call to
drawString
, for example: