Remove only top and bottom margins using pdfcrop
pdfcrop
reports the bounding boxes if option --verbose
is given, e.g.:
pdfcrop --verbose test.pdf
It reports:
PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
[...]
* Running ghostscript for BoundingBox calculation ...
[...]
Page 1
%%BoundingBox: 133 89 308 668
* Page 1: 133 89 308 668
%%HiResBoundingBox: 133.767980 89.369997 307.295991 667.205980
[...]
In this case, the left margin is 133. The right margin can be calculated via the width. The size of the PDF file is reported by pdfinfo
(assuming the pages have all the same size), e.g.:
pdfinfo test.pdf
It reports:
[...]
Page size: 595.276 x 841.89 pts (A4) (rotated 0 degrees)
[...]
Then the missing values for --margins
can be calculated and added:
pdfcrop --margins '133 0 288.276 0' test.pdf
Alternatively the bounding box option can be used:
pdfcrop --bbox '0 89 595.276 668' test.pdf
Here's a scripted version I created. Thought this might be of use for other people.
#!/bin/bash
fname="$1"
pagesize=( $(pdfinfo "$fname" | grep "Page size" | cut -d ":" -f2 | \
awk '{ print $1,$3 }') )
bounding=( $(pdfcrop --verbose "$fname" | grep "%%HiResBoundingBox" | \
cut -d":" -f2 ) )
rm "${fname//.pdf/-crop.pdf}"
lmarg="${bounding[0]}"
rmarg="$(python -c "print(${pagesize[0]} - ${bounding[2]})")"
pdfcrop --margins "$lmarg 0 $rmarg 0" "$fname" "$fname"