Thursday, May 2, 2024
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 3802  / 1 Year ago, sun, january 15, 2023, 9:42:49

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?


More From » software-recommendation

 Answers
6

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

[#35206] Tuesday, January 17, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ttarum

Total Points: 417
Total Questions: 101
Total Answers: 115

Location: Maldives
Member since Wed, Nov 4, 2020
4 Years ago
ttarum questions
Sat, Aug 20, 22, 12:42, 2 Years ago
Wed, Sep 28, 22, 18:07, 2 Years ago
Mon, Feb 7, 22, 20:23, 2 Years ago
;