Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GhostScript use bbox to crop Postscript file

What I am trying to accomplish is to crop my PostScript file called example.ps using the output described in bbox. I am doing this in a batch process where the bbox might be different for certain files. I have looked at pdfcrop and seen that it uses a similar approach. Here is the command I am using to crop right now.

gs -o cropped.pdf \
   -sDEVICE=pdfwrite \
   -dDEVICEWIDTHPOINTS=160 \
   -dDEVICEHEIGHTPOINTS=840 \
   -dFIXEDMEDIA \
   -c "0 0 translate 0 0 160 840 rectclip" \
   -f  example.ps

The issue with this command is that I have to specify what width and height to use. What I want to happen is to some how call bbox first and then call this statement either through code or by using some command line redirection.

like image 998
SuNnY_sYeD Avatar asked Apr 08 '26 10:04

SuNnY_sYeD


1 Answers

First, be aware that not every single page from a multi-page PostScript file will show the exact same "bounding box" values (in fact, this is rather rare). So you probably want to find out the common denominator across all possible bounding boxes (which would include them all).

Second, what you see in the console window when you run gs -sDEVICE=bbox is a mix of stdin and stdout output channels. However, the info you're after is going to stderr. If you redirect the command output to a file, you're capturing stdout, not stderr! To suppress some of the version and debugging info going to stderr add -q to the commandline.

So in order to get a 'clean* output of the bounding boxes for all pages, you have to re-direct the stderr channel first, which you then capture in file info.txt. So run a command like this (or similar):

gs              \
  -dBATCH       \
  -dNOPAUSE     \
  -q            \
  -sDEVICE=bbox \
   example.ps   \
2>&1            \
| tee info.txt

or even this, should you not need the info about the HiResBoundingBox:

gs              \
  -dBATCH       \
  -dNOPAUSE     \
  -q            \
  -sDEVICE=bbox \
   example.ps   \
2>&1            \
| grep ^%%Bound \
| tee info.txt

Also, BTW, note that can determine the bounding boxes of PostScript as well as PDF input files.

This should give you output like the following, where each line represents a page of the input file, starting with page 1 on the first line:

 %%BoundingBox: 36 18 553 802
 %%BoundingBox: 37 18 553 804
 %%BoundingBox: 36 18 553 802
 %%BoundingBox: 37 668 552 803
 %%BoundingBox: 40 68 532 757

Lastly, you might want to read up in the following answers for some background info about Ghostscript's bbox device. You'll also find some alternative PostScript code for the cropping job there:

  • PDF - Remove White Margins
  • How to crop a section of a PDF file to PNG using Ghostscript
like image 112
Kurt Pfeifle Avatar answered Apr 10 '26 12:04

Kurt Pfeifle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!