ruby-on-rails-3pdfprawn

How do I format and style a sub table in Prawn?


I'm new to generating PDFs with Prawn so this may be a simple question but it's driving me crazy!

I have a table with a nested sub table. I have been able to style and format the main table easily but I can't quite seem to do the same with the nested sub table. All I really need to do to it is set the column widths and remove the borders but I can't seem to figure it out.

Here's the code I have so far:

  def line_items
    data = line_item_rows
    table(data)  do
       row(0).font_style = :bold
       columns(0).width = 160
       columns(1).width = 300
       columns(2).align = :right
       columns(2).valign = :bottom
       row(0).columns(2).valign = :top
       row(0).columns(2).align = :left
       self.header = true
    end   
  end

  def line_item_rows
    [["Description", "Items" ,"Price ex GST"]] +
    @line_items.map do |item|
      [item.description, sub_item_rows(item), price(item.charge_ex_gst)] 
    end +
    [["","Total", price(@project.charge_ex_gst)]]
  end

  def sub_item_rows(item)
   item.sub_items.map do |sub_item|
      ["#{sub_item.quantity} x  #{sub_item.name} #{price(sub_item.total_charge_ex_gst)}"] 
    end
  end

Any suggestions as to to how I apply styling to the sub table? Thanks in advance for your help.

Cheers, Mark


Solution

  • OK, here's how it was fixed. I needed to use "make_table" and apply the formatting there like so:

      def line_items
        move_down 15
        data = line_item_rows
    
            table(data) do
               row(0).font_style = :bold
               columns(0).width = 160
               columns(1).width = 300
               columns(2).align = :right
               columns(2).valign = :bottom
               row(0).columns(2).valign = :top
               row(0).columns(2).align = :left
               self.header = true
            end     
      end
    
      def line_item_rows
        [["Description", "Items" ,"Price ex GST"]] +
        @line_items.map do |item|
          [item.description, 
            sub_items(item), 
            price(item.charge_ex_gst)] 
        end +
        [["","Grand Total", price(@project.charge_ex_gst)]]
      end
    
      def sub_items(item)
        sub_data = sub_item_rows(item)
        make_table(sub_data) do
           columns(0).width = 200
           columns(1).width = 100
           columns(1).align = :right
           #columns(0).borders = []
        end   
      end
    
      def sub_item_rows(item)
       item.sub_items.map do |sub_item|
          ["#{sub_item.quantity} x #{sub_item.name}", price(sub_item.total_charge_ex_gst)] 
        end  +
          [["","Total"]]
      end