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 :-)

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=$(( w210 )) h2px=$(( h210 )) 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=&quot;$( echo &quot;$f&quot; | cut -f 1 -d '.')_1.pdf&quot;
urf=&quot;$( echo &quot;$f&quot; | cut -f 1 -d '.')_2.pdf&quot;
dlf=&quot;$( echo &quot;$f&quot; | cut -f 1 -d '.')_3.pdf&quot;
drf=&quot;$( echo &quot;$f&quot; | cut -f 1 -d '.')_4.pdf&quot;

# Up-Left page
echo &quot;... into page ${ulf}&quot;
gs -o ${ulf} -sDEVICE=pdfwrite -g${w2px}x${h2px} -c &quot;&lt;&lt;/PageOffset [0 -${h2}]&gt;&gt; setpagedevice&quot; -f ${f}

# Up-Right page
echo &quot;... into page ${urf}&quot;
gs -o ${urf} -sDEVICE=pdfwrite -g${w2px}x${h2px} -c &quot;&lt;&lt;/PageOffset [-${w2} -${h2}]&gt;&gt; setpagedevice&quot; -f ${f}

# Down-Left page
echo &quot;... into page ${dlf}&quot;
gs -o ${dlf} -sDEVICE=pdfwrite -g${w2px}x${h2px} -c &quot;&lt;&lt;/PageOffset [0 0]&gt;&gt; setpagedevice&quot; -f ${f}

# Down-Right page
echo &quot;... into page ${drf}&quot;
gs -o ${drf} -sDEVICE=pdfwrite -g${w2px}x${h2px} -c &quot;&lt;&lt;/PageOffset [-${w2} 0]&gt;&gt; setpagedevice&quot; -f ${f}

echo &quot;... done!&quot;
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!"