Shell scripting fun: pdfsplit
Posted on June 7, 2011
by Tommy McGuire
Like various other people, I have been irritated by the problems with viewing two-column PDF files on a Kindle. Yesterday, however, I ran across pdfsplit, which made me decide to do something about it.I downloaded the package, which is Cabal-ized Haskell (!), and like my other adventures with Cabal, it failed to build. Naturally, I took a peek at the code, and found it to be a very simple script that used some libraries I apparently don't have (or have and don't know it; or something).
I like writing Haskell as much as anyone, but sometimes the right tool really is a hammer. In that spirit, here is a Bourne shell script:
#!/bin/sh
#
# pdfsplit - create a single-column version of a 2-column PDF file
if [ $# -lt 1 ]
then
echo "Usage: $0 file.pdf ..."
echo " Produces file-split.pdf ..."
exit 1
fi
for input in "$@"
do
base=`basename $input .pdf`
tmp=$base-split.tex
pages=`pdfinfo $input | grep Pages | awk '{print $2}'`
cat <<eof >$tmp
\\documentclass{article}
\\usepackage[paperwidth=4.25in,paperheight=11in]{geometry}
\\usepackage{pdfpages}
\\begin{document}
eof
n=1
while [ $n -le $pages ]
do
cat <<eof >>$tmp
\\includepdf[pages=$n,noautoscale,offset=2.125in 0]{$input}
\\includepdf[pages=$n,noautoscale,offset=-2.125in 0]{$input}
eof
n=$((n+1))
done
cat <<eof >>$tmp
\\end{document}
eof
pdflatex $tmp
rm -f $tmp $base-split.aux $base-split.log
done
To use it, you will need pdflatex, the pdfpages package, and pdfinfo (from the poppler-utils package on my Ubuntu laptop).