49

I am not a programmer, but would like to learn how to crop a PDF using Ghostscript.

I have installed Ghostscript 9.01 in my machine.

Please guide me step by step process (starting from invoking Ghostscript) to crop a PDF with the specific coordinates.

I am even new to Ghostscript.

j0k
  • 22,600
  • 28
  • 79
  • 90
AMER
  • 971
  • 2
  • 10
  • 9

1 Answers1

80

First, take note that the measurement unit for PDF is the same as for PostScript: it's called a point [pt].

72 points == 1 inch == 25.4 millimeters

Assuming you have a page size of A4. Then the media dimensions are:

595 points width  == 210 millimeters
842 points height == 297 millimeters

Assuming you want to crop off:

   left edge: 24 points == 1/3 inch ~=  8.5 millimeters
  right edge: 36 points == 1/2 inch ~= 12.7 millimeters
    top edge: 48 points == 2/3 inch ~= 17.0 millimeters
 bottom edge: 72 points ==   1 inch ~= 25.4 millimeters

Then your Ghostscript commandline is this (on Windows):

gswin32c.exe                     ^
  -o cropped.pdf                 ^
  -sDEVICE=pdfwrite              ^
  -c "[/CropBox [24 72 559 794]" ^
  -c " /PAGES pdfmark"           ^
  -f uncropped-input.pdf

Or on Linux:

gs                               \
  -o cropped.pdf                 \
  -sDEVICE=pdfwrite              \
  -c "[/CropBox [24 72 559 794]" \
  -c " /PAGES pdfmark"           \
  -f uncropped-input.pdf

However, this may not work reliably for all types of PDFs [1]. In those cases you should alternatively try these commands:

gswin32c.exe                 ^
  -o cropped.pdf             ^
  -sDEVICE=pdfwrite          ^
  -dDEVICEWIDTHPOINTS=595    ^
  -dDEVICEHEIGHTPOINTS=842   ^
  -dFIXEDMEDIA               ^
  -c "24 72 translate"       ^
  -c " 0 0 535 722 rectclip" ^
  -f uncropped-input.pdf

or

gs                           \
  -o cropped.pdf             \
  -sDEVICE=pdfwrite          \
  -dDEVICEWIDTHPOINTS=595    \
  -dDEVICEHEIGHTPOINTS=842   \
  -dFIXEDMEDIA               \
  -c "24 72 translate"       \
  -c " 0 0 535 722 rectclip" \
  -f uncropped-input.pdf

[^] : To be more specific: it will not work for PDFs which come along with their own /CropBox already defined to specific values. A dirty hack around that is to change the string /CropBox for all pages where it is desired to /cROPBoX (or similar case-changing) with a text editor prior to running the above GS command. The case-change effectively "disarms" the cropbox setting (without changing any PDF object offsets invalidating the existing xref table) so it is no longer considered by PDF renderers.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • Thank you so much for your swift response. This satisfy my requirements. – AMER May 31 '11 at 10:39
  • 4
    If you have no idea what the dimensions of the original pdf are you can try `gs -sDEVICE=bbox -f uncropped-input.pdf` – simonb Jan 26 '12 at 02:35
  • 7
    @jolly swagman: No, sorry, no-no-no! The `bbox` device does **NOT** indicate the 'dimensions' of the original PDF. It does indicate that (virtual) box on each page which would contain all marks on the printed or viewed page. One command that will work to get the dimensions ('MediaBox') for each page is: `pdfinfo -box some.pdf`. – Kurt Pfeifle Jan 26 '12 at 18:12
  • 1
    @pipitas right, it only gives you a feel for the size of the page, but in future I will use `pdfinfo` as that seems much more useful. – simonb Jan 26 '12 at 20:13
  • 1
    Also, Google can help you with the conversion https://www.google.co.uk/search?q=10cm+in+points – Andy Dec 20 '12 at 11:45
  • 2
    *"However, this may not work reliably for all types of PDFs. In those cases you should alternatively try [...]"* Well, I have tried both with Ghostscript 9.10 and neither of them worked for me. On the other hand, [podofobox](http://podofo.sourceforge.net/tools.html) works and it is also much faster. See also [How to crop PDF margins using pdftk and /MediaBox](http://stackoverflow.com/q/5307825/341970) – Ali Aug 28 '14 at 12:57
  • 1
    @Ali: I also know `podofobox`, and know that it works. Also I'm familiar with some other commandline tools. But the OP explicitly asked about *Ghostscript*, and and stated that he liked to learn using Ghostscript. And that's what the answer dealt with... ;-) – Kurt Pfeifle Aug 28 '14 at 21:22
  • 1
    @KurtPfeifle My previous comment is for those future visitors who try the things in your answer and fail. Sorry, I probably should have stated that in my comment. I did not downvote your answer, it is most likely *the* answer to the question. However, my goal was to crop the damn PDF at hand no matter how. :) – Ali Aug 28 '14 at 21:43
  • Inspired by this answer, I have [answered](http://askubuntu.com/a/592206/161463) a related question over at askubuntu. My answer provides a small bash script to simplify the process and I explain a method to determine the coordinates. Maybe you want to add the link in your answer? – bluenote10 Mar 09 '15 at 20:53
  • @KurtPfeifle The line `-c "24 72 translate \ ` lack a closing `"` as well as the Windows version. I cannot edit the post as diff is less than 6 characters x) – hdl May 29 '15 at 14:40
  • @hdl: I think you *still* could have edited the post (even without the trick of adding more blanks): if you write a longer comment, why+what you edited, it should work. – Kurt Pfeifle May 29 '15 at 14:44
  • Are left/right/top/bottom the same when PDF orientation is landscape? I cannot manage to make either of your commands work for an a3/landscape PDF that I'd like to crop to a4/portrait (removing 595pt from the left) – hdl May 29 '15 at 14:51
  • @hdl: Yes they are the same. Be careful however if a `90 rotate` command is involved somewhere.... Check with `pdfinfo -box -l 100 the.pdf` what the `/Media/Crop/Art/Trim&/BleedBox` settings are for the first 100 pages, as well as their rotational status (indicated by `rot: ...`. – Kurt Pfeifle May 29 '15 at 15:04
  • Well I must have mistyped something in the first place for your first command worked the 2nd time, thanks! FWIW, rot value was 0. – hdl May 29 '15 at 15:15