2014-09-14 3 views
0

Я создал два индексных отчета с использованием креветок с рельсами 3. Я хотел бы иметь возможность объединить эти два pdf-файла как один pdf.Как слить две печатные страницы PDF с креветкой (рельсы 3)

Вот ссылки в поле зрения:

= 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 

ответ

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 

Я хотел бы рассмотреть вопрос о переносе кода в формате PDF креветки к вспомогательным методам, так что он доступен из-за пределы render процесса.

Смежные вопросы