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.
First,
determine the bounding box of your first PDF page:
The resulting output will be:
It means:
(119,531)
(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 therefor72 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:
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:
Update:
On Windows in a CMD window, the console application names for Ghostscript are
gswin32c.exe
and/orgswin64c.exe
(instead ofgs
). Also, you'd have to use^
as a line continuation character (instead of\
).On Windows the console application names for Ghostscript are
gswin32c.exe
and/orgswin64c.exe
(instead ofgs
).1. CMD window
In a CMD window you have to use
^
as a line continuation character (instead of\
). Also,grep
may not be available -- usefindstr
instead. Last, ifgswinXX.exe
is not in your%PATH%
, and if the full path contains a space, you have to quote it:2. PowerShell window
In a PowerShell window, just quoting the full path to the executable will not work. You have to run: