Search code examples
ruby-on-railsrubypdf-generationprawn

Prawn: Table of content with page numbers


I need to create a table of contents with Prawn. I have add_dest function calls in my code and the right links in the table of content:

add_dest('Komplett', dest_fit(page_count - 1))

and

text "* <link anchor='Komplett'> Vollstaendiges Mitgliederverzeichnis </link>", :inline_format = true

This works and I get clickable links which forward me to the right pages. However, I need to have page numbers in the table of content. How do I get it printed out?


Solution

  • you should read the chapter on Outline in this document http://prawn.majesticseacreature.com/manual.pdf, p.96. It explains with examples on how to create TOC.

    UPDATE

    destinations, page_references = {}, {}
    
    page_count.downto(1).each {|num| page_references[num] = state.store.object_id_for_page(num)}
    
    dests.data.to_hash.each_value do |values|
        values.each do |value|
            value_array             = value.to_s.split(":")
            dest_name               = value_array[0]
            dest_id                 = value_array[1].split[0]
            destinations[dest_name] = Integer(dest_id)
        end 
    end 
    
    state.store.each do |reference| 
        if !(dest_name = destinations.key(reference.identifier)).nil?
            puts "Destination - #{dest_name} is on Page #{page_references.key(Integer(reference.data[0].to_s.split[0]))}"
        end 
    end