random, infrequent posts about R and programming.

mediocre economist.
aspiring data scientist.
ultimate player.

Cropping a Multiple Page PDF Using Command Line (macOS)

· Read in about 2 min · (357 Words)
command line macOS

pdfcrop command

I learned about pdfcrop in a stackoverflow post. pdfcrop is a tool that can crop multiple-page PDFs (not to be confused with multiple PDFs).

Discovering it was memorable enough that I thought it warranted a post.

Installation

The full version of MacTex comes with a command line tool called pdfcrop. See if you have it by typing:

$ which pdfcrop
/Library/TeX/texbin/pdfcrop

If you don’t have, it can be installed from TexLive:

sudo tlmgr install pdfcrop

Usage with Newer PDFs

Below is an example script that crops Hadley Wickam’s Tidy Data paper.

$ cd ~/Downloads
$ wget https://www.jstatsoft.org/index.php/jss/article/view/v059i10/v59i10.pdf
$ pdfcrop --margins '10 15 10 15' --xetex v59i10.pdf
PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
==> 23 pages written on `v59i10-crop.pdf'.
$ ls *crop.pdf
v59i10-crop.pdf
  • The --margins "<left> <top> <right> <bottom>" option adjusts the margins. For newer PDFs with meta data, --margins initially removes the entire margin before implementing the adjustment. If the numbers is positive, then --margins will actually augment the file with white space (in bp = big point units where 72 bp = 1 inch). To reduce the margins, values need to be negative.
  • Note that --margins "<number>" will add/remove a uniform band around the margin.
  • I use the --xetex option because I found that it produced a high quality PDF that took up less disk space.

Usage with Older PDFs

As mentioned, newer PDFs with margins meta data, the --margins removes the margin white space prior to adjustment. This is NOT the case with older PDFs. With older PDFs, you generally only want to remove margins (--margins with negative values) because pdfcrop finds no margins meta data. Here is an example where we trim 0.75 inches (0.75 in = 72*0.75 = 54 bp) around a very old PDF.

$ wget http://seismo.berkeley.edu/~kirchner/eps_120/Odds_n_ends/Students_original_paper.pdf
$ pdfcrop --margins '-54' --xetex Students_original_paper.pdf
pdfcrop --margins '-54' --xetex Students_original_paper.pdf
PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
==> 26 pages written on `Students_original_paper-crop.pdf'.
$ ls Students*crop.pdf
Students_original_paper-crop.pdf

For the Lazy

If you don’t feel like running the code above, you can get the PDFs downloaded then adjusted in the examples below:

Comments

comments powered by Disqus