【问题标题】:How do I strike through an entire row in a prawn PDF table?如何删除对虾 P​​DF 表格中的整行?
【发布时间】:2012-05-13 11:29:26
【问题描述】:

我有一个 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>" 中,但是否可以删除整行?

【问题讨论】:

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


    【解决方案1】:

    我尝试了一下,结果是这样的:

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

    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
    

    【讨论】:

    • 嘿,看起来不错!一般来说,没有固定的列宽会很好,但对于我正在做的事情来说这很好。
    • @synecdoche 你可能只是把你的表格写出来,并确保使用固定的 height 单元格,然后将光标绝对移动到正确的位置,然后循环使用 each_with_index 再次获取数据并使用索引来计算将光标向下移动多远,但我不想对什么对你有用。
    • 非常感谢它对我有很大帮助@Unixmonkey
    【解决方案2】:
    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"
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 2022-11-30
      • 2014-05-26
      • 1970-01-01
      相关资源
      最近更新 更多