Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get a web page header/footer printed on every page?

Based on my research, it seems that what I want to do is not possible, but in case something has changed, I wanted to check to see if anyone had come up with a way to do this.

I have a web app that generates reports for print based on user selections in the browser window. I have a custom header and footer that, when the report is printed from the browser, should be repeated on every printed page. It is not the browser header and footer I need, but rather the custom ones that I generate. Also, I don't think this is an issue of CSS and media types, but I am not a CSS expert. I have no issues getting the header and footer to print once, but I can't get them to print on each page. I have read that perhaps if I recreated my report pages using tables, and then used table head tags and CSS, that may work at least to get the header on each page. I have not had success with this yet, but I will try it again if it is the only option. A coworker suggested that I count lines in my php and manually put out the header and footer as required. I guess that is an option, but it just seems like there should be a way to do this that isn't so "brute force"!

The other caveat is that I have to support IE 6, so I suspect some of the CSS things I have tried are just not supported.

If anyone knows any way to do this, that would be great! If there isn't, I will have to rethink my approach.

Thanks in advance!

UPDATE (14 Dec 2011)

I made considerable progress with this issue, and using some of the info from the answers, I did produce reports that were usable, but never as nice or as professional as I wanted. Footers would tend to be not close enough to the bottom of the page, I had to do a lot of guess work and "brittle" calculations about how big text was going to be to decide on inserting page breaks, I could only support a restricted set of page formats, and any changes to the reports resulted in a cascade of code changes and yet more brittle calculations. There was always a scenario that broke some part of some report. We revisted the requirements, and are now generating reports as PDFs using TCPDF. The documentation is a bit opaque, and it takes some experimentation, but the results are far superior and now the reports appear as they should. I would say to anyone trying to do HTML reports from the browser, unless they are very simple, save yourself the frustration (as others told me here) and go with PDFs or something similar.

like image 743
Carvell Fenton Avatar asked Nov 12 '09 14:11

Carvell Fenton


People also ask

How do I set up headers and footers to print on all pages?

In the File tab, click Print, click Page Setup, and then click the Legend tab. Under Legend on, click Every page, Legend page, or None. See Customize printing of a legend or title for more information. The header and footer that you set will appear on every page.


2 Answers

It can be done with tables -- and I know I'm going to risk a downvote by suggesting using tables for layout - but we are talking IE6 here which isn't known for its fantastic CSS support :-)

If you set a CSS style as follows:

thead { display: table-header-group; } tfoot { display: table-footer-group; } 

Then when you create your HTML, render your body as:

<body> <table>    <thead><tr><td>Your header goes here</td></tr></thead>    <tfoot><tr><td>Your footer goes here</td></tr></tfoot>    <tbody>      <tr><td>      Page body in here -- as long as it needs to be      </td></tr>    </tbody> </table> </body> 

Yes it's not good (tables vs CSS), it's not ideal, but (importantly for you) it does work on IE6. I can't comment on Firefox as I've not tested it there, but it should do the job. This will also handle differnt sized pages, differing font sizes, etc. so it should "just work".

If you want the header and footer to appear on printed media only, then use the @media parameters to do the right thing:

@media print {     thead { display: table-header-group; }     tfoot { display: table-footer-group; } } @media screen {     thead { display: none; }     tfoot { display: none; } } 

Note
As of July 2015, this will still only work in Firefox and IE. Webkit-based browsers (cf. Chrome, Safari) have long standing bugs in their issue trackers about this if anyone feels strongly enough to vote on them:

The comments below this question tell me this is now resolved in Chrome. I haven't checked myself :-)

The original bugs against Chrome (for reference) are:

  • https://bugs.webkit.org/show_bug.cgi?id=17205
  • https://code.google.com/p/chromium/issues/detail?id=24826
  • https://code.google.com/p/chromium/issues/detail?id=99124
like image 191
Chris J Avatar answered Sep 20 '22 02:09

Chris J


This will work in some browsers, not not all. I don't think there is an elegant cross-browser solution

Include the print footer/header you want in divs on the page (in this example div id='printableFooter')

In the screen css file put:

#printableFooter {display: none;} 

In the print css file:

#printableFooter {display: block; position: fixed; bottom: 0;} 
like image 41
UpTheCreek Avatar answered Sep 24 '22 02:09

UpTheCreek