You can use GNU Ghostscript, gs, to manipulate PDF files. Other tools
like qpdf, pdftk, and pdfunite are also available and may be easier
to use for some tasks. These can be installed via conda-forge if not
already available.
Merge¶
You can merge multiple PDFs using gs:
gs -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=output.pdf -dBATCH \
input1.pdf input2.pdf input3.pdfThe backslash (\) allows the command to continue on the next line.
Alternatively, using pdfunite (simpler syntax):
pdfunite input1.pdf input2.pdf input3.pdf output.pdfOr using qpdf:
qpdf --empty --pages input1.pdf input2.pdf input3.pdf -- output.pdfSubsetting¶
You can create a new PDF from a subset of the pages in an original
PDF using gs, where m and n are the first and last page numbers to extract:
gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dFirstPage=m -dLastPage=n \
-sOutputFile=output.pdf input.pdfFor example, to extract pages 5-10:
gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dFirstPage=5 -dLastPage=10 \
-sOutputFile=output.pdf input.pdfOr using qpdf (e.g., pages 5-10):
qpdf input.pdf --pages . 5-10 -- output.pdfCompress / Reduce File Size¶
You can reduce the file size of a PDF by reprocessing it with compression
settings. The -dPDFSETTINGS option controls the quality/size tradeoff:
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook \
-dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdfQuality options (from lowest to highest quality, smallest to largest file):
/screen- low quality, smallest file (72 dpi)/ebook- moderate quality (150 dpi)/printer- high quality (300 dpi)/prepress- highest quality, largest file (300 dpi, preserves color)
Or using qpdf for compression/optimization:
qpdf --compress-streams=y --recompress-flate input.pdf output.pdfConvert to Grayscale¶
To convert a color PDF to grayscale:
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dQUIET \
-sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray \
-sOutputFile=output.pdf input.pdfRotate Pages¶
To rotate all pages 90 degrees clockwise (Orientation values: 0=0°, 1=90°, 2=180°, 3=270°):
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dQUIET \
-sOutputFile=output.pdf -c "<</Orientation 1>> setpagedevice" \
-f input.pdfConvert PDF to PostScript¶
You can convert PDF files to PostScript format using either Poppler’s
pdftops or Ghostscript’s pdf2ps:
pdftops input.pdf output.psor
pdf2ps input.pdf output.psBoth commands will create a PostScript file from the PDF input.
Installing Tools¶
Most tools mentioned above are available on SCF Linux systems. If you need to install them locally, you can use conda-forge:
conda install -c conda-forge qpdf poppler