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.
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.
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
in terminal. To actually crop something away, use negative values in the argument for crop. For example,
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: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
.Thanks for Rasmus, you can install pdfcrop from texlive-extra-utils package:
Then crop pdf files using pdf crop command as:
use
--help
to see more amazing parameters like--margins
which crop pdf with 5 bp from each side of page
You can also crop PDF files simply using Ghostscript. I have written a small script to simplify the process (inspired by this answer):
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 atest_cropped.pdf
which looks like that:I have no idea though, how the Ghostscript solution compares to
pdfcrop
in terms of quality and correctness.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:
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:
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).Briss is not command line, but worth a look at.
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
Run using
For help
If a graphical tool is also fine I would recommend
krop
: http://arminstraub.com/software/kropThis 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.
Example:
This is before This is after ctrl + k
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.
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:
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 packagepdfpages
from 2021/03/06