1

我有一个 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.majesticseaacreature.com/manual.pdf中看到,我可以使用 Document.generate 的 :inline_format 选项删除每个单元格的文本并将文本换行,"<strikethrough>#{text}</strikethrough>"但是否可以删除全线?

4

2 回答 2

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
于 2012-05-07T21:39:06.923 回答
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"

这行得通。您只需要制作表格,然后迭代并稍后更改各个单元格。

于 2020-11-02T17:55:16.197 回答