Skip to content

Instantly share code, notes, and snippets.

@exos
Created November 15, 2017 21:13
Show Gist options
  • Save exos/0c46da8961bb4792869910f413826252 to your computer and use it in GitHub Desktop.
Save exos/0c46da8961bb4792869910f413826252 to your computer and use it in GitHub Desktop.
Script para cortar pdf's en formato de libro
#!/usr/bin/env bash
DIVISOR=16
CORES=$(cat /proc/cpuinfo| grep processor | wc -l)
NOW=$(date +%s)
export TMP_PATH="${NOW}"
mkdir $TMP_PATH
error() {
echo $@ > /dev/stderr
exit 1
}
get_pages() {
local PAGES=$(pdfinfo "$1" | grep "Pages" | cut -d":" -f2 | sed "s/^[ ]\+//g")
echo $PAGES
}
get_ranges() {
local TOTAL=$1
local D=$2
local I=1
local F=0
while [ $I -lt $TOTAL ]; do
let "F = $I + D - 1"
[[ $F -gt $TOTAL ]] && F=$TOTAL
echo "$I-${F}"
let "I = $I + $D"
done
}
process_range() {
TMP_FILE="${TMP_PATH}/p-$2.pdf"
pdftk "$1" cat $2 output $TMP_FILE
pdfbook --outfile "${TMP_PATH}/pages-$2.pdf" "${TMP_PATH}/p-$2.pdf"
rm $TMP_FILE
}
PAGES=$(get_pages "$1")
if [ $PAGES -lt 1 ]; then
error "El libro no tiene paginas"
fi
export -f process_range
get_ranges $PAGES $DIVISOR | xargs -n1 -I {} -P $CORES bash -c "process_range \"$1\" {}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment