0

I am struggling to revert / undo a 4 pages per sheet PDF to a 1 sheet per page document.

The reason why I am trying to undo it is because I can barely read the printed document. The original file has been supplied as is and I have no mean to print it in a comfortable format.

Almost all the topics I found by searching for "PDF handout" and for "PDF multiple pages" offer to assemble multiple pages into a single sheet of paper. I am trying to proceed the exact opposite way.

The sheets are arranged follows:

+----------------+
| Page 1  Page 2 |
| Page 3  Page 4 |
+----------------+

I would like to turn them to:

+--------+
| Page 1 |
+--------+
| Page 2 |
+--------+
| Page 3 |
+--------+
| Page 4 |
+--------+

Any idea or suggestion to do it?

I thank you for your help :-)

Sebastien
  • 13
  • 1
  • 4

1 Answers1

0

I finally found a former thread (Thanks to Chris Hill) which helped me at writing the following script. Which answers my question :-)

#!/bin/bash
#
# Usage ./<script name>.sh <source file>.pdf
#
# Slices each page from the <source> into 4 pages; then,
# Reassemble the slices into output.pdf; and, finally,
# Clean up the mess
#

echo "Working directory creation ..."
mkdir -p tmp
echo "... done!"
echo

# Split source into multiple files
echo "Splitting source ..."
cd tmp
pdftk "$1" burst
echo "... done!"
echo

# Slice each page into multiple files
echo "Reading doc_data.txt ..."
pw=`cat doc_data.txt  | grep PageMediaDimensions | head -1 | awk '{print int($2)}'`
ph=`cat doc_data.txt  | grep PageMediaDimensions | head -1 | awk '{print int($3)}'`
w2=$(( pw / 2 ))
h2=$(( ph / 2 ))
w2px=$(( w2*10 ))
h2px=$(( h2*10 ))
echo "... done!"
echo

for f in  pg_[0-9]*.pdf ; do
    echo "Slicing file: ${f} ..."

    # Name outputs so that they can be reassembled in order
    ulf="$( echo "$f" | cut -f 1 -d '.')_1.pdf"
    urf="$( echo "$f" | cut -f 1 -d '.')_2.pdf"
    dlf="$( echo "$f" | cut -f 1 -d '.')_3.pdf"
    drf="$( echo "$f" | cut -f 1 -d '.')_4.pdf"

    # Up-Left page
    echo "... into page ${ulf}"
    gs -o ${ulf} -sDEVICE=pdfwrite -g${w2px}x${h2px} -c "<</PageOffset [0 -${h2}]>> setpagedevice" -f ${f}

    # Up-Right page
    echo "... into page ${urf}"
    gs -o ${urf} -sDEVICE=pdfwrite -g${w2px}x${h2px} -c "<</PageOffset [-${w2} -${h2}]>> setpagedevice" -f ${f}

    # Down-Left page
    echo "... into page ${dlf}"
    gs -o ${dlf} -sDEVICE=pdfwrite -g${w2px}x${h2px} -c "<</PageOffset [0 0]>> setpagedevice" -f ${f}

    # Down-Right page
    echo "... into page ${drf}"
    gs -o ${drf} -sDEVICE=pdfwrite -g${w2px}x${h2px} -c "<</PageOffset [-${w2} 0]>> setpagedevice" -f ${f}

    echo "... done!"
    echo
done

# Merging sliced page into a single PDF file
echo "Reassembling the PDF file ..."
ls -1 pg_[0-9]*_[1-4].pdf > fl
pdftk $( cat fl ) cat output output.pdf
echo "... done!"
echo

# Cleaning up
echo "Cleaning up ..."
mv output.pdf ..
cd ..
rm -fr tmp
echo "... done!"
echo

echo "It's all done ... you lucky man!"
Sebastien
  • 13
  • 1
  • 4