Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating page-break locations dynamically

Status
Not open for further replies.
Aug 27, 2001
502
US
I am using PHP to dynamically generate pages with content garnered from a database. The pages contain tables which I would like to keep from being split on a page break. However, I'd like more than one table to print on a page if it will fit.

I can use the 'page-break-before' or 'page-break-after' CSS tags to accomplish this, however, I need to be able to calculate where to place these tags. Can anyone help me out here? How can I calculate where the page breaks should be inserted?

One caveat is that I am also using JavaScript to allow the user to "collapse" certain sections of tables if they do not wish to clutter their screen with that info (I use object.style.display="none") to do this. As a result, the page break locations can change depending on which tables are visible. So, I'd like to use the "onbeforeprint" event to trigger JS code that will calculate page breaks.

Any help here would be GREATLY appreciated.

-Ron

P.S. Of course, this post would not be necessary if IE supported the "page-break-inside:avoid;" CSS standard!
 
I recommend that you load all of the table rows into an array:

allRows = document.getElementsByTagName("tr")

and then look at each row to see if it's displayed and has the same parent node as the previous row... count how many rows can fit onto a page and insert your page breaks where neeeded. A little tedious, but it should work. Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
 
On second thought, you can get all of the tables into an array and see how many rows are displayed in each table (remember that the firstChild of a table node is always a tBody node even though you don't write it in your code).

Once you've figured the number of displayed rows in each table, figure out how many tables fit on a page and put your page break in accordingly. Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
 
Okay, but some rows will contain more text (remember that the content is dynamic) & therefore will be bigger than others. How do I determine the amount of space that any given row takes up?

-Ron
 
hmmm - haven't tried it, but even the table itself may have a height property that is accurate (fat chance), or the row height may be available. Each node has about 100 attributes... Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
 
We investigated that for about a month.

First, when your generating the HTML on the server you don't have access to DOM information. That only exsits in the browser. So forget about getting row height etc.

So basically you can use pixel arithmetic if you use CSS to control everything at the pixel level in your layout. Then of course you can only get information about the users screen resolution and the Browser window height/width not the printer. So what will you do with pixel arithmetic?

If you break your data into single line rows you can count table rows and use a "estimated" target. That's about as close as we got to making anything work.

Bottom line is if you need more specific printing capability you need to look at a different technology like FOP or VML or a desktop application etc.

good luck
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top