在我的发票系统中,我想要一个备份功能,将所有发票一次下载到一个zip文件中。这个系统是在heroku上运行的,所以只能临时保存pdf文件。
我已经安装了rubyzip和wicked_pdf gem。
我当前在控制器中的代码:
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
以消息结束IOError in BillsController#zip_all_bills closed stream
我认为您的代码中有什么问题是您的zip中有t,但您也将其用于单个pdf。所以我认为,当你试图使用pdf的tempfile时,这是一个问题,因为你已经在使用它的zip。
但是我认为你根本不需要使用tempfile(实际上我从来没有在Heroku上得到一个tempfile解决方案)
这是一个控制器方法,为我工作-也使用wickedpdf和rubyzip在heroku。请注意,我没有使用Tempfile,而是使用StringIO做所有事情(至少我认为这是底层技术)。
def dec_zip
require 'zip'
#grab some test records
@coverages = Coverage.all.limit(10)
stringio = Zip::OutputStream.write_buffer do |zio|
@coverages.each do |coverage|
#create and add a text file for this record
zio.put_next_entry("#{coverage.id}_test.txt")
zio.write "Hello #{coverage.agency_name}!"
#create and add a pdf file for this record
dec_pdf = render_to_string :pdf => "#{coverage.id}_dec.pdf", :template => 'coverages/dec_page', :locals => {coverage: coverage}, :layout => 'print'
zio.put_next_entry("#{coverage.id}_dec.pdf")
zio << dec_pdf
end
end
# This is needed because we are at the end of the stream and
# will send zero bytes otherwise
stringio.rewind
#just using variable assignment for clarity here
binary_data = stringio.sysread
send_data(binary_data, :type => 'application/zip', :filename => "test_dec_page.zip")
end