I'm developing a document editor using Tauri, and I'm trying to implement automatic pagination like in Word or Google Docs. The editor renders content with HTML and CSS, and I want the content to automatically move to the next page when it exceeds the page height. I need to ensure that manual page breaks are respected, and the document looks the same when exported as it does on-screen. I've already tried measuring content height, using CSS page breaks, manual splitting, and height-based splitting, but I keep running into edge cases and issues during export. I have a repository of my current implementation [here](https://github.com/RKG765/OpenWriter). I'm looking for guidance on how to approach pagination correctly, layout calculation strategies, and common mistakes to avoid. Any help would be greatly appreciated!
3 Answers
I’ve seen a lot of tools struggle with ensuring the exported document matches what’s rendered on-screen. It's a tough task, even if you get the measurements right. I found some interesting discussions on this topic that you might find helpful: [TUGboat Article](https://www.tug.org/tugboat/tb21-3/tb68fine.pdf) and the [Widows and Orphans Wikipedia page](https://en.wikipedia.org/wiki/Widows_and_orphans).
Have you looked at how other word processors handle pagination? A good example to check out is Collabora Online, which has its code available under the Mozilla Public License 2.0. That might give you some insights!
I think a simple approach could be to count the number of lines on the page. If it's over a certain limit, you create a new page. You could also factor in font size scaling to make it work smoothly. This might work well for your minimum viable product!

But what about handling images and LaTeX typesetting? That could complicate things, right?