Beginner

Although pdfkit docs make it seem like you always have to be the one to add a new page via doc.addPage() but that is not the case!

It actually does a good job of automatically letting the content overflow into the next page.

Here’s a gist that you can copy/paste into pdfkit’s interactive browser demo to see everything working perfectly without calling doc.addPage().

Advanced

In mature projects this little secret can get lost and folks often don’t see the behavior I’m describing because of a small gotcha:

Any (x,y) coordinate changes you make: doc.text('line1',x,y) … will dictate the location of the next line, if its output without specifics: doc.text('line2')

Why would you even output a line without specifics? To take advantage of (a) automatic layout of one line after the other and (b) the overflow into the next page, ofcourse!

What happens in mature projects is that some folks optimize PDF generation by adding footers on the pageAdded event and this means that it changes the Y position to the bottom of the page and then starts printing the content for rest of the page which works fine when specific coordinates are given but not otherwise. So people start thinking that pdfkit doesn’t do page flow well but that’s not the case at all !!!

Sidebar: You should play around with moveUp and moveDown and completely grasp how they work because once you understand them, they can make all your coordinate calculations go away while still using pdfkit’s auto y-layout/overflow abilities.

References

Found out that James O’Cull was onto the same idea, way before me: https://github.com/foliojs/pdfkit/issues/539#issuecomment-255170981

Written with StackEdit.