Forum How do I...?

How to Force Page Break Before Last Sentence of Page?

herold
How can I create a page break before the last sentence of a page in order to avoid that the last sentence of a page it broken over to the next page?

See demonstration below:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. In laoreet dolor et lectus lobor- tis viverra. Etiam blandit mollis commodo. Vivamus eu ultricies odio. Phasellus at magna mi, eu mattis turpis. Sed leo nisl, auctor sit amet euismod vitae, porttitor in tor- tor. Praesent eu quam in felis viverra vestibulum. Aliquam in neque nisl. Maecenas dapibus, lorem sed congue mollis, tellus nisl laoreet metus, vel pharetra elit sapien sit

--- page break---

amet arcu. In tincidunt purus nec turpis rhoncus posuere. Nullam ligula nisi, feugiat non malesuada ac, placerat in velit. Ut quam turpis, sollicitudin tristique sollicitudin a, condimentum vel leo. Nunc adipiscing nisl at enim hendrerit vulputate.
mikeday
You can force a page break at particular points using the page-break-before and page-break-after properties, and you can use the widows and orphans properties to prevent leaving a few dangling lines at the beginning or end of a page. But these properties only apply to physical lines, not sentences.
herold
@mikeday

Thank you. These CSS rules are already in place and work fine for all my paragraphs, however it still leaves a few pages with sentences cut. If I widen the rules I end up having too much left space on the end of a page.

Any ideas?
mikeday
I don't know if anything can be done about this. Prince does not identify sentence boundaries (although you could add markup at runtime using JavaScript, perhaps?) and sentences can be very long or very short, so coming up with a consistent breaking policy seems difficult. It seems easier to rewrite your document as a list of bullet points, then you could say:
li:last-of-type { page-break-inside: avoid }
herold
Interesting idea! No idea how to do this? Would this change the way my document is displayed? I assume that these changes are internal and don't show up.
mikeday
Yes, basically the sentence would have to begin on a new line, it can't begin halfway along a line if you want to make the sentence not break. Or to put it more technically, the sentence needs to be a separate block, so you can apply "page-break-inside: avoid" to it.
herold
I see. Meaning I really can't have a flowing paragraph any longer?
mikeday
Right. Page break control doesn't go down to the level of individual inline spans yet.
herold
Thank you Mike! Guess I have to live with it.