I am looking for an open source command line tool to crop PDF file just like we can do in Adobe Acrobat Pro. I have tried PdfTk, ImageMagick, PyPDF, and GhostScript—all with no success so far.
-
Can you please describe what kind of cropping you can do with Adobe Acrobat pro? Because I do not have it and can therefore not tell what you are looking for.– xubuntixApr 24, 2012 at 8:50
-
In Adobe Acrobat Pro we can use the margin controls to crop the PDF. we can provide the value's for the top, bottom, right and left to crop– RakeshApr 24, 2012 at 8:56
10 Answers
I would suggest you take a look at PDFcrop.
If you wish to crop a pdf with left, top, right and bottom margins of 5, 10, 20, and 30 pt (points), then run
pdfcrop --margins '5 10 20 30' input.pdf output.pdf
in terminal. To actually crop something away, use negative values in the argument for crop. For example,
pdfcrop --margins '-50 -50 -50 -50' input.pdf output.pdf
crops 50 pts from the left, top, right, bottom (in this order).
If you run only the command pdfcrop input
, it will output a file titled input-crop.pdf with zero margins. I find this very handy when including pdf illustrations in documents.
Cropping multiple files
Unfortunately, pdfcrop cannot crop multiple files at the time. It is however easy to write a script that will crop all pdfs in the folder the script is located in.
Create a new empty file, and call it something.sh
. Open it with a text editor and insert the following:
#!/bin/bash
for FILE in ./*.pdf; do
pdfcrop "${FILE}"
done
Save it, and close. Then right click the file, go to Properties > Permissions and check the field Allow executing file as program. Now close the dialog. Run the script by double clicking it and choosing Run in Terminal. And new, zero-margin cropped version of all pdfs with suffix -crop will now be printed in the folder. If you want margins or other things, you can of course just open the script and add arguments after pdfcrop
.
-
Note that instead of specifying negative margins, one can also use
--bbox "<left> <bottom> <right> <top>"
. This allows to use the approach to determine the crop area described in my answer below. Mar 3, 2015 at 16:22 -
Is there a possibility of telling page number(which need to be cropped)?– L.K.Mar 13, 2017 at 12:04
-
I fear it's all or nothing.
pdfcrop --help
lists the available options. I cannot see anything there that would allow specifying a range of pages.– RasmusMar 13, 2017 at 15:20 -
12Comparing the size of PDFCrop's output to its input, it looks as if pdfcrop only modifies the bounding boxes. It doesn't remove data. So this approach would be unsuitable to make the pdf smaller, or hide information.– init_jsApr 27, 2017 at 21:10
-
1pdfcrop is super slow (on pdf with 550 pages), and blew my file up to ten times the size. This worked much better for me: tex.stackexchange.com/a/42259/193342 Sep 23, 2021 at 0:01
Thanks for Rasmus, you can install pdfcrop from texlive-extra-utils package:
sudo apt-get install texlive-extra-utils
Then crop pdf files using pdf crop command as:
pdfcrop input.pdf output.pdf
use --help
to see more amazing parameters like --margins
pdfcrop --margins 5 input.pdf output.pdf
which crop pdf with 5 bp from each side of page
-
3The measurement is
bp
, which is slightly different frompt
. See tex.stackexchange.com/questions/8260/….– kopporSep 2, 2017 at 16:06 -
3For me pdfcrop inflated the file size from 300x (from 7MB to 2GB). I had to do
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS="/ebook" -sOutputFile=output2.pdf output.pdf
afterwards, which fixed the file size.– fiktorFeb 24, 2018 at 19:07 -
Thanks for the answer. I just wish texlive-extra-utils didn't require 87 dependencies (388 MB). I know, what does it matter in this day of terabytes of disk space, but still :)– piit79Feb 15 at 22:54
You can also crop PDF files simply using Ghostscript. I have written a small script to simplify the process (inspired by this answer):
#!/bin/bash
if [ $# -lt 5 ]
then
echo "Usage: `basename $0` <pdf-file> <x_min> <x_max> <y_min> <y_max>"
echo "Notes:"
echo " - all coordinates are absolute; no calculation of width/height necessary"
echo " - use 'gv' to determine the coordinates"
exit 65
fi
file="$1"
xmin="$2"
xmax="$3"
ymin="$4"
ymax="$5"
base="${file%.*}"
outfile="${base}_cropped.pdf"
echo "writing to: $outfile"
gs \
-o $outfile \
-sDEVICE=pdfwrite \
-c "[/CropBox [$xmin $ymin $xmax $ymax] /PAGES pdfmark" \
-f $file
In order to determine the coordinates for cropping, I use gv
, which prints the coordinates of the mouse cursor using the same units as Ghostscript. For example, here I determine the minimum coordinates for x/y (the values in the upper left corner):
Now the maximum coordinates:
And finally, I run the script pdf_crop_by_coordinates.sh test.pdf 45 429 38 419
producing a test_cropped.pdf
which looks like that:
I have no idea though, how the Ghostscript solution compares to pdfcrop
in terms of quality and correctness.
-
2Flat out didn't work for me, surprising for a powerhouse like ghostscript. But pdfcrop did the job perfectly, and in one pass. The units are funky, need time to see if possible to config standard: pt, px or the like.– u2nJan 13, 2020 at 19:25
When I can't do something with pdftk, the next place I turn is PDFjam, which is a command-line wrapper for the pdfpages LaTeX package (hence you also need that and a TeX distro installed). For help on how to use it, I recommend the regular help screen:
pdfjam --help
as the man page is sparse and the Web page concentrates on examples.
To crop a PDF, the command you need is something like this:
pdfjam --keepinfo --trim "10mm 15mm 10mm 15mm" --clip true --suffix "cropped" input.pdf
This will output a file called input-cropped.pdf
. The order of the trims should be left, bottom, right, top, as per \includegraphics
from graphicx.
To give an idea of how it compares with PDFcrop, I had cause to crop a quite fancy PDF recently. My original was 675 kB, my cropped version via PDFjam was 1.2 MB, while a version cropped via PDFcrop was 4.5 MB. While both PDFjam and PDFcrop stripped out the embedded hyperlinks and bookmarks, PDFjam with the --keepinfo
option preserved the document properties (e.g. title, author, subject).
-
4Note: this does not really remove the content that becomes off-screen from the PDF, only hides it. Same as what @init_js comments on in the top-scored answer. Jul 31, 2018 at 16:25
-
This worked well for me. I didn't realize at first that pdfcrop of
--margin '0'
even takes away the whitespace it finds and the non-zero margin adjusts from there. My pdf had annoying crop marks on most of the pages so this pdfjam program was better for removing them. Feb 17, 2020 at 19:31 -
Any way to keep annotations as well? I found that they're gone after using either
pdfcrop
orpdfjam
. Mar 4, 2021 at 7:51 -
Briss is not command line, but worth a look at.
-
This is a fantastic software and it retains my annotations!! The GUI lets you look at ALL pages of the PDF superimposed simultaneously!! Mar 4, 2021 at 8:05
If a graphical tool is also fine I would recommend krop
: http://arminstraub.com/software/krop
The pdfCropMargins program is a command-line application to automatically crop the margins of PDF files.
This program depends on either the Ghostscript program or the pdftoppm program being installed (and locatable) on the system. And analyze the page images with PIL to find bounding boxes, using the threshold 191.
install using
pip install pdfCropMargins
Run using
pdf-crop-margins -v -s -u your-file.pdf
For help
pdf-crop-margins -h | more
This may help you.
This is in accordance with the newer version of Ubuntu and life.
This is Master PDF Editor. You can use it crop, add some stuff, etc.
You could use a pypdf script from this page. But in the answer to this stackexchange question, there seem to be many options as well.
-
I am not able to get what the left top right bottom parameters are. are they points, inches, centimeters ?– RakeshApr 24, 2012 at 11:00
-
@Rakesh: See my answer for an explanation of the parameters and how to determine them easily. Mar 3, 2015 at 13:12
Using pdfjam you can set any option which comes from the package pdfpages originally, and so you can set --fitpaper true
, it should adapt the the paper size to fit to the (first) input file.
I had a case to combine multiple JPG into a PDF and adding metadata as well and trim it to incoming jpg size. This will work for equal sizes of input JPGs to be cropped well and have no white margins:
# equal sizes of input images
pdfjam Image_00001.jpg Image_00002.jpg Image_00003.jpg Image_00004.jpg Image_00005.jpg \
--pdftitle 'My custom title' \
--pdfsubject 'My custom subject' \
--pdfkeywords 'keyword1; keyword2; keyword3; aso;' \
--pdfauthor 'author1; author2;' \
--fitpaper true --outfile 'Images_Combined.pdf'
pdfinfo 'Images_Combined.pdf' # testing results for meta data
# Title: My custom title
# Subject: My custom subject
# Keywords: keyword1; keyword2; keyword3; aso;
# Author: author1; author2;
# Creator: LaTeX with hyperref
# Producer: pdfTeX-1.40.22
# CreationDate: Sun May 1 15:03:01 2022 CEST
# ModDate: Sun May 1 15:03:01 2022 CEST
# …
Note: If you have different sizes of input JPG, it will fit the paper size to the first one and scale all remaining pages to be merged to the size of that first one, so some pages will have no white margins and some will have.
Edit: this was done from TeX Live 2021 with pdfjam --version
3.03 and package pdfpages
from 2021/03/06