The Linux command line is quite magical in that it can do wonders in a few lines of writing. Work which used to take hundreds of hours can be done easily using a one-line command or a Bash shell script.

We would like to deal with PDFs today; As they are the default format for sharing documents and most people reading this article have probably used tons of them in their lives. We’ll show you how you can divide a large PDF file (say 500 pages) into much smaller PDF files (e.g. 100 PDF files where each one of them is 5 pages long).

This could be useful for automation tasks or other types of use cases that may come to your mind where dealing with the original PDF is just not quite feasible.

Getting the Shell Script

First, you have to install pdftk, which is a well-known command line tool to work with PDF files on Linux and other Unix-like operating systems. You may grab it by searching for its name in your package repository, or simply installing it as a Snap package:

sudo snap install pdftk

The good-and-working Shell script to do the the job was actually provided as an answer to a Unix StackExchange question, by the user @Unlink. It simply takes two parameters named $pagesper (Number of pages in each PDF chunk you want) and $file (Name or path of the PDF file you want to target), and then just uses a while loop in Bash to invoke the pdftk command, that’s it.

Here is the script:

number=$(pdfinfo -- "$file" 2> /dev/null | awk '$1 == "Pages:" {print $2}')

count=$((($number+$pagesper-1)/$pagesper))
filename=${file%.pdf}

counter=0
while [ "$count" -gt "$counter" ]; do
 start=$((counter*pagesper + 1));
 end=$((start + pagesper - 1));
 if [ $end -gt $number ]; then
   end=$number
 fi

 counterstring=$(printf %01d "$counter")
pdftk "$file" cat "${start}-${end}" output "${filename}_${counterstring}.pdf"
 counter=$((counter + 1))
done

Put it in your home folder (Or wherever you want to work with the PDF file) under the name pdfsplit.sh, and then make it executable with:

chmod +x pdfsplit.sh

Now we are ready to use the script.

Dividing the PDF File into Smaller Chunks

Simply run the Bash script using the following command:

pagesper=5 file=my_large_file.pdf ./pdfsplit.sh

Where 5 is the number of pages in each divided PDF file you want, and my_large_file.pdf is the name of the PDF file you want to divide (Must be of course in the same directory as the Shell script).

And you’ll see how the smaller PDF files are generated:

split pdf files on linux 5

Each one of these files contains 5 pages as we requested.

Other PDF Manipulation Possibilities

You can use the pdftk command line without the script to do further manipulations with PDF files on Linux. Run pdftk --help to see its full documentation.

You can for example divide the PDF file into smaller chunks where each new PDF file is just 1 page long using the following command (So that if you have 300 pages in the PDF, 300 smaller files will be generated where each one of them is 1 page long):

pdftk my_large_file.pdf burst

You may also extract specific ranges from the PDF file into smaller files (E.g you may just want pages 1-4, 8-12 and 16 from the PDF file and nothing else):

pdftk A=my_large_file.pdf cat A1-4 A8-12 A16 output new_filename.pdf

The new PDF will contain only the pages you specified in the range.

Conclusion

So as you have seen it may not take you 5 minutes of work to get this script kicking in order to manipulate your large PDF files however you like. All thanks to the Linux command line utilities like pdftk and the power of the Bash shell.

If you have any further questions or additions on PDF manipulation on Linux, then we would love to have them in the comments section below.

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Newsletter

Enter your email address to subscribe to our newsletter. We only send you an email when we have a couple of new posts or some important updates to share.

Recent Comments

Open Source Directory

Join the Force!

For the price of one cup of coffee per month:

  • Support the FOSS Post to produce more content.
  • Get a special account on our website.
  • Remove all the ads you are seeing (including this one!).
  • Get an OPML file containing +70 RSS feeds for various FOSS-related websites and blogs, so that you can import it into your favorite RSS reader and stay updated about the FOSS world!