2012-05-03 27 views
1

我有一个大虾PDF,打印关闭门票的列表中的表:如何在虾PDF表格中打印整行?

Prawn::Document.generate("doorlist.pdf") do 
    table([["Ticket", "Name", "Product"]] + tickets.map do |ticket| 
    [ 
    make_cell(:content => ticket.number, :font => "Courier"), 
    make_cell(:content => ticket.holder.full_name), 
    make_cell(:content => ticket.product.name) 
    ] 
    end, :header => true) 
end 

而且我想通过行罢工哪里ticket.has_been_used?是真的。我可以在Prawn文档http://prawn.majesticseacreature.com/manual.pdf中看到,我可以使用inline_format选项将每个单元格的文本打包到Document.generate并将文本包装在"<strikethrough>#{text}</strikethrough>"中,但是是否可以贯穿整行?

回答

2

我曾在此一展身手,而这也正是我结束了:

的战略是创建一个新表的每一行,因此垂直分隔排队指定列固定宽度。在绘制一个表格(行)之后,我检查了我的条件句,如果为true,我将光标上移一格的一半高度,画出我的线条,然后将其移回到原来的位置。

require 'prawn' 
tickets = [ 
    {:number => '123', :name => 'John', :product => 'Foo', :used => true }, 
    {:number => '124', :name => 'Bill', :product => 'Bar', :used => false}, 
    {:number => '125', :name => 'John', :product => 'Baz', :used => true} 
] 

Prawn::Document.generate("doorlist.pdf") do 

    widths = [150,180,200] 
    cell_height = 20 

    table([["Ticket", "Name", "Product"]], :column_widths => widths) 

    tickets.each do |ticket| 

    table([[ 
     make_cell(:content => ticket[:number], :height => cell_height, :font => "Courier"), 
     make_cell(:content => ticket[:name], :height => cell_height, :font => "Courier"), 
     make_cell(:content => ticket[:product], :height => cell_height, :font => "Courier") 
    ]], :column_widths => widths) 

    if ticket[:used] 
     move_up (cell_height/2) 
     stroke_horizontal_rule 
     move_down (cell_height/2) 
    end 

    end 

end 
+0

嘿,那看起来不错!一般来说不会有固定的列宽,但对于我所做的很好。 – synecdoche

+1

@synecdoche你可能只需写出你的表格,并确保使用固定的* height *单元格,并且只需将光标移到正确的位置,然后用each_with_index第二次遍历数据并使用索引计算向下移动光标的距离有多远,但我不想对什么会适合您的情况做出太多的假设。 – Unixmonkey

+0

非常感谢@Unixmonkey – Kashiftufail