使用对虾gem我在pdf文件中显示表格..现在我想设置桌子的位置,请指导我怎么做。
我使用以下代码生成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"])
谢谢
发布于 2011-12-18 22:58:52
您可以将函数缩进()与函数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
`
发布于 2011-09-28 05:46:22
您可能需要围绕该表创建一个bounding_box
。请参阅documentation for bounding boxes。
另外:您是否意识到您正在为标题和每个用户创建一个新的表?
发布于 2012-04-23 14:21:04
除了对虾之外,还需要将对对虾布局的依赖项包括到Gemfile
中
# Gemfile
...
gem 'prawn'
gem 'prawn-layout'
...
然后运行一个
bundle install
从控制台,让bundler下载新的gem。
在此之后,除了对虾要求之外,还必须包括对虾/布局要求:
# 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
https://stackoverflow.com/questions/7578678
复制