Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ghostscript PDF to PNG: output is always 595x842 (A4)

Tags:

ghostscript

I try to convert PDF to PNG, but ouput image is always A4, however the source PDF is very huge. Here are my commands:

-dNOPAUSE ^
-dBATCH ^
-dSAFER ^
-sDEVICE=png16m ^
-dFirstPage=1 ^
-sOutputFile="D:\PDF.png" ^
 "D:\PDF.pdf" ^
-sPAPERSIZE=a1

I tried several options (-r, -g, -sDEFAULTPAPERSIZE), but none worked.

How can I force the output image dimensions?

P.S: my PDF file

like image 242
desperate man Avatar asked Apr 10 '12 15:04

desperate man


People also ask

How does GhostScript work?

Ghostscript is capable of interpreting PostScript, encapsulated PostScript (EPS), DOS EPS (EPSF), and -- if the executable was built for it -- Adobe Portable Document Format (PDF). The interpreter reads and executes the files in sequence, using the method described under "File searching" to find them.

Why GhostScript is used?

Ghostscript can be used as a raster image processor (RIP) for raster computer printers—for instance, as an input filter of line printer daemon—or as the RIP engine behind PostScript and PDF viewers. It can also be used as a file format converter, such as PostScript to PDF converter.

How do I stop GhostScript?

The option -c quit will execute the quit command and therefor terminate GhostScript. You can execute arbitrary commands with -c . So you can just append quit to the commands you pass for the conversion, Or you can feed the commands on stdin and GhostScript will terminate at end-of-file.


1 Answers

Your linked-to PDF file has only 1 page. That means your commandline parameter -dFirstPage=1 doesn't have any influence.

Also, your -sPAPERSIZE=a1 parameter should not be last (it doesn't have any influence here -- so Ghostscript takes the default size from the pagesize of the input PDF, which is A4). Instead it should appear somewhere before the "D:\PDF.pdf" (which must be last).

It looks like you want a PNG with the size of A1, and your OS is Windows (guessing from the partial commandline you provided)?

Try this instead (it adds -dPDFFitPage=true to the commandline and puts the arguments into a correct order, while also shortening it a bit using the -o trick):

gswin32c.exe ^
  -o "D:\PDF.png ^
  -sDEVICE=png16m ^
  -sPAPERSIZE=a1 ^
  -dPDFFitPage=true ^
   "D:\PDF.pdf"

This should give you a PNG with the size of 1684x2384 pixel at 72dpi (which is the builtin default for all Ghostscript image output, used if no other resolution is specified). For different combinations of resolution and pagesize add your variation of -rXXX and -gNNNxMMM (instead of -sPAPERSIZE=a1) but by all means keep the -dPDFFitPage=true....

You can also keep the -sPAPERSIZE=a1 and add -r100 or -r36 or -r200 if you want a different resolution only. Be aware that increasing resolution may not improve the image quality compared to the default output of 72dpi. That depends on the resolution of the images that were embedded in the PDF page. But it surely increases the file size...

like image 165
Kurt Pfeifle Avatar answered Oct 29 '22 16:10

Kurt Pfeifle