#!/usr/bin/env php
<?php
/**
* Generate the page order for building a PDF of A4 pages printed as an A5 Booklet form on a MacBook that is
* missing the proper A5 Booklet form.
*
* @return int[]
*/
function getPageList(int $pageCount): array
{
// Generate a list of page numbers, including additional blank pages required to get the total number of page
// divisible by 4 to represent the list of A4 pages to be printed in as an A5 Booklet form.
$pages = $pageCount % 4 !== 0
?
array_merge(
range(1, $pageCount),
array_fill(
$pageCount + 1,
4 - ($pageCount % 4),
'blank'
)
)
: range(1, $pageCount);
$results = [];
// Extract the pages to be printed in the relevant order for an A5 Booklet form.
while (count($pages) !== 0) {
// The page order is last page, first page, first + 1, last - 1.
// As we remove these pages from $pages, we can repeat this extraction whilst there are no pages to left.
$results[1 + count($results)] = [
'Left front' => array_pop($pages),
'Right front' => array_shift($pages),
'Right back' => array_shift($pages),
'Left back' => array_pop($pages),
];
}
return $results;
}
print_r(getPageList($argv[1]));