16

I have PDF file in which I want to trim all the white space around as much as possible. It has to automatically detect and do it.

I use Foxit reader and it has option to crop pages automatically while viewing.

But I am looking for a command line solution in Linux.

I tried PDFCrop but it crops all the pages uniformly. I want it to crop based on the white space around each individual page using this command:

pdfcrop input.pdf output.pdf

But it works only partially, some pages are cropped but some are not.

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
Santhosh
  • 649
  • 1
  • 8
  • 20
  • Good question. Have you looked at [any of these answers on a similar question posted on Ask Ubuntu](https://askubuntu.com/q/124692)? – Giacomo1968 Sep 30 '19 at 01:58
  • 1
    Edited my question with what i tried based on the link – Santhosh Sep 30 '19 at 02:03
  • see also [Are there tools available for trimming PDF margins?](https://superuser.com/questions/205126/are-there-tools-available-for-trimming-pdf-margins) – milahu Dec 30 '22 at 15:18

1 Answers1

20

The best way to remove margins is using pdfCropMargins

Install:

pip install pdfCropMargins --upgrade

Now we can use the command pdf-crop-margins to crop the pdf the way we want

I want to crop all the pages and then add 6bp margin all around. I can do that by

pdf-crop-margins -v -p 0 -a -6 input.pdf

-v : Verbose
-p : how much percentage of margin to be retained. We want the bounding box triming all the white margins on all sides. So say 0%
-a : Note: first -p is applied to create a bounding box. 
     IN this option it applies further on the bounding box after -p is applied. we can remove or add margin. Negative means add margin to the bounding box. So it will add 10bp all around after cliping

The output file is affixed with _cropped

Also I checked the cropped file and all the contents and links are intact.

Greenonline
  • 2,235
  • 11
  • 24
  • 30
Santhosh
  • 649
  • 1
  • 8
  • 20