README.md

Brochure printing

Printing a booklet on a printer that does not support this function.

Not all printers (mine, for example) support the very handy “booklet printing” feature, but many can do the following:

  • print 2 pages per sheet;
  • understand which pages and in what order to print.

That. The required function can be reduced to the following sequence of actions:

  • calculation of numbers and order of internal pages; their printing and stop;
  • manual overturning of already printed pages so that further printing occurs on the reverse (clean) side of the sheets;
  • calculation of numbers and order of external pages; their printing and stop;
  • it remains only to bend the sheets in the middle and somehow fix them (if there is a suitable stapler and the like).

First, since four pages can fit on a sheet in this way (two on one side, two on the back), you need to make sure that the number of pages is a multiple of 4, and, if this is not the case, supplement the document with blank sheets to the desired number. For example, there are 9 pages in a document, which means you need to add 3 blank pages to the end of the document.

The selection of internal pages is carried out as follows:

  • the first even page is displayed on the first sheet (No. 2 for the document from the example above) and the last odd one (No. 11): {2, 11};
  • further the number of the first page is increased by 2, and the second one is decreased by 2; until they differ by 1: {4, 9}, {6, 7};
  • thus we get a sequence of pages: 2, 11, 4, 9, 6, 7.

Selection of external pages - by analogy with internal ones, only the first pair will be {12, 1}, and all other operations are performed symmetrically::

const pagesCount = 12;
const pagesInside = [];
const pagesOutside = [];

let pageLeft = 2;
let pageRight = 11;

do {
    pagesInside.push(pageLeft, pageRight);
    pagesOutside.unshift(pageRight + 1, pageLeft - 1);
    pageLeft += 2;
    pageRight -= 2;
} while (pageRight - pageLeft >= 1);

alert(`
    Inside: ${pagesInside.join(', ')}
    Outside: ${pagesOutside.join(', ')}
`);

screenshot-01

Usage

use Jzucen\BrochurePrint\BrochurePrint;

// Optional: Define your own cache folder path.
define('JZUCEN_BROCHURE_PRINT_CACHE', '/path_to_cache');

// Open a document. The file name will be determined automatically. Otherwise, specify manually as the second argument.
$jbp = BrochurePrint::byPath('/path_to_document/test.pdf');

// Output all data from DocumentInfo (see its getters):
var_export($jbp->getData());

// or, for example, output the actual document with blank pages at the end:
header('Content-Type: application/pdf');
echo $jbp->getData()->getDocumentData();
exit();

screenshot-02

Конвейеры
0 успешных
0 с ошибкой