I don't know what the scope of this is, but
https://pagedjs.org/ is Javascript that does pagination and page margin styling. It's essentially a polyfill for CSS Paged Media:
https://www.w3.org/TR/css-page-3/Pretty nice to work with, if you can run JS. (The rest is just Puppeteer to print. Though I couldn't use their command line tool, because it force-injects paged.js, and it didn't play well with the Preact components for previewing I had made.)