4
votes

using prawn gem i display table in the pdf file.. now i want to set position of the table, please guide me how to work on this..

i used the following code to genrate pdf report,

pdftable = Prawn::Document.new

pdftable.table([["Name","Login","Email"]],
:column_widths => {0 => 80, 1 => 80, 2 => 80, 3 => 80}, :row_colors => ["d5d5d5"])

 @users.each do|u|
 pdftable.table([["#{u.name}","#{u.login}","#{u.email}"]],
 :column_widths => {0 => 80, 1 => 80, 2 => 80, 3 => 80 }, :row_colors => ["ffffff"])

thanks

3
how do you want it positioned? - Ben Lee

3 Answers

3
votes

You can use function indent() in combination with functions move_down and move_up, so for example setting table at position(50,20) (relative to your cursor position) will look like this:

move_down 20
indent(50) do #this is x coordinate
    pdftable.table([["Name","Login","Email"]],
    :column_widths => {0 => 80, 1 => 80, 2 => 80, 3 => 80}, :row_colors => ["d5d5d5"])

    @users.each do|u|
    pdftable.table([["#{u.name}","#{u.login}","#{u.email}"]],
    :column_widths => {0 => 80, 1 => 80, 2 => 80, 3 => 80 }, :row_colors => ["ffffff"])
end

`

0
votes

You probably need to create a bounding_box around the table. See the documentation for bounding boxes.

Also: Do you realize you are creating a new table for the header and for each user?

0
votes

You need to include a dependency to prawn-layout (in addition to prawn) into your Gemfile:

# Gemfile
...
gem 'prawn'
gem 'prawn-layout'
...

then run a

bundle install

from the console, to let bundler download the new gem.

After this, you must include, where you want to generate your PDF, in addition to the prawn requirement, also the prawn/layout requirement:

# your_pdf_builder_lib.rb
require 'prawn'
require 'prawn/layout'

Done this, the only thing you have to write to center your table is something like this:

# your_pdf_builder_lib.rb
require 'prawn'
require 'prawn/layout'
...
def build_pdf
  p = Prawn::Document.new(:page_size => "A4")
  ...
  data = [["header 1", "header 2"], [data_col1, data_col2], ...] # your content for table
  ...
  p.table data, :position => :center # :position => :center will do the trick.
  ...
  p.render
end