headers in a multidocument context

17 views
Skip to first unread message

jaambros

unread,
Nov 21, 2009, 6:07:57 PM11/21/09
to Prawn
Hi all,
I'm trying to use prawn in my Rails app using prawnto.
I want to produce a series of student reports, where each report has a
student heading and a (potentially multipage) body;
I tried (in my file.pdf.prawn)
@students.each do |s|
header( pdf, s )
body( pdf, s )
end
where header uses prawn's header routine (using pdf.text calls) and
body prints the rest of the doc for that student also using pdf.text
calls and which may span more than one page (and I would like to have
the student's header appear in the following pages in case it does).
I expected to obtain a diff heading for each student; but ended up
with the same heading for all (that of the last student).
It would appear that the header is rendered at the end using the last
definition for it.

Is there a way to do what I need?
Say, some method that I can define and that would get called when a
new page is created so I can print the appropriate heading?

Thanks in advance, Jose

Henrik Nyh

unread,
Nov 21, 2009, 7:35:20 PM11/21/09
to prawn...@googlegroups.com
On Sun, Nov 22, 2009 at 00:07, jaambros <jo...@kiubo.net> wrote:
> Hi all,
> I expected to obtain a diff heading for each student; but ended up
> with the same heading for all (that of the last student).
> It would appear that the header is rendered at the end using the last
> definition for it.

Perhaps I misunderstand, but this works as expected for me:

require "rubygems"
require "prawn"
require "prawn/layout"

LOREM = "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum. "

Prawn::Document.new do

3.times do |i|

header([0, bounds.height + 20]) do
text "Student #{i}", :style => :bold
end

text LOREM*20

stroke_horizontal_rule
start_new_page unless i==2

end


render_file '/tmp/test.pdf'
end
`open /tmp/test.pdf` # OS X

jaambros

unread,
Nov 22, 2009, 3:45:40 PM11/22/09
to Prawn
require "rubygems"
require "prawn"
require "prawn/layout"

I guess I wasn't clear enough. Please see below;
I expected to get John's header with John's body. Instead I get Mary's
on both John's and Mary's

Prawn::Document.new do
@students = [{:name=>'John', :body=>'John History=A'},
{:name => 'Mary', :body=>'Mary History=A+' }]

@students.each_with_index do |s,i|
start_new_page unless i==0
header([0, bounds.height + 20]) do
text s[:name], :style => :bold
end
text s[:body]
stroke_horizontal_rule

James Healy

unread,
Nov 22, 2009, 11:19:07 PM11/22/09
to prawn...@googlegroups.com
Two things:

- the attached code is a slightly cleaner version (imho). It uses the
skip_page_creation option to avoid needed each_with_index.
- the attached version seems to render headers correctly for me (on
prawn 0.6.3)

-- James Healy <ji...@deefa.com> Mon, 23 Nov 2009 15:18:47 +1100
> --
>
> You received this message because you are subscribed to the Google Groups "Prawn" group.
> To post to this group, send email to prawn...@googlegroups.com.
> To unsubscribe from this group, send email to prawn-ruby+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/prawn-ruby?hl=.
>
>

James Healy

unread,
Nov 22, 2009, 11:21:37 PM11/22/09
to prawn...@googlegroups.com
James Healy wrote:
> Two things:
>
> - the attached code is a slightly cleaner version (imho). It uses the
> skip_page_creation option to avoid needed each_with_index.
> - the attached version seems to render headers correctly for me (on
> prawn 0.6.3)

And this time with the attachment.

-- James Healy <ji...@deefa.com> Mon, 23 Nov 2009 15:20:53 +1100
students.rb

jaambros

unread,
Nov 23, 2009, 2:38:29 AM11/23/09
to Prawn, jo...@kiubo.net
Hi James,
Thanks for your message and improvement on my program.
However, I've run your example on my MacBook Pro OS X 10.5.8 Ruby
1.8.7 and I'm getting Mary's heading on BOTH pages.
I have
$ gem list prawn

*** LOCAL GEMS ***

prawn (0.6.3)
prawn-core (0.6.3)
prawn-format (0.2.3)
prawn-layout (0.3.2)
prawn-security (0.1.1)

In case it may be a version problem I reinstalled all of these gems
and did a 'gem pristine' on all of the above.
What else can/should I do?

Thanks, Jose

James Healy

unread,
Nov 23, 2009, 8:11:19 AM11/23/09
to prawn...@googlegroups.com
jaambros wrote:
> Thanks for your message and improvement on my program.
> However, I've run your example on my MacBook Pro OS X 10.5.8 Ruby
> 1.8.7 and I'm getting Mary's heading on BOTH pages.

Apologies, I was testing your code against a branch of my code.

Headers and footers are designed to be the same on every page, an
unfortunately weakness of the current version of prawn. In prawn 0.6.3
they are added to every page just before your document is rendered, so
whatever you last set the header/footer to will appear on every page.

-- James Healy <ji...@deefa.com> Tue, 24 Nov 2009 00:10:55 +1100

Gregory Brown

unread,
Nov 23, 2009, 9:21:53 AM11/23/09
to prawn...@googlegroups.com
On Mon, Nov 23, 2009 at 8:11 AM, James Healy <ji...@deefa.com> wrote:
> jaambros wrote:
>> Thanks for your message and improvement on my program.
>> However, I've run your example on my MacBook Pro OS X 10.5.8 Ruby
>> 1.8.7 and I'm getting Mary's heading on BOTH pages.
>
> Apologies, I was testing your code against a branch of my code.
>
> Headers and footers are designed to be the same on every page, an
> unfortunately weakness of the current version of prawn. In prawn 0.6.3
> they are added to every page just before your document is rendered, so
> whatever you last set the header/footer to will appear on every page.

And this eventually *will* change, the question is only when.

-greg

jaambros

unread,
Nov 23, 2009, 1:10:30 PM11/23/09
to Prawn
Hi James and Greg,
Thanks for the explanation as to why I'm getting the same header in
both pages.

In any event, in my application I need to have a "header" (and footer)
that appears on each page that changes every so often.
Is there a way to have some of my code run each time a new_page is
called so that I can insert it myself?

Thanks, Jose

Valera

unread,
Nov 23, 2009, 3:50:18 PM11/23/09
to Prawn
I had the same problem, and here's what I did:
have a page_headers array that stores the headers for each page
then, do this:

page_headers = []

@students.each do |s|
student_start_page = page_count

...write student info...

student_end_page = page_count
page_headers.fill(s[:name], (student_start_page-1)..
(student_end_page-1))
end

page_headers.each_with_index do |head, page|
if head # don't try to write missing headers
go_to_page page
text head, :at => [0, bounds.height + 20] # or figure out
optimal position...
end
end

That worked for me, and I hope it can help you, too.

Valera

unread,
Nov 23, 2009, 3:57:46 PM11/23/09
to Prawn
I'm going to modify the students.rb file that was sent a little bit
ago to demonstrate the code in full.

Valera Trubachev

unread,
Nov 23, 2009, 4:02:10 PM11/23/09
to Prawn
Here's a students.rb file, with the generated PDF. Tested with Acrobat 9 and the headers are correct on every page.

students.rb
test.pdf

jaambros

unread,
Nov 24, 2009, 1:04:14 AM11/24/09
to Prawn
Hi Valera,
Interest approach.
I think I can use it to solve my problem.

THanks for your help, Jose
Reply all
Reply to author
Forward
0 new messages