In my invoice system, I want a backup function to download all invoices at once in one zip file. This system is running on heroku - so it's only possible to save the pdfs temporary.
I've the rubyzip and wicked_pdf gem installed.
My current code in the controller:
def zip_all_bills
@bill = Bill.all
if @bill.count > 0
t = Tempfile.new("bill_tmp_#{Time.now}")
Zip::ZipOutputStream.open(t.path) do |z|
@bill.each do |bill|
@bills = bill
@customer = @bills.customer
@customer_name = @customer.name_company_id
t = WickedPdf.new.pdf_from_string(
render :template => '/bills/printing.html.erb',
:disposition => "attachment",
:margin => { :bottom => 23 },
:footer => { :html => { :template => 'pdf/footer.pdf.erb' } }
)
z.puts("invoice_#{bill.id}")
z.print IO.read(t.path)
end
end
send_file t.path, :type => "application/zip",
:disposition => "attachment",
:filename => "bills_backup"
t.close
end
respond_to do |format|
format.html { redirect_to bills_url }
end
end
This ends with the message IOError in BillsController#zip_all_bills closed stream