开发者

ruby prawn gem, table position

开发者 https://www.devze.com 2023-04-09 02:54 出处:网络
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..

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开发者_StackOverflow社区.email}"]],
 :column_widths => {0 => 80, 1 => 80, 2 => 80, 3 => 80 }, :row_colors => ["ffffff"])

thanks


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

`


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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消