views:

55

answers:

2

Is there a technique for adding a text footer the bottom of each page when it is printed? For example "Copyright My Company 2010" - I know there is probably a way to do this with a background image using CSS, but I would really like to use text for this portion so the client can edit it. Any ideas?

+3  A: 

You can have this in your normal footer and hide it in your regular CSS. For printing, include a separate print stylesheet that displays the footer.

deceze
This doesn't actually answer the question of how to put a footer on "the bottom of each page" as asked.
Andrew Vit
@Andrew Then I have misinterpreted the word "each", or rather thought about the wrong kind of "page".
deceze
+3  A: 

CSS doesn't have any notion of page media, so it's going to be impossible to guarantee where the page breaks are going to occur naturally.

EDIT As pointed out below, CSS 2.1 did introduce @page as a way to deal with paged media, but it was never implemented across the common browsers. So, as I wrote above, it doesn't exist, although that's not technically true.

You can set hard page breaks, e.g. by placing a <div class="page-break"> at the approximate locations. You can then style it with page-break-before:always to ensure that a break happens there.

There's also a page-break-after property; but then you don't know how far down the page the element starts. So, when you need to position it, the only thing you can use is position:absolute;bottom:0 which wouldn't fix it to the page media, but to the bottom of the whole document.

If you use page-break-before then you know it always appears at the top of the page. Then, you can use position:absolute without giving a top or bottom, which results in only taking it out of the document flow. Then, giving it a height of 720pt (10 inches) means you have a bottom edge that you can position content against.

Here's how I would tackle it:

/* hide the page footer on screen display */
.page-break { display: none; }

@media print {
  /* make a 10-inch high block that sits on top of the page content */
  .page-break {
    page-break-before: always;
    display: block;
    position: absolute;
    height: 720pt;
  }

  /* stick the contents of .page-break to the bottom of the 10 inch block */
  .page-break .copyright {
    position: absolute;
    bottom: 0px;
  }
}

However, I have no idea how well browsers actually support this in reality. I remember playing with page breaks a while back, and ended up giving up because I couldn't get them to work reliably enough. I suspect it's still either impossible or very hackish and unreliable.

Andrew Vit
CSS doesn't have any notion of paged media?! Umm… It has a whole chapter on the subject! http://www.w3.org/TR/CSS21/page.html
David Dorward
@David, I should clarify, I don't think it was supported anywhere when I last tried it...
Andrew Vit
Im creating these docs in HTML/CSS but I am converting them to PDF using a ruby gem, is there a way to pass on this info directly to the PDF? That is, is there another, more reliable technique for documents that are getting converted to PDF?
Thomas
@Thomas, whatever method you're using to convert your HTML to PDF, I assume it goes through some engine, most likely WebKit. You're going to be subject to the same limitations whether you "print" your page from ruby or from the browser. If you really need fine control over page layout, you'll have to look into something like Prawn or PDFlib to build your pages programatically.
Andrew Vit