使用 wicked_pdf 从生成的 PDF 生成 ZIP

Adr*_*ian 6 heroku rubyzip ruby-on-rails-3 wicked-pdf

在我的发票系统中,我想要一个备份功能,将所有发票一次性下载到一个 zip 文件中。该系统在 heroku 上运行 - 因此只能临时保存 pdf。

我已经安装了 ruby​​zip 和 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
Run Code Online (Sandbox Code Playgroud)

以 BillsController#zip_all_bills 关闭流中的消息 IOError 结束

elc*_*elc 5

我认为你的代码有什么问题是你的 zip 没有,但你也将它用于个人 pdf。所以我认为当您尝试将临时文件用于 pdf 时,这是一个问题,因为您已经将它用于 zip。

但是我认为您根本不需要使用临时文件(而且我实际上从未在 Heroku 上使用过临时文件解决方案)

这是一个对我有用的控制器方法——也在heroku上使用wickedpdf和rubyzip。请注意,我没有使用 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
Run Code Online (Sandbox Code Playgroud)