summaryrefslogtreecommitdiff
path: root/info/latex4musicians/pdfcrop.sh
blob: 87430839bf25bc8f04a203ee91338b36c5272b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/sh

# pdfcrop.sh
# Guido Gonzato, PhD. GPL 2 or later.

MYSELF=$(basename $0)

if [ $# -eq 0 ] ; then
  printf "Usage: ${MYSELF} <file.pdf>\n"
  printf "This script uses 'gs' to crop a one-page pdf file.\n\n"
  exit 1
fi

# GhostScript for Windows must be installed in C:\Gs
# GS=/c/gs/gs9.26/bin/gswin64c.exe
# GNU/Linux and others:
GS=/usr/bin/gs

INPUT=$1
PDF=$(basename $1 .pdf)
OUTPUT=$PDF-crop.pdf
GSOPTS="-q -sDEVICE=bbox -dBATCH -dNOPAUSE"

# find out the bounding box
$GS $GSOPTS $INPUT 2>&1 | grep "%%B" > $PDF.bbox

# read bbox coordinates in variables
read tmp X1 Y1 X2 Y2 < $PDF.bbox

# write the output, cropped to bbox
$GS -q -o $OUTPUT \
  -sDEVICE=pdfwrite \
  -c "[ /CropBox [$X1 $Y1 $X2 $Y2] /PAGES pdfmark" \
  -f $INPUT

/bin/rm -f $PDF.bbox

echo "$INPUT cropped to $OUTPUT"