As can be seen, the extraction will be done with pdftk (more info here). Now, these commands are simply printed to the standard output. Here is how to execute them too:

./extract.py | sh

That is, pass the commands to the shell “sh”, which will execute them line by line.

Okay, now we have the pieces in the directory “pieces”. Enter the directory “pieces” and join the PDFs:

pdftk *.pdf cat output all.pdf

Known issue

Well, to tell the truth, this method will produce a huge single PDF. The extracted pieces are also very big (5 to 10 MB), and the final PDF is about 130 MB! So actually I used Adobe Acrobat 8 Professional to merge the pieces with the conversion setting “Smaller File Size”. Acrobat Pro optimized the files and produced a file of size 6 MB. If you know how to have a similar result with open source tools, let me know.