Break large unbreakable boxes early when we have to break them anyway
Hello,
I am trying to use the CSS property break-inside to avoid splitting a div into two pages.
I get an issue when the height of the div is bigger than the height of a page. In this case, it's impossible to avoid a page break. Weasyprint starts displaying the div after breaking a page. Since the div starts on a fresh new page, this new page is not enough to contain the content on the div. Then a page break occurs and a third page is used.
What I am expecting, is that the pdf only uses 2 pages and avoids breaking the page before displaying the div.
It's not that easy to explain, so here is a light example showing my issue: https://gist.github.com/gg0512/d99ddd0fa3dc7fe9e3df9045075855a5
Here is the pdf that I am getting: break_inside_avoid.pdf
Here is the pdf that I am expecting: break_inside_avoid_expecting.pdf Here is the pdf that I am expecting if the height of the div is not bigger that the height of a page (this works): break_inside_avoid_small_expecting.pdf
Of course, the height of the div depends on inputs, sometimes it's bigger than a page, and sometimes not.
Do you have any idea of how to handle that?
Thank you in advance,
gg0512
WeasyPrint version 52.1
Hi!
That would definitely be an improvement, even if I think that it’s not really specified (and it’s pretty hard to implement, I think). Splitting a break-inside: avoid block early when we know that it will be split on the next page is definitely a good idea.
I’ve already used a workaround that works for simple cases like lists. Instead of using ul { break-inside: avoid }, I’ve used li { break-after: avoid } li:nth-child(n+7) { break-after: auto }, allowing lists to be split only when they have more than 7 items. Of course, that’s not a real solution…