打开另一个pdf后,数组值在pdf中变为空白

时间:2018-06-20 07:47:00

标签: ruby-on-rails ruby ruby-on-rails-4 prawn

我有一个生成pdf的代码,为此,我必须在控制器中创建一个实例数组,然后将其传递给pdf渲染器。

当我打开pdf时,它可以正常工作并显示该值,但是,如果我打开另一个pdf,则显示为空白,如果我返回到以前打开的pdf,则现在也显示为空白。

我的代码在下面

order_sheet_controller.rb   

def print_order_sheet_summary
        @order_sheet = @company.order_sheets.friendly.find(params[:id])
        @item_array = []
        @valid_items = []
        @order_sheet.order_sheet_details.each do |order_sheet_detail|
            order_sheet_detail.jobs.first.items.each do |item| 
                @item_array << item
            end
        end

        @item_array.each do |item|
        if item.item_present?

        else
            #VALID_ITEMS << item
            @valid_items << item
               end
        end
        respond_to do |format|
            format.pdf do 
                pdf = OrderSheetSummaryPdf.new(@order_sheet, @valid_items, @item_array)
                send_data pdf.render, filename:
                "order_sheet_summary_#{@order_sheet.order_sheet_name}_#{@order_sheet.created_at.strftime("%d/%m/%Y")}.pdf",
                type: "application/pdf", disposition: "inline"
            end
        end
    end


order_sheet_summary_pdf.rb


class OrderSheetSummaryPdf < Prawn::Document

  def initialize(order_sheet, valid_items, item_array)
    super()
    @order_sheet = order_sheet
    @valid_items =[]
    @valid_items = valid_items
    @item_array = []
    @item_array = item_array
    order_sheet_summary
  end

  def total_item_quantity_in_item_array(v)
    quantity = 0
    @item_array.each do |i|
      if i.inventory_item == v.inventory_item
        quantity = quantity + i.total_quantity_with_wastage
      end
    end
    return quantity.round(2)
  end

  def order_sheet_summary
    move_down 10 

    table([ [ "Article Name", "Total quantity" ] ], :column_widths => [140, 90]) do
      cells.style(size: 8, :font_style => :bold)
    end

    @valid_items.each do |v|
      table([ [ "#{v.inventory_item.name}", "#{total_item_quantity_in_item_array(v)} #{v.inventory_item.consumption_unit} " ] ], 
        :column_widths => [140, 90])do
        cells.style(size: 8)
      end
    end

  end

请帮助!!!

0 个答案:

没有答案