Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

php - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...