generate ZIP from generated PDFs with wicked_pdf

独自空忆成欢 提交于 2019-12-06 03:10:46
elc

I think what is wrong in your code is that you have t for your zip but you also also use it for individual pdfs. So I think when you try to use the tempfile for a pdf it is a problem because you are already using it for the zip.

But I don't think you need to use tempfiles at all (and I never actually got a tempfile solution working on Heroku)

Here is a controller method that works for me--also using wickedpdf and rubyzip on heroku. Note that I'm not using a Tempfile instead I'm just doing everything with StringIO (at least I think that's the underlying tech).

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!