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

I need to crop a specific section in my PDF file in PNG (this will be automated using Ghostscript with PHP). This is what I am doing now, which basically turns the first PDF page into PNG:

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

In particular, I'm trying to crop this top left map to PNG. I am also open to more suggestions on how to do this.

+4
source share
2 answers

Firstly,
Define the bounding box of your first PDF page:

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

The resulting result will be:

 %%BoundingBox: 119 531 464 814 

It means:

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

Values ​​are at PostScript points (where 72 pt == 1 inch ). A bounding box is a rectangle that includes these PDF graphics that leave ink or toner marks on the page.

Then
create your PNG.

Coming out of the bounding box, it seems to you that it has a width of 345 pt ( = 464 - 119 ) and 283 pt high ( = 814 - 531 ). This results in a page size of -g345x283 (specified in pixels, since Ghostscript uses 72 dpi by default to display the image (unless otherwise specified), and for this 72 px == 1 inch .

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

You also need to cut 119 pt from the left edge (118 pt for β€œsecurity”) and 531 pt from the bottom edge (530 for security).

Therefore, the command will be:

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

Here is the PNG result:

out.png

For better PNG quality, increase the resolution from the standard 72 dpi to 720 dpi and use the following command:

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

Update:

On Windows in the CMD window, the names of the console applications for Ghostscript are: gswin32c.exe and / or gswin64c.exe (instead of gs ). In addition, you will need to use ^ as a line continuation character (instead of \ ).

+13
source

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

1. CMD window

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

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

2. PowerShell Window

In the PowerShell window, it will not simply indicate the full path to the executable file. You need to run:

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

Source: https://habr.com/ru/post/1434939/


All Articles