How I print books

I tend to print books or zines on paper rather than read it from the screen (I help killing one small tree every week), I have some tools to do make this easier.

This is a shell script to split a pdf into chapters using the builtin bookmarks:

#!/bin/bash

infile=$1 # input pdf
outputprefix=$2

[ -e "$infile" -a -n "$outputprefix" ] || exit 1 # Invalid args

pagenumbers=( $(pdftk "$infile" dump_data | grep -A1 '^BookmarkLevel: 1'| \
                grep '^BookmarkPageNumber: ' | cut -f2 -d' ' | uniq)
              end )

for ((i=0; i < ${#pagenumbers[@]} - 1; ++i)); do
  a=${pagenumbers[i]} # start page number
  b=${pagenumbers[i+1]} # end page number
  [ "$b" = "end" ] || b=$[b-1]
  pdftk "$infile" cat $a-$b output "${outputprefix}"_$a-$b.pdf
done

I use duplex since I don't own a fancy printer that does in-hardware printing. duplex is a bunch of shell scripts that makes use of scripts provided by cups, it has a queue and can print non pdf documents (Postscript and textfiles).

I think definitively better than my old 5 line shell script that used pdftk:

pdftk A=$1 cat Aeven output /tmp/even_pages.pdf
pdftk A=$1 cat Aodd  output /tmp/odd_pages.pdf
pdftk /tmp/even_pages.pdf cat end-1 output /tmp/even_pages1.pdf 
lp /tmp/odd_pages.pdf
read -rsp $'Press any key when ready...\n' -n1 key
lp /tmp/even_pages1.pdf
hacker emblem