GhostScript Commands

For Linux, use "gs", for 64-bit Windows, use "gswin64c", for 32-bit Windows, use "gswin32c". I'll flit between them here, but will mostly be on Windows.

Extract a subset of pages from a PDF

gswin64c -dNOPAUSE -dQUIET -dBATCH -sOutputFile="output.pdf" -dFirstPage=14 -dLastPage=32 -sDEVICE=pdfwrite "input.pdf"

Extracts pages 14-32 of input.pdf and saves to output.pdf.

Taken from here.

Note that the "-dNOSAFER" option may be necessary on newer systems.

See here.

Extract each page of a PDF individually

gswin32c -dNOPAUSE -dQUIET -dBATCH -dFirstPage=1 -dLastPage=10 -o "output_%d.pdf" -sDEVICE=pdfwrite "input.pdf"

10 page document assumed.

Taken from here

Combine PDF files

gswin64c -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dAutoRotatePages=/None -dAutoFilterColorImages=false -dAutoFilterGrayImages=false -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode -dDownsampleMonoImages=false -dDownsampleGrayImages=false -sOutputFile=output.pdf file1.pdf file2.pdf file3.pdf file4.pdf

Taken from here.

Compress a PDF

Warning: long command

gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution=64 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=64 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=64 -sOutputFile=output.pdf input.pdf

Taken from here.

Extract the text of a PDF

gswin64c -sDEVICE=txtwrite -o "output.txt" "input.pdf"

(This can be looped in cmd.exe as the following):

for %i in (*.pdf) do if not exist "%~ni.txt" gswin64c -sDEVICE=txtwrite -o "%~ni.txt" "%~i"

Extract a PDF into images

gs -q -dBATCH -dNOPAUSE -sDEVICE=jpeg -dFirstPage=1 -dLastPage=10 -sOutputFile=Output%d.jpg -r300 Input.pdf

10 page document assumed.

(This can be used with Tesseract to extract the text from scanned PDFs).

Taken from here.