Skip to content

Instantly share code, notes, and snippets.

@UnrulyJuli3
Last active July 2, 2024 03:19
Show Gist options
  • Save UnrulyJuli3/0881f7b5ac0cba5329de580692ce1d69 to your computer and use it in GitHub Desktop.
Save UnrulyJuli3/0881f7b5ac0cba5329de580692ce1d69 to your computer and use it in GitHub Desktop.
Calculate booklet page printing order
// Pass in the number of pages in your document, e.g. 8
// The output is an array describing how to layout the pages for a booklet
// e.g. [8, 1, 2, 7, 6, 3, 4, 5]
// This means the front of your first sheet of paper
// should have pages 8 and 1 of your document side-by-side,
// then the back should have pages 2 and 7, then the front
// of your second sheet should have pages 6 and 3, etc.
// 0 indicates that a portion should be blank
const createBookletOrder = pages => {
const source = pages,
res = [];
while (pages % 4 !== 0) pages++;
let up = 1,
down = pages;
for (let i = 0; i < pages; i++) {
const x = i % 4,
y = (x === 1 || x === 2) ? up++ : down--;
res.push(y > source ? 0 : y);
}
return res;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment