3

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
SuNnY_sYeD
  • 513
  • 1
  • 6
  • 19
  • it is straightforward to parse the input file, read the BB line and cook up the appropriate gs command. The specifics will depend on what language you'd like to use. – agentp Dec 22 '12 at 15:28
  • The bbox line in the postscipt is not correct so I need to use the bbox command to find the correct bbox. Then create the gs command based on the output of bbox. – SuNnY_sYeD Dec 22 '12 at 19:25
  • This is the windows cmd.exe shell? I could probably help with bash and awk, but I don't know windows that well at the moment. – luser droog Dec 23 '12 at 09:22
  • well then you run gs twice, first with -sDEVICE=bbox, parse the output and again with the pdf device. A more satisfying alternative would be to dive into the pdf device driver and give it an option to do what you want... – agentp Dec 23 '12 at 16:26
  • I tried redirecting the output of the gs command by including ">> out.txt" but i cant seem to capture the bbox output since the command invokes a shell. – SuNnY_sYeD Dec 25 '12 at 07:22
  • @user1864948: see my answer about your problem to capture the `bbox` info in a file. – Kurt Pfeifle Dec 25 '12 at 12:38

1 Answers1

4

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:

Community
  • 1
  • 1
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345