2011-09-28 62 views
2

使用蝦寶石,我在PDF文件中顯示錶..現在我想設置表的位置,請指導我如何在這方面努力..紅寶石蝦寶石,表中的位置

我用下面的代碼到genrate PDF格式的報告,

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"]) 

感謝

+0

你想如何定位? –

回答

2

您可以組合使用功能縮進()與move_down功能和move_up, 因此,例如在位置(50,20)(相對於你的光標位置)設置表看起來就像這樣:

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

您需要包括的依賴於對蝦佈局(除了)到您的Gemfile

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

然後運行

bundle install 
從控制檯

,讓打捆下載新的寶石。

在這之後,你必須包括,要生成的PDF,除了要求,也是蝦/佈局要求:

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

這樣做,唯一你必須寫信給中心你的表是這樣的:

# 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