Pdftk

Aus SchnallIchNet
Wechseln zu: Navigation, Suche

pdf split

pdftk myoldfile.pdf cat 1 2 4 5 output mynewfile.pdf

create new file which is containing only pages 1, 2, 3, 4 and 5
you can also set ranges like cat 1-2 4-5


pdf drehen

to rotate an pdf-file you scanned you will have to do
some steps...

  1. uncompress the pdf-file (pdftk)
  2. replace 'Rotate 0' by 'Rotate 180' using sed
  3. re-compress the file

example:

pdftk scanned.pdf output temp_uncompressed.pdf uncompress

uncompress the scanned file

sed -i "s/Rotate 0/Rotate 180/g" temp_uncompressed.pdf

replace all occcurences of 'Rotate 0' by e.g. 'Rotate 180' or 90 or 240 ... whatever!

pdftk temp_uncompressed.pdf output final_pdffile_page1.pdf compress

re-compress the file

repeat this steps until all pages are rotated it's head up
now you have one pdf-file per page...

merge files together if you need it, to get only one pdf-file for all pages!
to do this you need ghostscript installed
look here: Ghostscript


pdfrotate script

#!/bin/bash
#
# script to rotate pdf-files
# (c) by chris steidl
#

# debug
#set -x

NAME=`basename $0`
PDFFILES=""
DEGREES=180
DEBUG=0

TMPFILE=`mktemp`
rm $TMPFILE

function usage()
{
        echo;
        echo "Usage: $NAME [options] -r|--rotate DEGREES file1.pdf file2.pdf fileX.pdf ..."
        echo "Rotates the given files for DEGREES°"
        echo "Options:"
        echo "-r|--rotate <DEGREES>   Rotate all files for DEGREES° (default: 180)"
        echo "-h|--help               Show this shit here"
        echo;
        echo "Example: "
        echo "   $NAME -r 90 file1.pdf file2.pdf"
        echo "   Rotates file1.pdf and file2.pdf for 90°";
        echo;
}

while [ "$1" ]; do
        case $1 in
                -r|--rotate)
                        shift;
                        DEGREES=$1;
                        shift;
                ;;

                -h|--help)
                        usage;
                        exit 1
                ;;

                *)
                        PDFFILES="$PDFFILES $1";
                        shift;
                ;;
        esac
done

if [ -z "$PDFFILES" ]; then
        echo;
        echo "No pdf-files given to rotate!";
        usage;
        echo;
        exit 1;
fi
if [ $DEGREES -le 0 ]; then
        echo;
        echo "DEGREES is less or equal 0"
        echo "use an integer between 1 and 360 - e.g. '-r 180'"
        usage;
        echo;
        exit 1
fi

# everythings fine...
# do it...
for f in $PDFFILES; do
        # uncompress pdf
        echo -n "Uncompressing $f: ";
        /usr/bin/pdftk $f output ${TMPFILE}.pdf uncompress &> /dev/null
        [ $? -eq 0 ] && echo "done." || exit 2

        # rotate uncompressed file
        echo -n "Rotating $f: ";
        /usr/bin/sed -i "s/Rotate 0/Rotate 180/g" ${TMPFILE}.pdf
        [ $? -eq 0 ] && echo "done." || exit 3

        # re-compress the tempfile
        echo -n "Re-Compressing $f: ";
        /usr/bin/pdftk ${TMPFILE}.pdf output ${f}.${NAME}_${DEGREES}.pdf
        [ $? -eq 0 ] && echo "done." || exit 4

        echo "Saved rotated file to ${f}.${NAME}_${DEGREES}.pdf";
        echo;

        # delete tmpfile
        /bin/rm ${TMPFILE}.pdf
done

exit 0

### EOF ###