pdfpdf-generationprawnprawnto

How to align the header and the column properly in prawn pdf?


We are generating pdf for invoice with prawn in rails 3.1.4.

  header =[[  "Date", "#", "sample name", "model","lease item", "hours", "$/hour", "Total"]]
  items = @invoice.invoice_items.map do |item|
    [
                item.lease_usage_record.lease_usage_items.map do |li|
                [
                  li.lease_date.strftime("%Y-%m-%d"),
                  item.lease_usage_record_id,
                  li.sample_name,
                  li.sample_model_num,
                  li.lease_item.short_name,          
                  li.total_hour,
                  li.charge_rate,
                  li.subtotal
                ]
                end
    ] 
  end
items = header + items
pdf.table items, :header => true, :width => 480 
t = pdf.make_table([ ["                                                       ", "Total:     ","    #{@invoice.total}"] ])
t.draw

It works with a problem. The problem is that all the data cells are squeezed into the first column which is "Date". Tried to add one one more [] to header and it generated error of "data is 2-dimensional array...". How to align each column properly under its header? Thanks so much.


Solution

  • The problem was solved by reducing the dimension of the data (items) to 2 dimension. The prawn can only take 2 dimensional data. The following line does the trick:

    items = items.flatten(2)