Combine multiple PDFs into multiple PDFs


I'm trying to take a set of PDFs (that I'll have already separated into one page files) that I want to combine into multiple PDFs. As a teacher, I want to be able to scan a stack of tests into one big PDF file, split that into single pages (I already have the Automator service for that), and then combine the results into a separate file for each student. So if I had 10 students and a 4 page test, I'd want to be able to combine those 40 files into 10 separate PDFs, ideally labeled by student name (if there was a way for me to input that list of names into Automator). Worst case scenario, I just run a combinePDF service for each individual student, but I didn't know if there was a way I could do that all in one fell swoop?

Thanks for your help!

Best Answer

I'd use the third-party command line tool, cpdf, and script the process of creating the separate PDF documents for each student.

Have a look at: Coherent PDF Command Line Tools Community Release

  • Download pre-built cpdf tool (This is a direct download link. Building from source is available from link on main page linked above.)
  • A comprehensive user manual for the PDF tools can be found as a PDF document, or browsable online.
  • Note: the Community Release version of Coherent PDF Command Line Tools is free.

There is no need to first extract the the scanned PDF document into individual files to then combine them as the following example shell script code extracts the pages from the scanned PDF document four pages at a time while naming each new PDF document for each student:


    # Replace e.g. "Student 01", etc. with the students name
    # in the order required as scanned in the PDF document.

my_students=("Student 01" "Student 02" "Student 03" "Student 04" "Student 05" "Student 06" "Student 07" "Student 08" "Student 09" "Student 10")

    # Replace "/path/to/scanned.pdf" with the 
    # actual pathname of the scanned document.


    # NOTE: If cpdf is not in your PATH, then use its fully
    #       qualified pathname, e.g: /use/local/bin/cpdf

for this_student in "${my_students[@]}"; do
    cpdf "${scanned_document}" $x-$y -o "${this_student}.pdf"
    x=$(( x + 4 ))
    y=$(( y + 4 ))

To use this shell script in Terminal, first run the following compound command:

f="processpdf"; touch "$f"; open -e "$f"; chmod u+x "$f"

Then in the document that opens, TextEdit by default, copy and paste the example shell script code into it, make the appropriate edits, and then save it.

It can now be used in Terminal using e.g. ./processpdf or /path/to/processpdf or if placed in a location that's within the PATH, then simply, e.g.: processpdf

Note: As coded the new PDF documents are created in the PWD in Terminal. You can add e.g. /path/to/ to "${this_student}.pdf", e.g., "/path/to/${this_student}.pdf" if you want.

If you need/want to apply this to a Run Shell Script action in Automator, instead of regular shell script usage, let me know an I can update the answer.

In either case, if you want to pass the script a file with the names of the students, instead of using an array, as presently coded, that is doable too.

Note: The example shell script code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. As an example, as presently coded, the new PDF documents will overwrite existing documents of the same name without warning.