Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to crop a section of a PDF file to PNG using Ghostscript

I need to crop a certain section in my PDF file to PNG (this will be automated using Ghostscript with PHP). This is what i do now which basically turns the first page of a PDF to PNG:

gs -q -dNOPAUSE -dBATCH \
   -sDEVICE=pngalpha -dEPSCrop \
   -sOutputFile=output.png input.pdf

Specifically, i'm trying to crop this top left card to a PNG. I'm also open for more suggestions on how to accomplish this.

like image 255
Tom Avatar asked Sep 18 '12 20:09

Tom


1 Answers

First,
determine the bounding box of your first PDF page:

gs                          \
 -q                         \
 -dBATCH                    \
 -dNOPAUSE                  \
 -sDEVICE=bbox              \
 -dLastPage=1               \
  stackoverflowQuestion.pdf \
2>&1                        \
| grep %%BoundingBox

The resulting output will be:

%%BoundingBox: 119 531 464 814

It means:

  • the lower left corner of the bounding box is at coordinate (119,531)
  • the upper right corner of the bounding box is at coordinate (464,814)

The values are in PostScript points (where 72 pt == 1 inch) . The bounding box is that rectangle, which includes these graphical PDF objects that leave ink or toner marks on a page.

Then,
create your PNG.

Deriving from the bounding box value, you seem to want it 345 pt wide (= 464 - 119) and 283 pt high (= 814 - 531). This leads to a pages size of -g345x283 (given in pixels, because Ghostscript uses by default 72 dpi for image output (unless specified otherwise), and therefor 72 px == 1 inch.

Or better, we keep a security zone of 1 pt away from the bounding box, so we make the image a bit bigger than the bare minimum and we get this image dimension: -g347x285.

You also need to cut off 119 pt from the left edge (118 pt for 'security') and 531 pt from the bottom edge (530 for security).

Hence the command would be:

gs                                                      \
  -o out.png                                            \
  -sDEVICE=pngalpha                                     \
  -g347x285                                             \
  -dLastPage=1                                          \
  -c "<</Install {-118 -530 translate}>> setpagedevice" \
  -f stackoverflowQuestion.pdf 

Here is the resulting PNG:

out.png

For a better PNG quality, increase the resolution from the default 72 dpi to 720 dpi and use this command:

gs                                                      \
  -o out720dpi.png                                      \
  -sDEVICE=pngalpha                                     \
  -r720                                                 \
  -g3470x2850                                           \
  -dLastPage=1                                          \
  -c "<</Install {-118 -530 translate}>> setpagedevice" \
  -f stackoverflowQuestion.pdf 

Update:

On Windows in a CMD window, the console application names for Ghostscript are gswin32c.exe and/or gswin64c.exe (instead of gs). Also, you'd have to use ^ as a line continuation character (instead of \).

like image 116
Kurt Pfeifle Avatar answered Oct 22 '22 01:10

Kurt Pfeifle