Reputation: 2135
I have a Pyramid web application that uses an internal web service to convert some data into a PDF file using reportlab. This works great, but only one PDF file is generated at a time. The client now wants to be able to print (it can be zipped up, instead of an actual print through a printer) multiple PDF files.
I currently have this piece of code at the bottom:
result = {"sales_order_data": {"sales_order_list": order_pdf_data}}
encoded_result = urllib.urlencode(result)
received_data = requests.post('http://convert_to_pdf/sales_order', data=encoded_result)
pdf_file = received_data.content
content_disposition = received_data.headers['Content-Disposition']
res = Response(content_type='application/pdf', content_disposition=content_disposition)
res.body = pdf_file
return res
The pdf_file is the binary form of the PDF file. I was thinking of running my pdf conversion code multiple times and each time storing the pdf binary data in a list of sorts and then using StringIO and ZipFile to zip a bunch of the files together.
I'm not too sure if this is possible:
list_of_pdf_files = []
for order in list_of_orders:
<processing of data here>
result = {"sales_order_data": {"sales_order_list": order_pdf_data}}
encoded_result = urllib.urlencode(result)
received_data = requests.post('http://convert_to_pdf/sales_order', data=encoded_result)
pdf_file = received_data.content
list_of_pdf_files.append(pdf_file)
zipped_file = <What do I do here to zip the list of pdf files?>
content_disposition = 'attachment; filename="pdf.zip"'
res = Response(content_type='application/zip', content_disposition=content_disposition)
res.body = zipped_file
return res
What do I do after getting a list of binary pdf files so that I can generate a zipped file in-memory and return that file via content-disposition as the response?
Upvotes: 2
Views: 901
Reputation: 798626
Use ZipFile.writestr()
to write the PDF files one at a time to the archive.
Upvotes: 1