I usually use pdfcrop to remove white margins of pdf illustrations in LaTeX documents. Using it by command line:

pdfcrop myfigure.pdf

it removes all margins but to avoid me to resize the figure (if it has left and right white margins) I need a way to remove only top and bottom margins. I mean someting like

pdfcrop --margins '- 0 - 0' input.pdf

(where the - should be a way to make pdf crop keep the original margin) to set top and bottom margins to 0 keeping original left and right margins.

Perhaps, if there is a tool to measure the white margins of a (single page) .pdf file, I could write a script to achieve my purpose.

2 Answers 2


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
  • 2
    This helps a lot. Please consider to add this feature to your software. I mean a way to crop to 0 only the desidered margins, keeping the other unchanged.
    – Gabriele
    Commented Jun 1, 2018 at 6:23
  • I found that your solution does't work for the right margin. Indeed it doesn't keep the original margin but adds more white space to it.
    – Gabriele
    Commented Jun 2, 2018 at 21:51
  • You need to get the figure's width, e.g. with identify figure.pdf, and then subtract the right bounding box value to get the right margin value.
    – Gabriele
    Commented Jun 2, 2018 at 22:40
  • 1
    @GabrieleNicolardi Thanks, answer fixed and a variant with option --bbox added. Commented Jun 3, 2018 at 15:12
  • Just wondering if anyone has a script that can do this in one fell swoop? Commented Dec 3, 2019 at 17:59

Here's a scripted version I created. Thought this might be of use for other people.

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}"
rmarg="$(python -c "print(${pagesize[0]} - ${bounding[2]})")"
pdfcrop --margins "$lmarg 0 $rmarg 0" "$fname" "$fname"
  • Neat. I had to change the print to print('{:.3f}'.format(${pagesize[0]} - ${bounding[2]})), otherwise python used scientific notations for small margins and pdfcrop couldn't parse that
    – Clément
    Commented Jul 22, 2020 at 17:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .