#!/bin/csh # usage: # fixpdfbbox pdffile # given a pdf file with bogus bounding box (MediaBox?) # info, extract correct bbox and regenerate pdf with it # requires csh, pdftops, gs, grep, awk, head, tail, epstopdf # keywords: BoundingBox, PDF, correct, incorrect, convert # seth teller, dec 2004 # updated feb 2009 with fix from brendan diamond # generate aux filenames set pdf = $1 set base = $pdf:r set badeps = {$base}.eps- set bbox = {$base}.bbox set neweps = {$base}.eps+ set newpdf = {$base}.pdf+ # convert source PDF to EPS file with bad bounding box pdftops -eps $pdf $badeps # using ghostscript, scan convert to produce actual bbox info gs -sDEVICE=bbox -dNOPAUSE -dBATCH $badeps |& grep "%%BoundingBox:" > $bbox # get line number of BoundingBox line to replace in EPS file set line = `grep -n %%BoundingBox: $badeps | awk -F : '{print $1}'` # reconstruct new EPS file with correct BoundingBox line set last = `expr $line - 1` head -$last $badeps > $neweps # insert correct bounding box saved from earlier gs output cat $bbox >> $neweps # echo post-bbox section of eps file set first = `expr $line + 1` tail --line=+$first $badeps >> $neweps # finally convert corrected EPS file back to PDF epstopdf $neweps --outfile=$newpdf echo "created corrected PDF,EPS files" $newpdf"," $neweps # clean up cruft rm -f $badeps $bbox