如何将两个pdf打印与Prawn合并(rails 3)

时间:2014-09-14 07:29:35

标签: javascript ruby-on-rails ruby prawn

我已经使用Prawn和rails 3生成了两个索引报告。我希望能够将这两个pdf合并为一个pdf。

以下是视图中的链接:

 = link_to "Print inventories (PDF)", site_dc_power_inventories_path(@site, format: "pdf"), target:'_new'

      br
 = link_to "Print Equipment inventories (PDF)", site_equipment_inventories_path(@site, format: "pdf"), target:'_new'

equipment_controllers

  def index
    # @equipment = Equipment.all
    @equipment = Equipment.order("name asc" ).search(params[:keyword]).page params[:page]

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @equipment }
      format.pdf { render pdf: :contents }
    end
  end

dc_power_supplies_controllers

 def index
        # @dc_power_supplies = DcPowerSupply.all
        @dc_power_supplies = DcPowerSupply.order("name desc" ).search(params[:keyword]).page params[:page]
        @equipment = Equipment.order("name asc" ).search(params[:keyword]).page params[:page]

        respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @dc_power_supplies }
          format.json { render json: @equipment }
          format.pdf { render pdf: :contents }

        end

  end

查看/位点/ dc_power_inventories / index.pdf.prawn

prawn_document(page_layout: :landscape) do |pdf|

  ## Header

  pdf.repeat :all do
  pdf.image "#{Rails.root}/app/assets/images/power_logo.png", width: 150, height: 50
  pdf.image "#{Rails.root}/app/assets/images/bch_logo.png", width: 150, height: 40
    # header
    pdf.canvas do
      pdf.bounding_box([pdf.bounds.left, pdf.bounds.top], width: pdf.bounds.width) do
        pdf.cell content: "Site Inventories",
          width: pdf.bounds.width,
          height: 50,
          align: :center,
          borders: [],
          padding: 12
        pdf.cell content: Time.now.strftime("%Y/%m/%d"),
          width: pdf.bounds.width-35,
          height: 50,
          align: :right,
          borders: [],
          padding: 12
      end
    end
  end

  # Comments
  pdf.move_down 40
  pdf.text "Site DC Power Inventories for  ( #{@site.site_code} )", size: 10, style: :bold
  table_data = [['ID','DC Power Supply', 'Capacity (amp/hr)', 'Quantity', 'Total Capacity (amp/hr)']]
  if @dc_power_inventories.blank?
    table_data.concat([
     [{content: "No inventories found.", colspan: 5}]
    ])
  else
    table_data.concat(@dc_power_inventories.map {|i| 
      [i.id, i.dc_power_supply.name, i.dc_power_supply.battery.capacity, i.quantity, i.dc_power_supply.battery.capacity * i.quantity,  ]
    }.compact)
  end
  pdf.table(table_data, header: true, width: 440, column_widths: [40, 200, 80, 50, 70], cell_style: {size: 10})

  pdf.move_down 20



  pdf.text "Site Equipment Inventories for  ( #{@site.site_code} )", size: 10, style: :bold

  table_data = [['ID','Equipment', 'Amp', 'Quantity', 'Total Amp']]
  if @equipment_inventories.blank?
    table_data.concat([
     [{content: "No Equipment found.", colspan: 5}]
    ])

  else
    table_data.concat(@equipment_inventories.map {|i| 
      [i.id, i.equipment.name, i.equipment.amp.amp, i.quantity, i.equipment.amp.amp * i.quantity]
    }.compact)
  end
  pdf.table(table_data, header: true, width: 417, column_widths: [47, 200, 50, 50, 70], cell_style: {size: 10})

end

查看/位点/ equipment_inventories / index.pdf.prawn

prawn_document(page_layout: :landscape) do |pdf|

  ## Header

  pdf.repeat :all do
  pdf.image "#{Rails.root}/app/assets/images/power_logo.png", width: 150, height: 50
  pdf.image "#{Rails.root}/app/assets/images/bch_logo.png", width: 150, height: 35
    # header
    pdf.canvas do
      pdf.bounding_box([pdf.bounds.left, pdf.bounds.top], width: pdf.bounds.width) do
        pdf.cell content: "Site Inventories",
          width: pdf.bounds.width,
          height: 50,
          align: :center,
          borders: [],
          padding: 12
        pdf.cell content: Time.now.strftime("%Y/%m/%d"),
          width: pdf.bounds.width-35,
          height: 50,
          align: :right,
          borders: [],
          padding: 12
      end
    end
  end

  # Comments
  pdf.move_down 40
  pdf.text "Site Equipment Inventories for  ( #{@site.site_code} )", size: 10, style: :bold

  table_data = [['ID','equipment', 'Amp', 'Quantity', 'Total Amp']]
  if @equipment_inventories.blank?
    table_data.concat([
     [{content: "No inventories found.", colspan: 5}]
    ])

  else
    table_data.concat(@equipment_inventories.map {|i| 
      [i.id, i.equipment.name, i.equipment.amp.amp, i.quantity, i.equipment.amp.amp * i.quantity]
    }.compact)
  end
  pdf.table(table_data, header: true, width: 417, column_widths: [47, 200, 50, 50, 70], cell_style: {size: 10})


end

1 个答案:

答案 0 :(得分:0)

您可以使用CombinePDF gem合并PDF流。

您的伪代码可能如下所示:

pdf = CombinePDF.parse load('view/sites/dc_power_inventories/index.pdf.prawn')
pdf << CombinePDF.parse load('view/sites/equipment_inventories/index.pdf.prawn')
pdf.to_pdf # renders the object to a PDF stream that can be sent

我会考虑将Prawn pdf代码移动到辅助方法,以便可以从render工作流程外部访问。