Fancy merging of PDFs I have 6 PDF files being the result of splitting multiple presentations in PDFs (which had 6 slides on each page). So essentially, now I have top-left pages of all the presentations in one file, top-right in another, middle-left in a file and so on...

That's basically how every initial presentation looked like:

page 1
 ___________________
|  ______   ______  |
| |slide1| |slide2| |
| |______| |______| |
|  ______   ______  |
| |slide3| |slide4| |
| |______| |______| |
|  ______   ______  |
| |slide5| |slide6| |
| |______| |______| |
|___________________|

page 2
 ___________________
|  ______   ______  |
| |slide7| |slide8| |
| |______| |______| |
.....................


Q: How can I merge them together to maintain the order of the initial presentations?

I kind of have to do the following:

for index in range(number_of_pages_in_each_pdf):
   final_pdf.append(pdf1[index])
   final_pdf.append(pdf2[index])
   final_pdf.append(pdf3[index])
   final_pdf.append(pdf4[index])
   final_pdf.append(pdf5[index])
   final_pdf.append(pdf6[index])


There is around a hundred pages in each of those six PDFs, so rearraging by hand is out of question.
Eventually, I'm answering my own question, because I finally found a way and I want to leave the complete solution here, in case someone struggles with a similar problem in the future.

The solution involves an amazing program called Poppler available through Homebrew, by brew install poppler. Poppler comes with a variety of pdf management tools, like pdfseparate and pdfunite featured in the script.

Without further ado, here's the script that I wrote:

#!/bin/bash

mkdir ~/Desktop/merging

# number of pdf files containing pages nr 1/2/3/...
n=6

for i in $(seq 1 $n); do
    # ${i}.pdf being a file containing pages nr i
    pdfseparate ~/Desktop/${i}.pdf ~/Desktop/merging/${i}_%d.pdf
done

number_of_pages=$(cd ~/Desktop/merging/; ls | wc -l)
number_of_pages_each=$((number_of_pages/n))

command="pdfunite"

for i in $(seq 1 ${number_of_pages_each}); do
    for j in $(seq 1 $n); do
        command="$command ~/Desktop/merging/${j}_${i}.pdf"
    done
done

command="$command ~/Desktop/output.pdf"

eval $command

# cleanup
rm -rf ~/Desktop/merging