How to use the command 'gs' (with examples)

How to use the command 'gs' (with examples)

GhostScript is a PDF and PostScript interpreter commonly used for viewing, manipulating, and converting PDF and PostScript files. It provides a wide range of options and functionalities for working with PDFs and PostScripts.

Use case 1: Viewing a file

Code:

gs -dQUIET -dBATCH file.pdf

Motivation: When you want to view the contents of a PDF file without opening a separate PDF viewer application, you can use GhostScript to quickly display the file on the command line.

Explanation:

  • gs: The command to run GhostScript.
  • -dQUIET: Suppresses the display of any messages or logos during conversion.
  • -dBATCH: Exits GhostScript after processing the file.
  • file.pdf: The input PDF file to be viewed.

Example output: The command will display the contents of file.pdf on the command line.

Use case 2: Reducing PDF file size for e-book devices

Code:

gs -dNOPAUSE -dQUIET -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -sOutputFile=output.pdf input.pdf

Motivation: When you have a large PDF file that you want to optimize for viewing on e-book devices, using GhostScript can help reduce the file size by resizing the images to 150 dpi.

Explanation:

  • -dNOPAUSE: Disables the display of prompts and pauses.
  • -dQUIET: Suppresses the display of any messages or logos during conversion.
  • -dBATCH: Exits GhostScript after processing the file.
  • -sDEVICE=pdfwrite: Specifies the output device to be a PDF file.
  • -dPDFSETTINGS=/ebook: Sets the PDF optimization level for e-book devices.
  • -sOutputFile=output.pdf: Specifies the output file name.
  • input.pdf: The input PDF file to be optimized.

Example output: The command will create a new PDF file named output.pdf with reduced file size and optimized for e-book devices based on the input file input.pdf.

Use case 3: Converting PDF to images

Code:

gs -dQUIET -dBATCH -dNOPAUSE -sDEVICE=jpeg -r150 -dFirstPage=1 -dLastPage=3 -sOutputFile=output_%d.jpg input.pdf

Motivation: When you need to extract specific pages from a PDF and convert them into images, GhostScript provides an easy way to do it. This example extracts the first three pages of a PDF and saves them as JPEG images with a resolution of 150 dpi.

Explanation:

  • -dQUIET: Suppresses the display of any messages or logos during conversion.
  • -dBATCH: Exits GhostScript after processing the file.
  • -dNOPAUSE: Disables the display of prompts and pauses.
  • -sDEVICE=jpeg: Specifies the output device to be JPEG images.
  • -r150: Sets the resolution of the output images to 150 dpi.
  • -dFirstPage=1: Specifies the first page to be converted.
  • -dLastPage=3: Specifies the last page to be converted.
  • -sOutputFile=output_%d.jpg: Specifies the output file name format.
  • input.pdf: The input PDF file to be converted.

Example output: The command will extract the first three pages of input.pdf and save them as JPEG images with filenames output_1.jpg, output_2.jpg, and output_3.jpg.

Use case 4: Extracting pages from a PDF file

Code:

gs -dQUIET -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=output.pdf input.pdf

Motivation: When you want to extract specific pages or a range of pages from a PDF file, GhostScript can be used to create a new PDF file containing only the desired pages.

Explanation:

  • -dQUIET: Suppresses the display of any messages or logos during conversion.
  • -dBATCH: Exits GhostScript after processing the file.
  • -dNOPAUSE: Disables the display of prompts and pauses.
  • -sDEVICE=pdfwrite: Specifies the output device to be a PDF file.
  • -sOutputFile=output.pdf: Specifies the output file name.
  • input.pdf: The input PDF file to be processed.

Example output: The command will extract all pages from input.pdf and save them as a new PDF file named output.pdf.

Use case 5: Merging PDF files

Code:

gs -dQUIET -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=output.pdf input1.pdf input2.pdf

Motivation: When you have multiple PDF files that you want to combine into a single PDF, GhostScript can merge them together.

Explanation:

  • -dQUIET: Suppresses the display of any messages or logos during conversion.
  • -dBATCH: Exits GhostScript after processing the files.
  • -dNOPAUSE: Disables the display of prompts and pauses.
  • -sDEVICE=pdfwrite: Specifies the output device to be a PDF file.
  • -sOutputFile=output.pdf: Specifies the output file name.
  • input1.pdf input2.pdf: The input PDF files to be merged.

Example output: The command will merge input1.pdf and input2.pdf into a single PDF file named output.pdf.

Use case 6: Converting PostScript to PDF

Code:

gs -dQUIET -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=output.pdf input.ps

Motivation: When you have a PostScript file that you want to convert to PDF, GhostScript can easily handle the conversion.

Explanation:

  • -dQUIET: Suppresses the display of any messages or logos during conversion.
  • -dBATCH: Exits GhostScript after processing the file.
  • -dNOPAUSE: Disables the display of prompts and pauses.
  • -sDEVICE=pdfwrite: Specifies the output device to be a PDF file.
  • -sOutputFile=output.pdf: Specifies the output file name.
  • input.ps: The input PostScript file to be converted.

Example output: The command will convert input.ps to a PDF file named output.pdf.

Conclusion:

GhostScript is a powerful tool for working with PDF and PostScript files. It provides various functionalities for viewing, optimizing, converting, extracting, and merging files. By utilizing the command line interface of GhostScript, you can efficiently perform these tasks without the need for complex software or graphical interfaces.

Tags :

Related Posts

rbenv (with examples)

rbenv (with examples)

1: Install a Ruby version rbenv install version Motivation: This command is used to install a specific Ruby version on your system.

Read More
How to use the command transmission-show (with examples)

How to use the command transmission-show (with examples)

Transmission-show is a command-line utility that provides information about torrent files.

Read More
How to use the command 'avrdude' (with examples)

How to use the command 'avrdude' (with examples)

The avrdude command is a driver program for programming Atmel AVR microcontrollers.

Read More