带有colspan和宽度的表格单元格的大虾CannotFit错误

时间:2013-06-07 17:40:27

标签: ruby prawn

尝试使用Prawn创建http://rghost.ru/46587227.view这样的表时,会导致出现一个CannotFit错误:

first = {:content=>"Foooo fo foooooo",:width=>50,:align=>:center}
second = {:content=>"Foooo",:colspan=>2,:width=>70,:align=>:center}
third = {:content=>"fooooooooooo, fooooooooooooo, fooo, foooooo fooooo",:width=>55,:align=>:center}
fourth = {:content=>"Bar",:width=>20,:align=>:center}

table_content = [[
  first,
  [[second],[third,fourth]]
]]
pdf.move_down(20)
pdf.table(table_content)

1 个答案:

答案 0 :(得分:4)

Prawn在计算colspan的细胞的细胞大小时遇到​​问题。

我能够通过遵循以下规则解决此问题:

  • 不要将width分配给colspan
  • 的单元格
  • 为表格提供正确的总宽度

应用于您的示例

 first = { content: "Foooo fo foooooo", rowspan: 2, width: 50 }
second = { content: "Foooo", colspan: 2 } # <- avoid width here!
 third = { content: "fooooooooooo, fooooooooooooo, fooo, foooooo fooooo", width: 55 }
fourth = { content: "Bar", width: 20 }

Prawn::Document.generate("test.pdf") do |pdf|
  table_content = [ [first, second       ],
                    [       third, fourth],
                    [1,     2,     3     ] ]
  pdf.move_down(20)
  pdf.table(table_content, width: 50+55+20, cell_style: {align: :center})
end

输出

PDF