gs command in Linux with Examples
gs command invokes Ghostscript, which is an interpreter of Adobe Systems PostScript and Portable Document Format(PDF) languages. After executing Ghostscript it reads further input from the standard input stream until it encounters ‘quit‘ command.
Syntax:
gs [ options ] [ files ] ...
Options: Below are some important options of gs command.
- –P : Makes Ghostscript to look first in the current directory for library files.
- -q : Quiet startup: suppress normal startup messages, and also do the equivalent of -dQUIET.
- -rnumber, -rnumber1xnumber2 : Same as -dDEVICEXRESOLUTION=number1 and -dDEVICEYRESOLUTION=number2.If only one number is given, it is used for both X and Y resolutions.
- -dNOCACHE : Disables character caching. Useful only for debugging.
- -dNOBIND : Disables the “bind” operator. Useful only for debugging.
- -dNODISPLAY : Suppresses the normal initialization of the output device. This may be useful when debugging.
- -dNOPAUSE : Disables the prompt and pause at the end. This may be desirable for applications where another program is driving Ghostscript.
- -dSAFER : Restricts the file operations that job can perform.
- -sDEVICE=device : Selects an alternate initial output device.
- -sOutputFile=filename : Selects an alternate output file (or pipe) for the initial output device.
Devices:
- PDF writer: The pdfwrite device outputs PDF.
- PS2 writer: The ps2write device outputs postscript language level 2. It is recommended that this device is used for PostScript output.
- EPS writer : The eps2write device outputs encapsulated postscript.
- PXL : The pxlmono and pxlcolor devices output HP PCL-XL, a graphic language understood by many laser printers.
- Text output : The txtwrite device will output the text contained in the document as Unicode.
Examples:
- To view a PDF :
gs -dSAFER syllabus.pdf
- To convert a figure into png :
gs -dSAFER -dNOPAUSE -sDEVICE=png16m -dGraphicsAlphaBits=4 -sOutputFile=front.png a.pdf
See front.png created in the directory.
- To convert image to 150 dpi :
gs -dSAFER -dNOPAUSE -sDEVICE=png16m -r150 -sOutputFile=front_1.png c.pdf
You can observe the difference between the two images first one is 150 dpi and second is the original image.
- To convert all pages to image :
gs -dSAFER -dNOPAUSE -sDEVICE=pgmraw -r150 -dTextAlphaBits=4 -sOutputFile=’paper-%d.pgm’ a.pdf
- To concatenate two PDFs :
gs -dSAFER -dNOPAUSE -sDEVICE=pdfwrite -r150 -sOutputFile=concatenate.pdf a.pdf b.pdf c.pdf
After executing the above command it will concatenate all three PDFs a.pdf, b.pdf, c.pdf, and save it as concatenate.pdf