Document with sections, each with it's own page numbering?

140 views
Skip to first unread message

wchrisjohnson

unread,
Feb 23, 2012, 2:05:14 PM2/23/12
to prawn...@googlegroups.com
I have a document that is being generated perfectly, thanks to Prawn. I have one remaining challenge that I suspect Prawn can handle, I'm just not sure how. I have a lot of experience with Prawn v0.4, but have only been using the latest for a couple of days.

It has a single page up front that is a summary/overview page, and then sections of detail pages. We would like for each section to have page numbering specific to the group, like so:

Summary page

Section 1 - page 1 of 3
Section 1 - page 2 of 3
Section 1 - page 3 of 3

Section 2 - page 1 of 2
Section 2 - page 2 of 2

Section 3 - page 1 of 4
Section 3 - page 2 of 4
...

Is there a way to "reset" page numbering? Is there a way to handle this in Prawn as-is with the current version?

I would appreciate some direction!

Chris

Brad Ediger

unread,
Feb 23, 2012, 2:44:49 PM2/23/12
to prawn...@googlegroups.com
On Thu, Feb 23, 2012 at 1:05 PM, wchrisjohnson <wchris...@gmail.com> wrote:
> I have a document that is being generated perfectly, thanks to Prawn. I have
> one remaining challenge that I suspect Prawn can handle, I'm just not sure
> how. I have a lot of experience with Prawn v0.4, but have only been using
> the latest for a couple of days.
>
> It has a single page up front that is a summary/overview page, and then
> sections of detail pages.

We don't support this, but Prawn's page numbering implementation is
pretty simple, using the repeater API. You can probably yank out the
innards and repurpose them to do what you want:

https://github.com/prawnpdf/prawn/blob/master/lib/prawn/document.rb#L569

-be

wchrisjohnson

unread,
Feb 23, 2012, 4:09:21 PM2/23/12
to prawn...@googlegroups.com
Thanks Brad. I now have it working. Thought I'd post it to help someone else.


@section_page_numbers_array = []

def generate_section
   start_new_page 
   section_page_number_start = page_number
   ...
   ... generate pages 1..N here
   ...
   @section_page_numbers_array.push [section_page_number_start, page_number]
end

This generates an array that looks like this:
[[2, 3], [4, 6], [7, 11], [12, 14], [15, 15], [16, 17], [18, 19], [20, 22]]

Then at the end of my document generation, this is the last thing I do:

@section_page_numbers_array.each do |x, y|
    string = "TRANSACTIONS BY FUND - PAGE <page> OF <total>"
    total_pages = y-x+1
    options = { :at => [bounds.right - 300, 10],
                  :width          => 300,
                  :align          => :right,
                  :page_filter    => (x..y),
                  :total_pages    => total_pages, 
                  :start_count_at => 1, 
                  :style          => :bold }
    number_pages string, options
end

This goes back and adds the footer with the appropriate page start# (always 1) and end# (total_pages) to the footer of each section.

Thanks for your response, and such a great product!

Chris
Reply all
Reply to author
Forward
0 new messages