1

当试图用 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)
4

1 回答 1

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格式

于 2013-06-07T18:52:16.960 回答