pdfcrop command: Trying to remove white margins using pdfcrop but it removes all links and bookmarks

Now when i look into my pdf it has all the bookmarks(or contents) removed and also i have links to various pages, the links no more work. HOw to keep intact the contents and the links while modifying a pdf

asked Sep 30, 2019 at 21:22 729 2 2 gold badges 9 9 silver badges 20 20 bronze badges

I'm not familiar with pdfcrop for other PDF command line tools I've used, you first have to export the metadata such as links, etc. to a file and save it, then run through thru crop process, and then reimport the metadata from the file you exported it to. Look over the help or documentation for such functionally with pdfcrop.

Commented Sep 30, 2019 at 22:50 how to import and reimport meta data from a pdf file in linux command line Commented Oct 1, 2019 at 0:28

I'm not seeing much with metadata and pdfcrop but I do see that it's possible via GhostScript which pdfcrop is built apparently. I'm not sure if pdftk is available in Linux or not but I've written somewhat about it's correlated as I was thinking about here: superuser.com/questions/1186580/…. There's also ways to do this with GhostScript from what I see as well. It might be that pdfcrop specifically does not support any metadata, keepinfo, dump_data, etc. functionality.

Commented Oct 1, 2019 at 1:49 pdftk's dump_data does not dumps the links data in the pdf. Commented Oct 1, 2019 at 8:07

1 Answer 1

The best way to remove margins is using pdfCropMargins https://pypi.org/project/pdfCropMargins/

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

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