1. Tutorials

Easily Split PDFs to Smaller Chunks With Linux Command Line

.

The Linux command line is quite magical in that it can do wonders in 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:

1 PDF

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

And 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.

.
guest
0 Comments
Inline Feedbacks
View all comments

Your Full Linux Guide

New to Linux and the open source world? We have compiled a huge list of resources to help you go through Linux and its distributions. Visit the full Linux guide page right now.

Privacy & Security Series

Care about digital privacy and security? We have prepared a long series of important articles to secure your online privacy as a Linux and open source user. Visit the Privacy & Security Series right now.

Subscribe for $5

Instead of using your adblocker, join us now on Patreon to unlock a complete ad-free experience + access to private FOSS Post forum where many internals are discussed.

Email Newsletter

.