大虾:Table的内容如何制作“Leader dots”?

Prawn: How to make “Leader dots” for Table of content?

我正在使用 Prawn gem 在我的 Rails 应用程序中生成 PDF 文档。我已经成功创建了 PDF 但是我有一个 table 的内容,我想知道是否有可能或者有没有办法在 Prawn ?

中创建引导点效果

是的,有一种方法可以在 Praw 中创建引导点效果。

如果你有:

  1. 总可用宽度
  2. 内容条目table的宽度
  3. 页码宽度
  4. 单个 前导点的宽度

space_for_dots = 1. - 2. - 3.

dots = (space_for_dots / 4) * '.'

toc_entry = entry_string + dots + entry_page_number


大虾计算一个字符串的宽度可以用compute_width_of,它是字体类的一部分。

您添加的图片示例如下所示:

def build_toc_entry(left_text, right_text, available_width, text_size)
  left_text_width = font(YOUR_FONT).compute_width_of(left_text, size: text_size)
  right_text_width = font(YOUR_FONT).compute_width_of(right_text, size: text_size)
  dot_width = font(YOUR_FONT).compute_width_of('.', size: text_size)
  space_width = font(YOUR_FONT).compute_width_of(' ', size: text_size)

  space_for_dots = available_width - left_text_width - right_text_width - space_width * 2
  dots = '.' * (space_for_dots / dot_width)

  "#{left_text} #{dots} #{right_text}" # return the finished toc entry
end

text 'Locate the information on the page indicated.'
text build_toc_entry('Leader Dots', '3', 350, 8)
text build_toc_entry('Dingbats', '5', 350, 8)
text build_toc_entry('Bullets', '8', 350, 8)

这有点晚了,但这是我为使点仅与左侧文本正确对齐所做的操作。但我认为修改左右文本会相当容易:

def build_dots(text, available_width, text_size)
        dots_hash = {}
        current_font = font.inspect.split('<')[1].split(':')[0].strip
      text_width = font(current_font).compute_width_of(text, size: text_size)
      dot_width = font(current_font).compute_width_of('.', size: text_size)
      space_width = font(current_font).compute_width_of(' ', size: text_size)

      space_for_dots = available_width - text_width - space_width * 2
      dots = '.' * (space_for_dots / dot_width)
      dots_width = font(current_font).compute_width_of(dots, size: text_size)
      dots_start = available_width - dots_width - (space_width * 2)
      dots_hash[:dots] = dots
      dots_hash[:position] = dots_start

      return dots_hash
end

    dot_values = build_dots(officer.title,150,text_size)
    p = 0
    float {
        text "#{officer.title}", size: text_size
    }

    indent(dot_values[:position]) do
        float {
            text dot_values[:dots], size: text_size
        }
    end

    p += 150
    indent(p,0) do
        text "#{officer.name}", size: text_size
    end