I have read through all relevant posts on Prawn but found no mentioning (even in Prawn's own documentation) of headers and footers.
However, I did see a demo on Prawnto's own website about headers and footers. I copied the entire source of that demo just to see if it works but an error of undefined method "header" is complained about. Am I to understand that Prawn took out header and footer recently in the gem or is there something else I need to do first to use header and footer?
The demo page: http://cracklabs.com/prawnto/code/prawn_demos/source/text/flowing_text_with_header_and_footer
the part of code of concern:
Prawn::Document.generate("flow_with_headers_and_footers.pdf") do
header margin_box.top_left do
text "Here's My Fancy Header", :size => 25, :align => :center
end
text "hello world!"
end
And by header, just in case, I mean the snippets of words that appear usually at a corner of every page of a document. Like your account number in your bills pages.
thanks!
The only way I've found to get a repeating item on a page is to use the
Prawn::Document::LazyBoundingBox
method. Basically this allows you to define a bounding box that is only rendered once you call it. So the usualpseudo-code
steps areThe example from the source shows how
end
This gives you a Page-count text output on each new page. What would be ideal is if this could be applied in the setup of a page template that is reused without the manual call to render the element. The combined with text flow it would give the traditional solution of headers and footers.
START EDIT
This works in prawn >= 0.12
END EDIT
Here is my solution using repeat, canvas and cell. Essentially I'm drawing my bounding boxes at the absolute top and bottom of every page. I'm using cell to have better styling control over it. Hope this is going to be helpful to someone. ( I used slightly annoying colors to better illustrate how you can control styling of header and footer)
here's the problem when using the
bounding_box
for creating the custom footer contents... it is still rendering within the bounds of a margin.I was looking for something that will write the contents in the margin area together with
number_pages
.(because a footer usually is set in the bottom margin area)... and it seems that there were none.so instead, I used
text_box
and place the coordinates outside my main bounding box like so:take note that the
repeat :all
, will render this footer text to every page.It's little bit different with latest version of Prawn you must passe an hash
@GrantSayer thx for the example, but this will only let you show the current page number, not the total number of pages.
You can also use the number_pages function for the footer:
However, in my case I also need to format/style and right align the page numbers to match company style guides. I used go_to_page(k) to create my own header and footer functions, which add the header and footer to each page after all the pages are created. This gives me both styling options and the total number of pages:
If you want a footer that do not write stuff over your text, you have to create the
bounding_box
below the margin of the document usingbounds.bottom
.It should look like that, you can see that the footer is outside the margin bottom :
Hope it help someone