How to split/join pdf files

How to split/join pdf files

Ghostscript is a very useful tool in this situation.

  • Install ghostscript (the gs command line).
sudo apt install -yq ghostscript
  • To extract pages from the 3rd page to the 5th page in in.pdf , write output to out.pdf
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=3 -dLastPage=5 -sOutputFile=out.pdf in.pdf

By default -dFirstPage is 1,  -dLastPage is the last page number.

  • To combine 2 pdf files in1.pdf in2.pdf, write output to out.pdf.
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sOutputFile=out.pdf in1.pdf in2.pdf
  • To resize a pdf size, use the -dPDFSETTINGS= argument (reference).
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dPDFSETTINGS=/ebook -sOutputFile=out.pdf in.pdf

From the official manual (reference):

Controls and features specific to PostScript and PDF input

-dPDFSETTINGS=configuration

Presets the “distiller parameters” to one of the following predefined settings:

  • /screen selects low-resolution output similar to the Acrobat Distiller (up to version X) “Screen Optimized” setting.
  • /ebook selects medium-resolution output similar to the Acrobat Distiller (up to version X) “eBook” setting.
  • /printer selects output similar to the Acrobat Distiller “Print Optimized” (up to version X) setting.
  • /prepress selects output similar to Acrobat Distiller “Prepress Optimized” (up to version X) setting.
  • /default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.
Buy Me A Coffee