Create a script to print a PDF booklet (nice for papers)

Hi fellows,

After a considerable time without posts I’m back with a script that I’ve been using to print papers and other documents in book format with two pages side-by-side, which it is being very useful!

Required packages:

  • psutils
  • pdftk
#!/bin/bash

# usage: pdf2book 

# acroread settings: 
# [x] auto rotate and center 
# [x] choose paper source by pdf page size
# [x] use custom paper size when needed
# [x] portrait 

FILE=$(echo $1 | sed 's/.pdf//')

echo "$ pdfcrop $FILE.pdf --margins 12"
pdfcrop $FILE.pdf --margins 12

echo "$ pdftops $FILE-crop.pdf"
pdftops $FILE-crop.pdf

echo "$ psbook $FILE-crop.ps book-$FILE.ps"
psbook $FILE-crop.ps book-$FILE.ps

echo "ps2pdf book-$FILE.ps book-$FILE.pdf"
ps2pdf book-$FILE.ps book-$FILE.pdf

echo "$ pdfnup book-$FILE.pdf --nup 2x1"
pdfnup book-$FILE.pdf --nup 2x1

echo "pdftk book-$FILE-2x1.pdf cat 1-endE output ready-$FILE.pdf"
pdftk book-$FILE-2x1.pdf cat 1-endE output ready-$FILE.pdf

echo "$FILE-crop.pdf $FILE-crop.ps book-$FILE.ps book-$FILE.pdf book-$FILE-2x1.pdf"
rm $FILE-crop.pdf $FILE-crop.ps book-$FILE.ps book-$FILE.pdf book-$FILE-2x1.pdf

Basically, it first cut the blank space in the margins of the pdf using pdfcrop (which cames with pdftk), convert it to ps using pdftops (instead of pdf2ps, I’m not sure but this algorithm seems better), then it change the order of pages (put the last and the first in the page 1, the penultimate and the second in page 2 and so) with psbook (which cames with psutils package), convert back to pdf using ps2pdf, rotate 90º to print it as a portrait (standard to lpr) with pdftk and finishes removing the temporary files.

Eduardo,

I couldn’t perform it using page-crunch, a nice tip you have posted before to print multiple pages per sheet. There is a book flag there but it put the PDF in sequential order two side per page (If I have done it right).

See ya

6 Responses to Create a script to print a PDF booklet (nice for papers)

  1. NicoLarve says:

    Hi,

    Here is a little improved version of you’re script:
    – things are processed *only* if previous steps ended correctly,
    – output is cleaned up and easier to read,
    – fixed bug for filenames that contains spaces and other characters that need to be escaped,
    – fixed bug for output filename of pdfnup (not “-2×1” but “-nup”

    Nicolas

    #!/bin/bash
    
    # usage: pdf2book 
    
    # acroread settings:
    # [x] auto rotate and center
    # [x] choose paper source by pdf page size
    # [x] use custom paper size when needed
    # [x] portrait
    
    FILE=$(basename "$1" .pdf)
    
    echo "+ pdfcrop $FILE.pdf --margins 12" \
    && pdfcrop "$FILE.pdf" --margins 12 &> /dev/null \
    \
    && echo "+ pdftops $FILE-crop.pdf" \
    && pdftops "$FILE-crop.pdf" &> /dev/null \
    \
    && echo "+ psbook $FILE-crop.ps book-$FILE.ps" \
    && psbook "$FILE-crop.ps" "book-$FILE.ps" &> /dev/null \
    \
    && echo "+ ps2pdf book-$FILE.ps book-$FILE.pdf" \
    && ps2pdf "book-$FILE.ps" "book-$FILE.pdf" &> /dev/null \
    \
    && echo "+ pdfnup book-$FILE.pdf --nup 2x1" \
    && pdfnup "book-$FILE.pdf" --nup 2x1 &> /dev/null \
    \
    && echo "+ pdftk book-$FILE-2x1.pdf cat 1-endE output ready-$FILE.pdf" \
    && pdftk "book-$FILE-nup.pdf" cat 1-endE output "ready-$FILE.pdf" \
    \
    && echo "cleaning..." \
    && rm "$FILE-crop.pdf" "$FILE-crop.ps" "book-$FILE.ps" "book-$FILE.pdf" "book-$FILE-nup.pdf"
  2. emanuelvianna says:

    Hi Nicolas,

    Nice to see that you’ve enjoyed this script. I liked the idea of have my papers in a “gibi” format (more portable and confortable to read). Thanks for the bug report and nice tip! (only execute next step if the last one ended correctly).

    See ya!
    Emanuel

  3. Ed Schouten says:

    Instead of all the hackery performed with the &&’s, why not simply put this above the script?

    set -e -x

    This will log all called commands and abort upon error. Also, use #!/bin/sh, not #!/bin/bash, since this script is not bash-specific in any way. 🙂

  4. emanuelvianna says:

    Hi Ed,
    Thanks for the improvement suggestions! I discover -x argument after this post (this feature saved me a lot of time debugging large scripts). Regarding /bin/sh, actually I didn’t know it, I’ve using /bin/bash in all my scripts. Good to know!
    See ya!
    Emanuel

  5. Hi there! In order to use e.g. pdfcrop you need texlive-extra-utils in Ubuntu / Debian.

  6. Sam I Am says:

    This is a very useful script, so thank you very much for sharing! Here’s the dependencies that I had to install in order for it to work on Ubuntu 12.10 (Quantal).

    sudo apt-get install texlive-latex-recommended pdfjam texlive-latex-base pdftk pdflatex texlive-extra-utils psutils

    Also, I had to modify the following line from:

    pdftk book-$FILE-2×1.pdf cat 1-endE output ready-$FILE.pdf

    to:

    pdftk book-$FILE-nup.pdf cat 1-endE output ready-$FILE.pdf

    Thanks again!

Leave a reply to Benjamin Bach Cancel reply