Saturday, April 3, 2010

Merging pdf files

In the previous post I discussed how to write some basic shell scripts that allow you to do many of the repetitive tasks automatically instead of typing the commands into the terminal manually.

Well, one of the tasks that I very often find myself doing is merging multiple pdf files into a single pdf file. The very fact that you can merge multiple pdf files using linux is itself a very nice thing. Better still, you can automate it by writing a script.

Below is the command that you would generally enter into a terminal. Of course, you need the software called ghostscript installed. After installing gs, collect all pdf files that you want to merge into a single folder and execute the following command in the terminal. The code given below is adapted from [1]

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=mergedAll.pdf -dBATCH *.pdf 

Once executed, you will have the resulting merged pdf file in mergedAll.pdf. Now, if you want to repeat the above process, make sure you delete/rename mergedAll.pdf and then execute the above command again.

A few more niceties that I like to have in my script besides the above command is to first automatically delete/rename any file called mergedAll.pdf so that I can keep adding new files into the folder and merging them. Secondly I also like to delete/move all previous pdf files so that I have only the merged pdf file in the folder.

Here is the script, to achieve all of the above :

a) For deleting all pdf files after merging,

mv mergedAll.pdf merged.pdf
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=mergedAll.pdf -dBATCH *.pdf 
mv mergedAll.pdf mergedAll
rm *.pdf
mv mergedAll mergedAll.pdf

b) For moving all pdf files into a subfolder after merging,

mv mergedAll.pdf merged.pdf
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=mergedAll.pdf -dBATCH *.pdf 
mv mergedAll.pdf mergedAll
mv *.pdf pdf
mv mergedAll mergedAll.pdf


[1] http://www.everyjoe.com/newlinuxuser/merge-multiple-pdfs-into-one-file

0 comments:

Post a Comment