Skip to content

Instantly share code, notes, and snippets.

@flying-sheep

flying-sheep/splitpdf.sh

Last active May 17, 2017
Embed
What would you like to do?
split up “nup”d PDFs. depends on pdfjam and pdftk
#!/bin/bash
# some PDFs are only delivered with multiple pages “nup”d onto one,
# e.g. 4 slides arranged 2×2 on one PDF page. This script separates them.
# horizontal×vertical nup layout, e.g.: '1x1' '2x2', '1x2'
NUP='1x1'
# When re-nuping, add a frame? 'true' or 'false'
FRAME='false'
# Trim: left down right up
# In this example we have 21cm × 29.7cm, i.e. half height is 15.5cm.
UPPER_TRIM='0.1cm 15.4cm 0.1cm 0.6cm'
LOWER_TRIM='0.1cm 0.5cm 0.1cm 15.5cm'
for infile in "$@"; do
echo -n "De-nuping “$infile”..."
TEMPDIR="$(mktmp -d)"
# extract upper and lower pages
pdfnup --nup 1x1 --outfile "$TEMPDIR/upper_pages.pdf" --trim "$UPPER_TRIM" "$infile" > /dev/null
pdfnup --nup 1x1 --outfile "$TEMPDIR/lower_pages.pdf" --trim "$LOWER_TRIM" "$infile" > /dev/null
# de-nup upper and lower pages
pdftk "$TEMPDIR/upper_pages.pdf" burst output "$TEMPDIR/page_%04d_a.pdf" > /dev/null
pdftk "$TEMPDIR/lower_pages.pdf" burst output "$TEMPDIR/page_%04d_b.pdf" > /dev/null
# join pages and re-nup them
pdfjoin --outfile "$TEMPDIR/concat.pdf" "$TEMPDIR"/page_????_?.pdf > /dev/null
pdfnup --nup "$NUP" --frame "$FRAME" --outfile "${NUP}_$infile" "$TEMPDIR/concat.pdf" > /dev/null
# temporäre dateien löschen
rm -rf "$TEMPDIR"
rm doc_data.txt
echo 'done.'
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment