Cutting and merging PDF files from the command line

piczmar_0 profile image Marcin Piczkowski Updated on ・1 min read

Photo by Sharon McCutcheon on Unsplash

I wanted to show you a few simple commands which saved me a lot of time while tidying up some PDF files.
You can cut them into pages, merge or split based on the page range. Here it is:

  • cut off the first page from input.pdf file and save result as output.pdf
pdftk input.pdf cat 2-end output output.pdf
  • merge file1.pdf and file2.pdf into single output.pdf
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite \
-sOutputFile=output.pdf file1.pdf file2.pdf 
  • split input.pdf into single-page pdf files starting from 2-nd and ending on 15-th page. It will create files: output_2.pdf, output_3.pdf, output_4.pdf and so on.. up to output_15.pdf
pdfseparate -f 2 -l 15 input.pdf out_%d.pdf
  • convert image to pdf file

First, install img2pdf:

sudo apt-get install img2pdf

Then convert image.jpg to output.pdf:

img2pdf image.jpg -o output.pdf

Posted on by:

piczmar_0 profile

Marcin Piczkowski


Software engineer with over 10 years experience in different technology stacks, architecting, developing, CI/CD and leading teams. Currently working with Java, Node.JS and Serverless


markdown guide