I'm working in a method of splitting a single large PDF file (which represents monthly settlements of a credit card). It is builded for printing but we'd like to split that file into single ones, for posterior use. Each settlement has a variable lenght: 2 pages, 3 pages, 4 pages... So we need to "read" each page, find the "Page 1 of X" and split the chunk 'till the next "Page 1 of X" appears. Also, each resulting splitted file has to have an unique Id (contained also in the "Page 1 of X" page).
While I was R&D-ing I found a tool named "PDF Content Split SA" that would do the exact task we needed. But I'm sure there's a way to do this in Linux (we're moving towards OpenSource+Libre).
Thank you for reading. Any help will be extremely useful.
EDIT
So far, I've found this Nautilus script that could do exactly what we need, but I can't make it work.
#!/bin/bash
# NAUTILUS SCRIPT
# automatically splits pdf file to multiple pages based on search criteria while renaming the output files using the search criteria and some of the pdf text.
# read files
IFS=$'\n' read -d '' -r -a filelist < <(printf '%s\n' "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"); unset $IFS
# process files
for file in "${filelist[@]}"; do
pagecount=`pdfinfo $file | grep "Pages" | awk '{ print $2 }'`
# MY SEARCH CRITERIA is a 10 digit long ID number that begins with number 8:
storedid=`pdftotext -f 1 -l 1 $file - | egrep '8?[0-9]{9}'`
pattern=''
pagetitle=''
datestamp=''
for (( pageindex=1; pageindex<=$pagecount; pageindex+=1 )); do
header=`pdftotext -f $pageindex -l $pageindex $file - | head -n 1`
pageid=`pdftotext -f $pageindex -l $pageindex $file - | egrep '8?[0-9]{9}'`
let "datestamp =`date +%s%N`" # to avoid overwriting with same new name
# match ID found on the page to the stored ID
if [[ $pageid == $storedid ]]; then
pattern+="$pageindex " # adds number as text to variable separated by spaces
pagetitle+="$header+"
if [[ $pageindex == $pagecount ]]; then #process last output of the file
pdftk $file cat $pattern output "$storedid $pagetitle $datestamp.pdf"
storedid=0
pattern=''
pagetitle=''
fi
else
#process previous set of pages to output
pdftk $file cat $pattern output "$storedid $pagetitle $datestamp.pdf"
storedid=$pageid
pattern="$pageindex "
pagetitle="$header+"
fi
done
done
I've edit the Search Criteria, and the Script is well placed in the Nautilus Script folder, but it doesn't work. I've try debugging using the activity log from the console, and adding marks on the code; apparently there's a conflict with the resulting value of pdfinfo, but I've no idea how to solve it.