Forum How do I...?

Change styling on the last element in a page

locilocisu
My document consists of divs that'll go on to multiple pages. However I'd like to style these elements differently if they are the last element in a page. Is there a way to select these elements?
These divs have page-break-inside: avoid.

Example:

div
div
div <-----this one should be styled differently
----page break
div
div <-----this one should be styled differently
----page break
div
div
....

Edited by locilocisu

mikeday
Unfortunately there is no CSS selector that can do this, as it requires performing layout first. (If there was such a selector then you could do sneaky things, like apply "page-break-before: always" to the last div on the page, pushing it to the next page, which means the selector would no longer apply, moving it back to the previous page, causing an infinite loop).

Currently the only way to achieve this is to run Prince twice. The first time you use JavaScript to locate the last divs on each page using the box tracking API and then report their IDs, so you can generate some new CSS that targets those IDs and then run Prince again to generate the final PDF. It's a bit complicated!

There is an example using a similar technique to implement changebars.
locilocisu
Thanks! looks like this is a pursuable path.

However, I'm having difficulty communicating back the result of the box API to the calling process. I am using the C# wrapper to call Prince and seems like there isn't a method that'd allow me to access the Log.data array.

What would be my option to communicate back the result of the box api call?
mikeday
Oops that is an oversight on our part, we need to add a way to collect this information.

In the meantime, perhaps you could call Log.warning instead and prefix the message with a string to identify that it's coming from the script? Slightly ugly approach, but it should work without any problems and you can collect the warnings from the onMessage event handler.
locilocisu
This worked! Thank you for the help.