I’m using Weasyprint to create PDF’s. I’d like to loop through objects, create a pdf from each, and then append them all together.
I am stuck at the following point. I am able to loop through all of the items, create a pdf, but it only saves the last last item.
I’m new to both django and weasyprint, so although this is semi-working, I really have no idea what it is doing or where I should make my changes.
def export_pdf(request):
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'inline; attachment; filename=Expenses' + \
str(datetime.datetime.now())+'.pdf'
#remove inline to allow direct download
#response['Content-Disposition'] = 'attachment; filename=Expenses' + \
response['Content-Transfer-Encoding'] = 'binary'
workorders = Workorder.objects.all()
for x in workorders:
print(x.id)
items = WorkorderItem.objects.filter(workorder = x.id)
#print(items.workorder_hr)
html_string=render_to_string('pdf/weasyprint/pdf-output.html', {'items':items})
html = HTML(string=html_string)
result = html.write_pdf()
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(result)
output.flush()
#rb stands for read binary
output=open(output.name,'rb')
response.write(output.read())
return response