Thursday, May 2, 2024
200
rated 0 times [  200] [ 0]  / answers: 1 / hits: 188277  / 3 Years ago, sat, july 3, 2021, 2:12:01

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.


More From » command-line

 Answers
7


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.


[#39126] Sunday, July 4, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fotres

Total Points: 35
Total Questions: 124
Total Answers: 112

Location: Federated States of Micronesia
Member since Sat, Jun 4, 2022
2 Years ago
;