Search code examples
ruby-on-railsprawn

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


I'm using Prawn gem to generate PDF documents within my Rails Application. I've successfully created the PDFs But I have a table of contents and I wonder is it possible Or Is there is a way to create the leader dots effect in Prawn enter image description here?


Solution

  • Yes, there is a way to create the leader dots effect in Praw.

    If you have:

    1. the total available width
    2. width of the table of content entry
    3. width of the page number
    4. width of a single leader dot

    space_for_dots = 1. - 2. - 3.

    dots = (space_for_dots / 4) * '.'

    toc_entry = entry_string + dots + entry_page_number


    To calculate the width of a string in prawn, you can use compute_width_of, which is part of the font classes.

    An example for the picture you added could look like this:

    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)