Search code examples
ruby-on-railsprawnprawnto

Rubyonrails -generate Pdf using prawn.how to align table to center in pdf


In a ruby on rails application i have created pdf using prawn(o.12.0) and prawnto.In the pdf there is a table the issue is that i was not able to align the table to center.

when am giving :align =>center ,it throws an error as undefined.

how can i solve this issue.

pdf.bounding_box([5, 705], :width => 530, :height => 680) do
pdf.stroke_bounds

pdf.move_down(20) 
pdf.text "Demographics", :size => 15, :style => :bold, :align => :center


invoiceData = [["Facebook Pushes - Total users","Users Shared Exercise For   Week","Users Shared Exercise For Day","Users Share TestResult"],
[100","20","50","30"]]

pdf.table(invoiceData,:row_colors => ["FDFFFF", "FFFFFF"], :cell_style => { :border_width => 0.1,:border_color=> 'C1C1C1' }) do |table|

end

pdf.bounding_box([pdf.bounds.right - 50,pdf.bounds.bottom], :width => 60, :height => 20) do
pagecount = pdf.page_count
pdf.text "Page #{pagecount}"
end
  end

[screenshot]

https://i.sstatic.net/pqBtA.png


Solution

  •     pdf.bounding_box([5, 705], :width => 530, :height => 680) do
    pdf.stroke_bounds
    
    pdf.move_down(20) 
    pdf.text "Demographics", :size => 15, :style => :bold, :align => :center
    
    pdf.bounding_box([25,pdf.bounds.top-100], :width => 516, :height => 680) do
    invoiceData = [["Facebook Pushes - Total users","Users Shared Exercise For   Week","Users Shared Exercise For Day","Users Share TestResult"],
    [100","20","50","30"]]
    
    pdf.table(invoiceData,:row_colors => ["FDFFFF", "FFFFFF"], :cell_style => { :border_width => 0.1,:border_color=> 'C1C1C1' }) do |table|
    end
    end
    
    pdf.bounding_box([pdf.bounds.right - 50,pdf.bounds.bottom], :width => 60, :height => 20) do
    pagecount = pdf.page_count
    pdf.text "Page #{pagecount}"
    end
      end
    

    By defining a new bounding box above table likethis we can solve this issue

    pdf.bounding_box([25,pdf.bounds.top-100], :width => 516, :height => 680) do