May 1, 2012

PDFCAT

I've looked around for a solution to concatenate PDFs in the past and never found one. It appears to be a niche need, but I've got a number of schematic PDFs that were created sheet by sheet which need to be combined into single sets of schematics. I can use PDF Creator which works really well for printing PDFs, but I've still got to open each PDF and print it.

So I created a simple script that probably makes some poor PDFs but it works.


#!/bin/sh

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <=http://www.gnu.org/licenses/>.

OUTPUT=-
VERBOSE=false
PDFTOPS=/usr/bin/pdftops
PS2PDF=/usr/bin/ps2pdf

usage() {
 echo Usage: `basename $0` [-v] [-o output file] files
 echo pdfcat concatenats PDF files in argument order into a
 echo single PDF file or to stdout. Beware when specifying
 echo wildcard arguments as the pages will come in the order
 echo your shell picks them up.
 echo
 echo Arguments:
 echo "     -h            Show this help screen"
 echo "     -l            Show the GPL license"
 echo "     -v            Use verbose mode...not very verbose"
 echo "     -o OUTPUT     Send the output PDF to the specified"
 echo "                   file. If missing output is generated"
 echo "                   to stdout"
}

license() {
 echo "This program is free software: you can redistribute it and/or modify"
 echo "it under the terms of the GNU General Public License as published by"
 echo "the Free Software Foundation, either version 2 of the License, or"
 echo "(at your option) any later version."
 echo 
 echo "This program is distributed in the hope that it will be useful,"
 echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
 echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
 echo "GNU General Public License for more details."
 echo 
 echo "You should have received a copy of the GNU General Public License"
 echo "along with this program.  If not, see <=http://www.gnu.org/licenses/>."
}

while getopts "vhlo:" OPTION
do
 case $OPTION in
  h)
   usage
   exit 1
   ;;
  l)
   license
   exit 1
   ;;
  v)
   VERBOSE=true
   ;;
  o)
   OUTPUT=$OPTARG
   ;;
  ?)
   usage
   exit1
   ;;
 esac
done

shift $(( OPTIND-1 ))

(
for pdf in $*
do
 if $VERBOSE
 then
  echo -n Processing `basename $pdf` >&2
 fi
 $PDFTOPS $pdf -
 if $VERBOSE
 then
  echo " DONE" >&2
 fi
done
) | $PS2PDF - $OUTPUT