6

請求書システムで、すべての請求書を一度に 1 つの 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

これは、BillsController#zip_all_bills クローズド ストリームのメッセージ IOError で終了し ます。

4

1 に答える 1