Split/Merge PDF Files From Command Line

Introduction

Sometimes we come across a situation where we need to split or stitch the pdf document. The simplest and easiest way is to use PDF Toolkit on the command line.

Let's first install it using the following command:

$ sudo apt-get install pdftk

Now that pdftk is installed, lets take a smaple pdf file and split it into parts.

Splitting PDF

We can split the entire pdf into single pages using the following command:

$ pdftk myFile.pdf burst

We can also split only specific page by specifying the page number:

$ pdftk myFile.pdf cat 21 output page_21.pdf

You can also split specific set of pages by using the following command:

$ pdftk myFile.pdf cat  21-end output 21_to_last.pdf

Merging PDF

Now that we have scissored a lot of pages. Let's go ahead and stitch a few of them.

We can merge specific pages by using the following command:

$ pdftk page_21.pdf 21_to_end.pdf cat output stitched.pdf

This will stitch the two pdf's we created above.

If we have a huge list of single age pdf's in a folder and if we want to stitch them together, we can run the following command.

$ pdftk *.pdf cat output newFile.pdf

This will stitch all the pdf in the current directory to a single pdf.

Conclusion

That's it. We have successfully split and merged pdf files using pdftk. Hope you find this useful.