#!/usr/bin/env perl

use v5.10;
use strict;
# use warnings;

require App::paperback;
exit App::paperback->main(@ARGV);

__END__

=head1 NAME

paperback - imposition of PDF pages for signature printing and binding

=head1 SYNOPSIS

B<paperback> B<file.pdf> (produces file-paperback.pdf)

B<paperback> [ option ]

Options:

=over 4

-v      print version and exit (also --version)

-h      print summary help and exit (also --help)

=back

=head1 VERSION

Version 1.30

=head1 DESCRIPTION

This program takes a PDF whose pages are small in size (A6, A5, Quarter Letter,
Half Letter, Quarter Legal, Half Legal and Letter are supported) and produces a new PDF
file, A4 or Letter or Legal or Tabloid or A3 in page size, with the original pages arranged in
the correct order to be printed, folded, cut, and sewn together in signatures.
This transformation is called "imposition".

All pages in the input PDF file will be imposed on a new PDF with
bigger paper size, ready to be duplex-printed, folded and put together
into signatures, according to its original page size. Input PDF is 
always assumed to be composed of vertical pages of the same size.

ISO 216 normalised (international) page sizes:

=over 4

Input page sizes A6 (105 x 148 mm) and A5 (148 x 210 mm) produce an
output page size of A4 (210 x 297 mm). Input page size A4 (210 x 297 mm)
produces an output page size of A3 (297 x 420 mm). Four A6 pages will
be put on each A4 page, two A5 pages will be put on each A4 page, or
two A4 pages will be put on each A3 page. Before that, input pages will
be reordered and reoriented so as to produce a final PDF fit for duplex
'long-edge-flip' printing.

=back

ANSI normalised (US) page sizes:

=over 4

Input page sizes Quarter Letter (4.25 x 5.5 in) and Half Letter (5.5
x 8.5 in) produce a Letter output page size (8.5 x 11 in). Input
page sizes Quarter Legal (4.25 x 7 in) and Half Legal (7 x 8.5 in)
produce a Legal output page size (8.5 x 14 in). Input page size Letter
(8.5 x 11 in) produces a Tabloid output page size (11 x 17 in). Four
Quarter-Letter pages will be put on each Letter page, two Half-Letter
pages will be put on each Letter page, four Quarter-Legal pages will be
put on each Legal page, two Half-Legal pages will be put on each Legal page,
or two Letter pages will be put on each Tabloid page. Before that, input 
pages will be reordered and reoriented so as to produce a final PDF fit for
duplex 'long-edge-flip' printing.

=back

=head1 SCHEMA EXPLANATION

1. Which schema series (4up or 2up) is used depends on input PDF page
size. For page sizes A6 (105 x 148 mm), Quarter Letter (4.25 x 5.5 in)
and Quarter Legal (4.25 x 7 in), a 4up schema is used. For page sizes A5
(148 x 210 mm), Half Letter (5.5 x 8.5 in), Half Legal (7 x 8.5 in),
Letter (8.5 x 11 in), and A4 (210 x 297 mm), a 2up schema is used.

2. What page size is used for the output PDF depends on input size page.
For input pages of size A6 or A5, the output PDF pages are A4 (210 x 297 mm);
for input pages of size Quarter Letter or Half Letter, the output PDF pages
are Letter (8.5 x 11 in); for input pages of size Quarter Legal or Half Legal,
the output PDF pages are Legal (8.5 x 14 in); for input pages of size Letter,
the output PDF pages are Tabloid (11 x 17 in); for input pages of size A4,
the output PDF pages are A3 (297 x 420 mm).

3. How many signatures are produced depends on the amount of input pages
to be processed. Recto-verso (or duplex) printing is always assumed. Maximum
number of pages in a signature is 16 for all schemas, but a signature is
composed of fewer pages when the input PDF is below 16 pages.

Rearranging of pages is made according the following rules.

=head2 4UP SCHEMA FOR 13 INPUT PAGES OR MORE

       RECTO              VERSO
  +------+------+    +------+------+
  |      |      |    |      |      |
  |  16  |   1  |    |   2  |  15  |
  |      |      |    |      |      |
  +------+------+    +------+------+ <- cut here
  |      |      |    |      |      |
  |  13  |   4  |    |   3  |  14  | <- row upside down
  |      |      |    |      |      |
  +------+------+    +------+------+

  +------+------+    +------+------+
  |      |      |    |      |      |
  |  12  |   5  |    |   6  |  11  |
  |      |      |    |      |      |
  +------+------+    +------+------+ <- cut here
  |      |      |    |      |      |
  |   9  |   8  |    |   7  |  10  | <- row upside down
  |      |      |    |      |      |
  +------+------+    +------+------+

To complete the block of 16 logical pages, blank pages are inserted if
needed. Beyond page 16, a new signature is started, with pages imposed
this same way (these page numbers + 16, these page numbers + 32, etc).

=head2 4UP SCHEMA FOR 9 TO 12 INPUT PAGES

       RECTO              VERSO
  +------+------+    +------+------+
  |      |      |    |      |      |
  |  12  |   1  |    |   2  |  11  |
  |      |      |    |      |      |
  +------+------+    +------+------+ <- cut here
  |      |      |    |      |      |
  |   9  |   4  |    |   3  |  10  | <- row upside down
  |      |      |    |      |      |
  +------+------+    +------+------+

  +------+------+    +------+------+
  |      |      |    |      |      |
  |   6  |   7  |    |   8  |   5  |
  |      |      |    |      |      |
  +------+------+    +------+------+ <- cut here
  |      |      |    |      |      |
  |  --  |  --  |    |  --  |  --  | <- row entirely blank
  |      |      |    |      |      |
  +------+------+    +------+------+

=head2 4UP SCHEMA FOR 5 TO 8 INPUT PAGES

       RECTO              VERSO
  +------+------+    +------+------+
  |      |      |    |      |      |
  |   8  |   1  |    |   2  |   7  |
  |      |      |    |      |      |
  +------+------+    +------+------+ <- cut here
  |      |      |    |      |      |
  |   5  |   4  |    |   3  |   6  | <- row upside down
  |      |      |    |      |      |
  +------+------+    +------+------+

=head2 4UP SCHEMA FOR 1 TO 4 INPUT PAGES

       RECTO              VERSO
  +------+------+    +------+------+
  |      |      |    |      |      |
  |   4  |   1  |    |   2  |   3  |
  |      |      |    |      |      |
  +------+------+    +------+------+ <- cut here
  |      |      |    |      |      |
  |  --  |  --  |    |  --  |  --  | <- row entirely blank
  |      |      |    |      |      |
  +------+------+    +------+------+

=head2 2UP SCHEMA FOR 13 INPUT PAGES OR MORE

       RECTO              VERSO
  +-------------+    +-------------+
  |             |    |             |
  |      1      |    |      2      |
  |             |    |             |
  +-------------+    +-------------+ <- fold here
  |             |    |             |
  |     16      |    |     15      |
  |             |    |             |
  +-------------+    +-------------+

  +-------------+    +-------------+
  |             |    |             |
  |      3      |    |      4      |
  |             |    |             |
  +-------------+    +-------------+ <- fold here
  |             |    |             |
  |     14      |    |     13      |
  |             |    |             |
  +-------------+    +-------------+

  +-------------+    +-------------+
  |             |    |             |
  |      5      |    |      6      |
  |             |    |             |
  +-------------+    +-------------+ <- fold here
  |             |    |             |
  |     12      |    |     11      |
  |             |    |             |
  +-------------+    +-------------+

  +-------------+    +-------------+
  |             |    |             |
  |      7      |    |      8      |
  |             |    |             |
  +-------------+    +-------------+ <- fold here
  |             |    |             |
  |     10      |    |      9      |
  |             |    |             |
  +-------------+    +-------------+

To complete the block of 16 logical pages, blank pages are inserted if
needed. Beyond page 16, a new signature is started, with pages imposed
this same way (these page numbers + 16, these page numbers + 32, etc).

=head2 2UP SCHEMA FOR 9 TO 12 INPUT PAGES

       RECTO              VERSO
  +-------------+    +-------------+
  |             |    |             |
  |      1      |    |      2      |
  |             |    |             |
  +-------------+    +-------------+ <- fold here
  |             |    |             |
  |     12      |    |     11      |
  |             |    |             |
  +-------------+    +-------------+

  +-------------+    +-------------+
  |             |    |             |
  |      3      |    |      4      |
  |             |    |             |
  +-------------+    +-------------+ <- fold here
  |             |    |             |
  |     10      |    |      9      |
  |             |    |             |
  +-------------+    +-------------+

  +-------------+    +-------------+
  |             |    |             |
  |      5      |    |      6      |
  |             |    |             |
  +-------------+    +-------------+ <- fold here
  |             |    |             |
  |      8      |    |      7      |
  |             |    |             |
  +-------------+    +-------------+


=head2 2UP SCHEMA FOR 5 TO 8 INPUT PAGES

       RECTO              VERSO
  +-------------+    +-------------+
  |             |    |             |
  |      1      |    |      2      |
  |             |    |             |
  +-------------+    +-------------+ <- fold here
  |             |    |             |
  |      8      |    |      7      |
  |             |    |             |
  +-------------+    +-------------+

  +-------------+    +-------------+
  |             |    |             |
  |      3      |    |      4      |
  |             |    |             |
  +-------------+    +-------------+ <- fold here
  |             |    |             |
  |      6      |    |      5      |
  |             |    |             |
  +-------------+    +-------------+


=head2 2UP SCHEMA FOR 1 TO 4 INPUT PAGES

       RECTO              VERSO
  +-------------+    +-------------+
  |             |    |             |
  |      1      |    |      2      |
  |             |    |             |
  +-------------+    +-------------+ <- fold here
  |             |    |             |
  |      4      |    |      3      |
  |             |    |             |
  +-------------+    +-------------+

=head1 LIMITATIONS

Input page sizes allowed are A4, A5, A6, Letter, Half Letter, Quarter
Letter, Half Legal, Quarter Legal and Letter. Other sizes give an error message.

Only PDF v1.4 is supported as input, although many higher-labeled
PDF files are correctly handled since they are essentially v1.4 PDF
files stamped as something more modern.

Work has started to add correct processing of v1.5+ PDF files.

=head1 INSTALL

$ B<cpanm App::paperback> (or B<sudo cpanm App::paperback>)

=head1 UNINSTALL

$ B<cpanm --uninstall App::paperback> (or B<sudo cpanm --uninstall App::paperback>)

=head1 AUTHOR

Hector M. Monacci

=head1 LICENSE

Copyright 2022 - Hector M. Monacci.

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 SEE ALSO

L<App::paperback>

L<https://github.com/hectormonacci/perl-app-paperback>

L<https://en.wikipedia.org/wiki/Imposition>

=cut

