| 1 | #!/bin/sh |
| 2 | |
| 3 | # pdfcrop.sh |
| 4 | # Guido Gonzato, PhD. GPL 2 or later. |
| 5 | |
| 6 | MYSELF=$(basename $0) |
| 7 | |
| 8 | if [ $# -eq 0 ] ; then |
| 9 | printf "Usage: ${MYSELF} <file.pdf>\n" |
| 10 | printf "This script uses 'gs' to crop a one-page pdf file.\n\n" |
| 11 | exit 1 |
| 12 | fi |
| 13 | |
| 14 | # GhostScript for Windows must be installed in C:\Gs |
| 15 | # GS=/c/gs/gs9.26/bin/gswin64c.exe |
| 16 | # GNU/Linux and others: |
| 17 | GS=/usr/bin/gs |
| 18 | |
| 19 | INPUT=$1 |
| 20 | PDF=$(basename $1 .pdf) |
| 21 | OUTPUT=$PDF-crop.pdf |
| 22 | GSOPTS="-q -sDEVICE=bbox -dBATCH -dNOPAUSE" |
| 23 | |
| 24 | # find out the bounding box |
| 25 | $GS $GSOPTS $INPUT 2>&1 | grep "%%B" > $PDF.bbox |
| 26 | |
| 27 | # read bbox coordinates in variables |
| 28 | read tmp X1 Y1 X2 Y2 < $PDF.bbox |
| 29 | |
| 30 | # write the output, cropped to bbox |
| 31 | $GS -q -o $OUTPUT \ |
| 32 | -sDEVICE=pdfwrite \ |
| 33 | -c "[ /CropBox [$X1 $Y1 $X2 $Y2] /PAGES pdfmark" \ |
| 34 | -f $INPUT |
| 35 | |
| 36 | /bin/rm -f $PDF.bbox |
| 37 | |
| 38 | echo "$INPUT cropped to $OUTPUT" |