如何浏览对虾PDF表格中的整行?

时间:2012-05-03 22:13:03

标签: ruby-on-rails ruby ruby-on-rails-3.1 pdf-generation prawn

我有一个Prawn 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中看到,我可以通过Document.generate的:inline_format选项查看每个单元格的文本,并将文本包装在"<strikethrough>#{text}</strikethrough>"中但是是否可以遍历整行?

2 个答案:

答案 0 :(得分:2)

我接受了这个,这就是我最终的结果:

策略是为每一行创建一个新表,并为列指定固定宽度,以便垂直分隔符对齐。在绘制一个表(行)之后,我检查我的条件,如果是,我将光标向上移动一半单元格的高度,绘制我的线,然后将其移回原来的位置。

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

答案 1 :(得分:0)

idx = 0 

        tableTest = pdf.make_table(data)


        while idx <= totalRows
            if tableTest.cells[idx, 2].content == "No Go"
                tableTest.cells[idx, 2].style(:background_color => 'FF0000')
            else 
                tableTest.cells[idx, 2].style(:background_color => '00FF22')
            end 
            idx += 1 
        end 

        tableTest.draw

        pdf.render_file "example.pdf"

这有效。您只需要制作表,然后迭代并在以后更改单个单元格即可。