1

I'm building a webapp with CakePHP (not really germane, but anyway) whose mission is, among other things, to produce a single web page containing a bunch of content that's stored in the database (a collection of articles). I need to be able to print this document and include a simple header and footer for each page when the document is printed.

Additionally, I have no way of guaranteeing that the rendered length of a single item in the collection of articles will be less than that of a piece of paper, so I won't be able to bookend each article like so:

<?php echo $header; ?>
<?php echo $article; // etc. ?>
<?php echo $footer; ?>

The plus side here is that I'm going to be the only person printing this (and saving it as a PDF, actually), so any proposed solution won't be required to work with any arcane browsers that still happen to be around (IE6, etc.).

(Before you mark this question as a duplicate of this similar question, note that I'm not trying to work with IE6 and the individual bits of content I'm printing may well exceed a single printed page in length. As such, the table-based solution to that problem is not applicable here -- at least, as far as I can tell.)

If this is going to require me to do some funky arithmetic to break up a large contiguous block of content into approximately-sized "pages" (which would be delimited by the footer+pagebreak+header combination, obviously), I'm fine with that provided there isn't a more sane way to go about it.

Thank you in advance - please comment if you feel I could provide more information or otherwise make this situation easier to resolve.

Community
  • 1
  • 1
brettkelly
  • 27,655
  • 8
  • 56
  • 72

2 Answers2

0

try using FPDF, which has header() and footer() as part of the framework.. http://www.fpdf.org/

that would handle your page-breaking as well as your header/footer was well as your PDF generation...

alternately to fpdf, if your content is reliably broken into page-size blocks, use css...

div.footer{
   page-break-after:always
}
FatherStorm
  • 7,133
  • 1
  • 21
  • 27
  • Thank you for the response! If at all possible, I'm looking to avoid generating the PDF using code. Reason being, I'm going to need to do a non-trivial amount of styling and layout to the resulting document and, in the bit of reading I've done, the FPDF API seems to make that sort of thing rather... cumbersome. – brettkelly Mar 30 '11 at 21:40
0

To handle the PDF side, use wkhtmltopdf - besides being fantastic in general for generating PDFs, there's solid support for a header and footer.

Take a look at the "Headers And Footer Options" section here in the manual: http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html

Of course, this presumes that you can run custom executables on your server.

thirtydot
  • 224,678
  • 48
  • 389
  • 349