Ghostscript
From wikinotes
Ghostscript is a commandline tool for modifying vector graphics like PDFs, PostScript files, etc.
It is very good at reformatting the documents for printers, shrinking files etc (uniformly scaling page-size, extracting pages, changing fonts... )
but it is not ideal for tasks associated with pdf/ps file authoring'.
Documentation
Main Page http://www.ghostscript.com/doc/9.18/Readme.htm#Theme_roadmap Intro to GS command (flags etc) http://www.ghostscript.com/doc/9.18/Use.htm List of Output-Devices https://ghostscript.com/doc/9.21/Devices.htm#PDF
Quirks
Important Flags
gs \ # ======= # general # ======= -dBATCH `# Command-Mode instead of Interpreter` \ -dNOPAUSE `# No 'ENTER' prompt` \ -sDEVICE `# Printer? PDF Writer? You choose` \ -sOutputFile="my_file.pdf" `# Writing to what file` \ -dNODISPLAY `# Does not display page to device. THIS` \ `# MEANS NOT WRITING FILE IF DEVICE IS` \ `# 'pdfwrite' or other writer ` \ -dFirstPage=1 `# first page in page-range to operate on` \ -dLastPage=5 `# last page in page-range to operate on` \ # ======== # Resizing # ======== -sPAPERSIZE=letter `# set your device-size ` \ -dDEVICEWIDTHPOINTS=100 `# set your own preferred X/Y for the ` \ -dDEVICEHEIGHTPOINTS=100 `# device you are writing to. Ghostscript ` \ `# will try as hard as possible to match ` \ `# the dimensions of this device ` \ -dPDFFitPage `# Required if you are enlarging an image,` \ `# or shrinking but need the entire page ` \ `# to remain visible ` \ -dFIXEDMEDIA `# Required any time you are overwriting ` \ `# saved page-size ` \ -g200x200 `# Manual Resize in inch/72 units (? how work?) ` \ my_in_file.pdfpts (unit)
## The base-unit for ghostscript is (1/72) of an inch. # An 8.5"/11" document is 612x792 # (72 * 8.5) / (72 * 11)Scaling in one direction
## Ghostscript cannot scale in one direction, but you can explicitly # resize a document if you know the fixed X/Y that you want. ## Unfortunately, despite my best efforts both using the -gXxY flag, # and the -DEVICEHEIGHTPOINTS/-DEVICEWIDTHPOINTS flags, even if I could # change the dimensions of the pdf size, (both with postscript and with pdfs) # it always resulted in a blank PDF. # # Because of this, I have refocused my efforts on python libraries # built to work with PDFs.
Source http://stackoverflow.com/questions/10004858/setting-auto-height-width-for-converted-jpeg-from-pdf-using-ghostscript Querying Document Info
pdfinfo \ -box `# query crop/bleed/trim/art/media boxes` \ -f 1 `# first page in range` \ -l 166 `# last page in range` \ my_pdf.pdf## page # box # startX / startY # resX / resY Page 156 TrimBox: 0.00 0.00 612.00 792.00 Page 156 CropBox: 0.00 0.00 612.00 792.00 ...ps
function get_ps_size() { ## Function to obtain postscript resolution ## local ps_file=$1 local ps_size=( $( gs -q \ -sDEVICE=nullpage \ -dBATCH \ -dNOPAUSE \ -c '/showpage{currentpagedevice /PageSize get{=}forall showpage} bind def' \ -f $ps_file ) ) echo "${ps_size[0]} ${ps_size[1]}" }
Extract all pages
gs \ -dBATCH \ -dNOPAUSE \ -dQUIET \ \ -sOutputFile="pdf_out/tmp-%04d.pdf" \ my_file.pdf
convert pdf to another format
gswin64c \ -dNOPAUSE \ -dBATCH \ -sDEVICE=ps2write \ -sOutputFile=out.ps \ in.pdf