-->

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

2020-02-23 05:00发布

问题:

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.

回答1:

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:

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 \).



回答2:

On Windows the console application names for Ghostscript are gswin32c.exe and/or gswin64c.exe (instead of gs).

1. CMD window

In a CMD window you have to use ^ as a line continuation character (instead of \). Also, grep may not be available -- use findstr instead. Last, if gswinXX.exe is not in your %PATH%, and if the full path contains a space, you have to quote it:

"c:\program files\ghostscript\gswin64c.exe" ^
 -q                         ^
 -dBATCH                    ^
 -dNOPAUSE                  ^
 -sDEVICE=bbox              ^
 -dLastPage=1               ^
  stackoverflowQuestion.pdf ^
| findstr %%BoundingBox

2. PowerShell window

In a PowerShell window, just quoting the full path to the executable will not work. You have to run:

& "c:\program files\ghostscript\gswin64c.exe" -q -o nul: -sDEVICE=bbox my.pdf