Results 1 to 7 of 7

Thread: pdftk usage question

  1. #1
    Join Date
    Jan 2007
    Beans
    Hidden!

    pdftk usage question

    Sometimes when the librarians scan papers/books to send me, they get the orientation wrong and all of the even pages need to be rotated 180 degrees. I can rotate all of those pages using pdftk

    Code:
    pdftk in.pdf cat 1-endevenS output out.pdf
    But how to 'cat' the odd pages back in, properly interleaved?

    Using this just gives me the all the rotated even pages first, then the odd pages:

    Code:
    pdftk in.pdf cat 2-endevenS 1-endodd output out.pdf

  2. #2
    Join Date
    Jan 2008
    Beans
    2

    Re: pdftk usage question

    Use the following shell script. Save it as pdf-rotate-even and make it executable.

    Code:
    #/bin/bash
    # script for rotating all even pages of a document
    
    # enough arguments?
    if [ $# -ne 2 ]; then
             echo 1>&2 Usage: pdf-rotate-even input.pdf output.pdf
             exit 127
        fi
    
    # generate a temporary directory
    TEMPDIR=/tmp/pdf`date +%N`
    mkdir $TEMPDIR
    
    # rotate all even pages, keep the odd ones
    pdftk $1 cat 1-endodd output $TEMPDIR/odd.pdf
    pdftk $1 cat 1-endevenS output $TEMPDIR/even.pdf
    
    # split the files...
    pdftk $TEMPDIR/odd.pdf burst output $TEMPDIR/pg%04d_A.pdf
    pdftk $TEMPDIR/even.pdf burst output $TEMPDIR/pg%04d_B.pdf
    
    # ... and recombine them, "_A" and "_B" suffixes leading to the correct order
    pdftk $TEMPDIR/pg*.pdf cat output $2
    
    # cleanup
    rm -r $TEMPDIR

  3. #3
    Join Date
    Sep 2009
    Beans
    1

    Re: pdftk usage question

    Thanks for the script, I had the same problem.

    I suggest du use evenD instead of evenS since it will rotate relatively by 180 degrees.

  4. #4
    Join Date
    May 2006
    Beans
    Hidden!

    Re: pdftk usage question

    Thank you, the script is very helpful. Here is one small modification that allows you to decide which direction the rotation is:

    Code:
    #/bin/bash
    # script for rotating all even pages of a document
    
    # enough arguments?
    if [ $# -ne 3 ]; then
             echo 1>&2 Usage: pdf-rotate-even <direction> input.pdf output.pdf
             exit 127
        fi
    
    # generate a temporary directory
    TEMPDIR=/tmp/pdf`date +%N`
    mkdir $TEMPDIR
    
    # rotate all even pages, keep the odd ones
    pdftk $2 cat 1-endodd output $TEMPDIR/odd.pdf
    pdftk $2 cat 1-endeven$1 output $TEMPDIR/even.pdf
    
    # split the files...
    pdftk $TEMPDIR/odd.pdf burst output $TEMPDIR/pg%04d_A.pdf
    pdftk $TEMPDIR/even.pdf burst output $TEMPDIR/pg%04d_B.pdf
    
    # ... and recombine them, "_A" and "_B" suffixes leading to the correct order
    pdftk $TEMPDIR/pg*.pdf cat output $3
    
    # cleanup
    rm -r $TEMPDIR

  5. #5
    Join Date
    Jun 2008
    Beans
    1

    Re: pdftk usage question

    Thanks a lot guys. I had the same kind of problem and it worked like a charm. This is exactly the reason why FOSS rules.

    I tweaked the script a little bit further to allow to specify particular rotation angles for odd *and* even pages. In case it might spare a few minutes to people in the same situation as I was, I share it here :

    Code:
    #/bin/bash
    # script for rotating all even pages of a document
    
    # enough arguments?
    if [ $# -ne 4 ]; then
             echo
             echo 1>&2 Usage: bash pdf-rotate-odd-even.sh input.pdf direction_odd direction_even output.pdf
             echo
             exit 127
        fi
    
    # generate a temporary directory
    TEMPDIR=/tmp/pdf`date +%N`
    mkdir $TEMPDIR
    
    # rotate all even pages, keep the odd ones
    pdftk $1 cat 1-endodd$2 output $TEMPDIR/odd.pdf
    pdftk $1 cat 1-endeven$3 output $TEMPDIR/even.pdf
    
    # split the files...
    pdftk $TEMPDIR/odd.pdf burst output $TEMPDIR/pg%04d_A.pdf
    pdftk $TEMPDIR/even.pdf burst output $TEMPDIR/pg%04d_B.pdf
    
    # ... and recombine them, "_A" and "_B" suffixes leading to the correct order
    pdftk $TEMPDIR/pg*.pdf cat output $4
    
    # cleanup
    rm -r $TEMPDIR

  6. #6
    Join Date
    Nov 2008
    Beans
    14

    Re: pdftk usage question

    Dear all,

    I have successfully used your script and wanted to thank you.

    Maybe using the mktemp -d command to create the temporary directory would be better.

  7. #7
    Join Date
    Jul 2007
    Location
    Magic City of the Plains
    Beans
    Hidden!
    Distro
    Xubuntu 18.04 Bionic Beaver

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •