I have a few books as PDFs, and I'd like to split them by chapter for easier consumption on a mobile phone. Is there any tool (or sequence of tools) I can use to easily split the book into different PDF files by chapter?
I have a few books as PDFs, and I'd like to split them by chapter for easier consumption on a mobile phone. Is there any tool (or sequence of tools) I can use to easily split the book into different PDF files by chapter?
This would be hard to do automatically, as there is no standard chapter label in a PDF file. However, if you do not mind some manual work, write down the pages for each chapter, and use pdftk
to select the pages and save them into a new pdf file. For example, if chapter 2 is pages 23-45, do
pdftk A=input.pdf cat A23-45 output chapter_2.pdf
You need to install pdftk first:
sudo apt-get install pdftk
You can also use ghostscript (which comes preinstalled) directly:
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=23 -dLastPage=45 -sOutputFile=chapter_2.pdf input.pdf
This is quite easy to convert a script that runs things semi-automatically. First, create an input file; on each line, put the desired output pdf file title, the starting page, the ending page -- like that:
chapter1 1 23
chapter2 24 56
Save this, using a text editor, to file pdfchaps.in
.
Now, create a script -- save the following to a file called pdfchaps.sh
:
#!/bin/bash
function pdfextr() {
echo "Chapter $2"
pdftk A=$1 cat A$3-$4 output $2.pdf
}
chapters=$1
pdffile=$2
echo "Splitting pdf file $pdffile by chapters from $chapters"
cat $chapters | while read line ; do pdfextr $pdffile $line ; done
Make the script executable:
chmod a+x pdfchaps.sh
And run it:
pdfchaps chaps.in mybook.pdf