0

I'm trying to write a simple script / automator tool that should achieve the following: I have 600 Pdf-Files in one folder and 600 Pdf-Files with the identical names in a different folder. Now I would like to combine the matching files with each other, so that I get 600 merged Pdf-files as a result. Can this be achieved?

4
  • 2
    Yes, that would be possible. What have you done so far? because this is not a free coding service.
    – Solar Mike
    May 30, 2021 at 12:35
  • Which part are you struggling with, combining two pdfs into one (there are similar questions with answers already on the site), looping through all files in one folder and finding the matching one in the other folder, or something else?
    – nohillside
    May 30, 2021 at 12:38
  • Thank you for your replies! I specifically have a problem to get the script to match the correct files. I figured out how to combine two files and save them to the right location but I can’t figure out how to match the correct files. (I‘m an absolute beginner)
    – 4gd4tfgg4
    May 30, 2021 at 14:15
  • 1
    Please add the script (or a screenshot thereof) of what you have so far. It's way easier to propose ways to improve an existing script than to do everything from scratch.
    – nohillside
    May 30, 2021 at 14:19

1 Answer 1

0

I'd use the third-party command line tool, cpdf, and script the process of merging the PDF documents.

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 fo non-commercial use.

Copy the appropriate cpdf binary executable from the extracted cpdf-binaries-master.zip file to a directory that is in the PATH passed to the shell.



To use the example shell script code:

In Terminal, run the following compound command:

f="mergepdf"; touch "$f"; open -e "$f"; chmod 0755 "$f"

In the opened mergepdf document, copy and paste the example shell script code, shown below, into the document, and then save and close the document.

  • Note: If you did don't copy the cpdf binary executable to a directory that is in the PATH passed to the shell, then use the fully qualified pathname of the cpdf binary executable in the shell script.

Back in Terminal:

./mergepdf

Outputs:

Parameter missing!...
Example: mergepdf '/path/to/PDF Files 1' '/path/to/PDF Files 2' '/path/to/Merged PDF Files'
  • Note: I'd move the mergepdf file to a location that's within the PATH passed to shell scripts, then you'd not have to preface the command with ./, or include it's fully qualified pathname.


Example shell script code:

#!/bin/bash

if [ -z "${3}" ]; then
    echo "Parameter missing!..."
    echo "Example: mergepdf '/path/to/PDF Files 1' '/path/to/PDF Files 2' '/path/to/Merged PDF Files'"
    exit 1
fi  

oridir="${1}"
dupdir="${2}"
merdir="${3}"

if [ ! -d  "${oridir}" ]; then
    echo "The directory \"${oridir}\" does not exist!..."
    exit 1
fi

if [ ! -d  "${dupdir}" ]; then
    echo "The directory \"${dupdir}\" does not exist!..."
    exit 1
fi

if [ ! -d  "${merdir}" ]; then
    echo "The directory \"${merdir}\" does not exist!..."
    exit 1
fi

while IFS= read -r orifile; do

    while IFS= read -r dupfile; do

        if [ -e "${dupfile}" ]; then
            if [ ! -e "${merdir}/${orifile##*/}" ]; then
                cpdf "${orifile}" "${dupfile}" -o "${merdir}/${orifile##*/}" 2>/dev/null
            else
                echo "\"${orifile##*/}\" already exists in \"${merdir}\"."
            fi
        else
            echo "No match found for \"${orifile}\" in \"${dupdir}/\"."
        fi

    done <<< "$(find "${dupdir}" -type f -name "${orifile##*/}")"

done < <(find "${oridir}" -type f -iname '*.pdf')

What the shell script does:

Finds of all files with a pdf extension in '/path/to/PDF Files 1' and then attempts to find the file of the same name in '/path/to/PDF Files 2'.

If it finds the file of the same name in '/path/to/PDF Files 2' it merges the two files with a pdf extension having the same name to '/path/to/Merged PDF Files', merging the second file to the end of the first file. Note that other command line options for the cpdf binary executable are available.

If a duplicate .pfd document of the same name is not found in '/path/to/PDF Files 2', then a message it output.

As coded, existing files in '/path/to/Merged PDF Files' are not overwritten. If a file exists a message is output.



Notes:

Because the cpdf binary executable is not signed, you'll have to allow it to run by clicking the [Allow Anyway] button in: System Preferences > Security & Privacy > General

The example shell script code, shown above, was tested as an executable shell script in Terminal under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate setting in System Preferences > Security & Privacy have been set/addressed as needed.

Testing under macOS Big Sur 11.4, I did not have any issue with the cpdf binary executable concerning its signing and it simply worked by copying the OSX-Intel version of cpdf to a location that's within the PATH passed to shell scripts.



Note: The example shell script code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .