(clockwise from top left): imposition from a single-page PDF into a booklet, anatomy of a book, a spread

PDF imposition[edit]

Premise: I will write my own script(s) to impose pages from a source PDF:

Impose 01.jpg

The aim is to write a Python script to automate the process by iterating recursively over pages in a PDF. These will be called in Python as subprocesses using the subprocess module.

PDF imposition from scratch[edit]

23.09.19
I'm working out which commands to use from the command line. The process should be as follows:

   #burst the source PDF, keep the name of the source plus page number (-%d)
   $ pdftk source.pdf burst output source-%d.pdf

Results:
Burst 01.png Burst 02.png

To identify the size of a single burst page in pixels, this is the command I used:

   $ identify Carrier_burst-1.pdf 

which returns this information:

   Carrier_burst-1.pdf PDF 398x591 398x591+0+0 16-bit sRGB 19486B 0.000u 0:00.009

26.09.19
Then for the next step, resizing each page to fit 2-up on an A4. It's easy enough to reduce to 50% if the original is A4, but in other formats it's a bit trickier...

   #resize the resulting burst single page PDF (this only does one - needs Python loop to iterate recursively)
   $ magick mogrify -resize 50% new_source.pdf
   #or to resize to specific pixel dimensions such as 256x256
   $ magick mogrify -resize 256x256 new_source.pdf

After resizing to 50%, the identify command returns these values:

    Carrier_burst-1.pdf PDF 199x296 199x296+0+0 16-bit sRGB 51910B 0.000u 0:00.000

27.09.19
The next steps are these:

   #impose 2up on a page using imagemagick montage command
   #assemble imposed PDFs into a single file

Using pdfimpose[edit]

pdfimpose is a python library that does imposition. It's quite easy and powerful, though again it is not quite set up for cutting pages. I found some Python code, which creates an imposed PDF in a 2x2 format, with folds to be made first vertically, then horizontally:

   from pdfimpose import impose, VERTICAL, HORIZONTAL
   
   impose(inname=["foo.pdf"],outname="foo-impose.pdf",fold=[VERTICAL, HORIZONTAL],bind="left",last=0,)

The resulting layout for the PDF is like so:

E r 2x2 imposed.png

Documentation for pdfimpose can be found here:

https://buildmedia.readthedocs.org/media/pdf/pdfimpose/latest/pdfimpose.pdf

booklet.sh for PDF imposition into a booklet[edit]

I found another way to do imposition using a script Michael wrote called booklet.sh, downloadable here: https://git.xpub.nl/murtaugh/95layouts
It's a shell script that can run in the bin folder from the home directory, which allows you to run it wherever you are in the computer - you don't have to cd to the folder where the script is. To enable this, first you have to echo the path for the bin folder that the scripts are in. The command to run it from the terminal is:

booklet.sh source.pdf

It needs a few dependencies to run (mainly psutils).

  1. Requirements: psutils, pdftk, python3 with reportlab (make_blank_pdf.py)
input=$1
base=${input%.*}
echo converting $input to $base.booklet.pdf
pdftk_utils.py $input pad --multiple 4 --output $base.01.pdf
pdftops -paper match $base.01.pdf $base.01.ps
psbook -s`pdftk_utils.py $base.01.pdf count` $base.01.ps $base.02.ps
psnup -2 -PA4 $base.02.ps $base.03.ps
ps2pdf $base.03.ps $base.booklet.pdf
rm $base.01.pdf $base.01.ps $base.02.ps $base.03.ps

Note: Comment out or delete the last line to produce three .ps files - the first one is the source PDF, the second is the PDF imposed in the correct order, the third one is the imposed PDF pages on an A4 page. This works well if all you want to do is impose, print and fold. For cutting sheets you need to position the pages 2-up, centred on the page, which I can't quite figure out how to do...