Yee Wei Law's Homepage

Recent site activity

Printing an A5-sized book on A4 papers

To me, reading A4-sized papers/books while commuting is cumbersome. I prefer A5-sized documents, but A5 papers are not widely available -- in fact I haven't seen any (paper sizes are nicely explained here). This is why I propose
  1. printing documents on A4 papers, with 2 pages on 1 side of a paper, or in other words, a total of 4 pages on 2 sides of one sheet of paper,
  2. cutting the papers in half,
  3. interleaving the papers,
  4. and finally binding the papers.
To achieve this, we need to print the pages in this order: 1,3,4,2,5,7,8,9,... or 1,4,3,2,5,8,7,9,.... The following Perl script re-orders a PDF document according to the first scheme, using Perl module PDF::API2 and the LaTeX program texexec.

#!/usr/bin/perl

use strict;
use warnings;

use PDF::API2;

undef $/;

if ($#ARGV < 1) {
print STDERR "Usage: $0 {input filename} {output filename}\n";
exit 0;
}

# open and read file
my ($ifn, $ofn, $pdf, $pages, $i, $order, $cmd);

$ifn = $ARGV[0];
$ofn = $ARGV[1];
$pdf = PDF::API2->open($ifn);
$pages = $pdf->pages;

print STDERR "Number of pages = $pages\n";

# order = 1,3,4,2,...
$i = 1;
while ($i <= $pages) {
if ($i+1 > $pages) {
$order .= $i;
} elsif ($i+2 > $pages) {
$order .= $i.','.($i+1).','.$i.','.($i+1).',';
} elsif ($i+3 > $pages) {
$order .= $i.','.($i+2).','.($i+2).','.($i+1).',';
} else {
$order .= $i.','.($i+2).','.($i+3).','.($i+1).',';
}
$i += 4;
}
if (rindex($order, ",") eq length($order)-1) {
$order = substr($order, 0, length($order)-1);
}

# exec cmd
$cmd = "texexec --pdfselect --selection=$order \"$ifn\" --result=\"$ofn\"";
print STDERR $cmd;
system $cmd;
The following picture shows the result: