#!/bin/csh # usage: # fixpdfbb 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 # modifiziert von Frank Hermanns # überschreibt jetzt das alte File mit dem korrigierten File # extract 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 PDF to EPS file with bad bounding box pdftops -eps $pdf $badeps # using ghostscript, scan out actual bbox info gs -sDEVICE=bbox -dNOPAUSE -dBATCH $badeps |& grep "%%BoundingBox:" > $bbox # get line number of BoundingBox line to replace set line = `grep -n %%BoundingBox: $badeps | awk -F : '{print $1}'` # reconstruct EPS file with correct bbox info set last = `expr $line - 1` head -$last $badeps > $neweps # insert correct bounding box from gs cat $bbox >> $neweps # echo post-bbox section of eps file set first = `expr $line + 1` tail +$first $badeps >> $neweps # convert corrected EPS file back to PDF epstopdf $neweps --outfile=$newpdf echo "created corrected PDF,EPS files" $newpdf"," $neweps cp $newpdf $pdf # clean up rm -f $badeps $bbox rm -f $neweps $newpdf