问题描述:

I have a Prawn PDF that prints off a list of tickets in a table:

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

And I'd like to strike through the rows where ticket.has_been_used? is true. I can see in the Prawn documentation http://prawn.majesticseacreature.com/manual.pdf that I can strike through each cell's text with the :inline_format option to Document.generate and wrapping the text in "<strikethrough>#{text}</strikethrough>" but is it possible to strike through the whole line?

网友答案:

I had a go at this, and this is what I ended up with:

The strategy was to create a new table for each row, and specify fixed widths for the columns so the vertical separators line up. After drawing a table (row), I check my conditional, and if true, I move the cursor up half the height of a cell, draw my line, then move it back down to where it was.

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
相关阅读:
Top