Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Aros888

macrumors newbie
Original poster
Mar 30, 2012
25
15
Chicago, IL
Hi guys! I'm a student and I use a scanner to take paper copies of handouts, etc. and turn them into PDFs. However, I only have a pretty cheap printer/scanner all in one, and while I can load dozens of pages at a time (thank god) it can only scan one side of double-sided documents.

I can manually merge two PDFs, one with the front sides and one with the back sides, in preview, but this is extremely time consuming for many page documents.

Is there any tool that can merge PDFs along a pattern something like "Take the first page of the first PDF and then the last page of the second PDF. Then the second page of the first followed by the second to last of the second..." so that I can automate this process? Or do any of you know a quicker way?

Thanks!

Note: I do have a windows side, so if any of you know software that works on Windows, that would also be fine.
 
Is there any tool that can merge PDFs along a pattern something like "Take the first page of the first PDF and then the last page of the second PDF. Then the second page of the first followed by the second to last of the second..."

CPDF and the following script ought to do it

First up is things that you'll need to do once:

1 Make a folder on your desktop, call it PDFMERGE

2 Download CPDF community edition from https://github.com/coherentgraphics/cpdf-binaries/archive/master.zip

3 inside that archive should be an OSX-Intel folder, copy the cpdf file from inside that folder over to the PDFMERGE folder you've made on your desktop

4 Now open the OSX Automator app

5 tell Automator you want to create a new application

6 in the left hand Automator pane select Utilities

7 In the middle Automator pane, find Run Shell Script and drag that to the big right hand pane

8 you should have a RunShell Script box in the big right hand pane, that has a text entry box with nothing but the word 'cat' in it. Delete the word cat and copy n paste the following into that box

Code:
#!/bin/bash
cd ~/Desktop/PDFMERGE
PAGES=$(./cpdf -pages even.pdf)
./cpdf -split odd.pdf -o tempodd%%%.pdf
./cpdf -split even.pdf -o tempeven%%%.pdf
j=1;
for i in tempodd*.pdf;
do
if (("$j" < 10))
then
	mv $i doc_00$j.pdf
else
	if (("$j" <100))
	then
		mv $i doc_0$j.pdf
	else
		mv $i doc_$j.pdf
	fi
fi
j=$((j+2));
done
j=$((PAGES*2));
for i in tempeven*.pdf;
do
if (("$j" < 10))
then
	mv $i doc_00$j.pdf
else
	if (("$j" <100))
	then
		mv $i doc_0$j.pdf
	else
		mv $i doc_$j.pdf
	fi
fi
j=$((j-2));
done
./cpdf  doc_*.pdf -remove-duplicate-fonts -o merged.pdf
rm doc_*.pdf

9 Now save the new automator app (anywhere you like e.g. your desktop)


Anytime you want to use it:


1 take your pdf file that's got first, third, fifth etc pages in it and place a copy of it into the PDFMERGE folder on your desktop. Rename that copy to odd.pdf

2 Next take the pdf thats got the reverse ordered second, fourth, sixth etc pages in it and place a copy of it into PDFMERGE folder too. Rename that copy to even.pdf

3 run your automator app

4 Once its finished you should have a file called merged.pdf in the PDFMERGE folder, which has your two pdfs combined in the correct order.

Code above should be good for odd and even files up to 499 pages long each

What it does is strip your original odd and even pdf files into their individual pages. It renames all the odd pages 001 003 005 etc and then renames all the even pages 002 004 006 etc. Then it merges the whole lot back into the new merged.pdf

While its working it'll generate a temporary pdf for each page then clean up and delete all those at the end.

The bigger the individual pdfs, the more temporary space and time it'll take, so if you've an old mac mini with a spinning hard disc it might take a while but should still be a lot faster (and less tedious) than doing them by hand.

The only other thing to watch out for is the size of the merged file - ideally it wouldn't be much larger than the size of the two source pdfs combined. Let me know if its significantly bigger than that.
 
Last edited:
Thanks for the reply :) I like your solution. I've never used Automator before, but I think I'll try to familiarize myself with it and maybe tweak your code slightly.

Again, thanks!
 
CPDF and the following script ought to do it

First up is things that you'll need to do once:

1 Make a folder on your desktop, call it PDFMERGE

2 Download CPDF community edition from https://github.com/coherentgraphics/cpdf-binaries/archive/master.zip

3 inside that archive should be an OSX-Intel folder, copy the cpdf file from inside that folder over to the PDFMERGE folder you've made on your desktop

4 Now open the OSX Automator app

5 tell Automator you want to create a new application

6 in the left hand Automator pane select Utilities

7 In the middle Automator pane, find Run Shell Script and drag that to the big right hand pane

8 you should have a RunShell Script box in the big right hand pane, that has a text entry box with nothing but the word 'cat' in it. Delete the word cat and copy n paste the following into that box

Code:
#!/bin/bash
cd ~/Desktop/PDFMERGE
PAGES=$(./cpdf -pages even.pdf)
./cpdf -split odd.pdf -o tempodd%%%.pdf
./cpdf -split even.pdf -o tempeven%%%.pdf
j=1;
for i in tempodd*.pdf;
do
if (("$j" < 10))
then
    mv $i doc_00$j.pdf
else
    if (("$j" <100))
    then
        mv $i doc_0$j.pdf
    else
        mv $i doc_$j.pdf
    fi
fi
j=$((j+2));
done
j=$((PAGES*2));
for i in tempeven*.pdf;
do
if (("$j" < 10))
then
    mv $i doc_00$j.pdf
else
    if (("$j" <100))
    then
        mv $i doc_0$j.pdf
    else
        mv $i doc_$j.pdf
    fi
fi
j=$((j-2));
done
./cpdf  doc_*.pdf -remove-duplicate-fonts -o merged.pdf
rm doc_*.pdf

9 Now save the new automator app (anywhere you like e.g. your desktop)


Anytime you want to use it:


1 take your pdf file that's got first, third, fifth etc pages in it and place a copy of it into the PDFMERGE folder on your desktop. Rename that copy to odd.pdf

2 Next take the pdf thats got the reverse ordered second, fourth, sixth etc pages in it and place a copy of it into PDFMERGE folder too. Rename that copy to even.pdf

3 run your automator app

4 Once its finished you should have a file called merged.pdf in the PDFMERGE folder, which has your two pdfs combined in the correct order.

Code above should be good for odd and even files up to 499 pages long each

What it does is strip your original odd and even pdf files into their individual pages. It renames all the odd pages 001 003 005 etc and then renames all the even pages 002 004 006 etc. Then it merges the whole lot back into the new merged.pdf

While its working it'll generate a temporary pdf for each page then clean up and delete all those at the end.

The bigger the individual pdfs, the more temporary space and time it'll take, so if you've an old mac mini with a spinning hard disc it might take a while but should still be a lot faster (and less tedious) than doing them by hand.

The only other thing to watch out for is the size of the merged file - ideally it wouldn't be much larger than the size of the two source pdfs combined. Let me know if its significantly bigger than that.

I know it's been a while since this was posted, but wondering if anyone has had any success with this? I tried the instructions given (thanks you!), and it seems to split and merge the pages as described, but instead of 1,2,3, etc, it is ordering it 1, last page of evens, 3, second to last page of evens, etc. Does anyone know how to tweak this script or know what I've done wrong? Thanks in advance.
 
I know it's been a while since this was posted, but wondering if anyone has had any success with this? I tried the instructions given (thanks you!), and it seems to split and merge the pages as described, but instead of 1,2,3, etc, it is ordering it 1, last page of evens, 3, second to last page of evens, etc. Does anyone know how to tweak this script or know what I've done wrong? Thanks in advance.


Hi

It's pretty simple.

You just need to change the lines :
Code:
j=$((PAGES*2));
to
Code:
j=2;

and get it to count up

Code:
j=$((j-2));
to
Code:
j=$((j+2));


I fiddled about a bit and came up with this.
It lets you choose whether to join odd or evens reversed or not.

Do the same as above instructions, but create an automater flow with 2 parts: (i may have attached it i think?)

1. Run Applescript and paste the following in
Code:
on run

    choose from list {"Evens Reversed", "Odds Reversed"} with prompt "Please make your selection" with multiple selections allowed and empty selection allowed

    return the result as string

end run

and then

2 run shell script (Choose pass input as arguments)

with this code.

Code:
#!/bin/bash
cd ~/Desktop/PDFMERGE

PAGESODD=$(./cpdf -pages odd.pdf)
PAGESEVEN=$(./cpdf -pages even.pdf)
./cpdf -split odd.pdf -o tempodd%%%.pdf
./cpdf -split even.pdf -o tempeven%%%.pdf


j=1;
flage=0
flago=0
case "$1" in 
  *Odd*)
    j=$((PAGESEVEN*2));
    j=$((j-1));
    flago=1;
    ;;
esac


for i in tempodd*.pdf;
do
if (("$j" < 10))
then
    mv $i doc_00$j.pdf
else
    if (("$j" <100))
    then
        mv $i doc_0$j.pdf
    else
        mv $i doc_$j.pdf
    fi
fi

if (("$flago" == 0))
then
    j=$((j+2));
else
    j=$((j-2));
fi
done
j=2;

case "$1" in 
  *Even*)
    j=$((PAGESEVEN*2));
    flage=1;
    ;;
esac
for i in tempeven*.pdf;
do
if (("$j" < 10))
then
    mv $i doc_00$j.pdf
else
    if (("$j" <100))
    then
        mv $i doc_0$j.pdf
    else
        mv $i doc_$j.pdf
    fi
fi
if (("$flage" == 0))
then
    j=$((j+2));
else
    j=$((j-2));
fi
done
./cpdf  doc_*.pdf -remove-duplicate-fonts -o merged.pdf
rm doc_*.pdf
 

Attachments

  • even.pdf
    17.8 KB · Views: 252
  • odd.pdf
    17.8 KB · Views: 241
  • pdfmerge.app.zip
    1.5 MB · Views: 241
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.