Merging Multiple PDFs by Date on Windows

Tuesday, January 25 2005 @ 10:11 AM PST

Contributed by: Admin

Pdftk is a command-line program, so it helps to know command-line (a/k/a "shell") programming. Especially when you want to do something that pdftk doesn't know how to do by itself.

A good example of this is using pdftk to combine PDF in a special order. This article describes how to use the Windows Command Prompt batch language to combine PDFs by file creation date. I also show how to do this using the bash shell which comes with MSYS.

Start by downloading pdftk and installing it as described here.

Are all of your PDFs in a single directory on your hard disk? If not, copy them into a single directory. We'll call it C:mypdfs

Open a command prompt by selecting Start > Programs > Accessories > Command Prompt. Then change into C:mypdfs by typing:

  cd /D C:mypdfs

and pressing Enter. The command prompt should change to reflect your new location. It should look like this, now:

  C:mypdfs>

Now, how do you want all of these PDFs ordered in your final PDF? If their alphabetical order is what you want, then run pdftk like so:

  pdftk *.pdf cat output combined.pdf

If you want them ordered by date (oldest to newest), then it's more complicated. On my Windows 2000 machine, you can do it like this. NOTE that you must type a space after set LIST=, e.g.: "set LIST= ".

  cmd /V
  set LIST=
  for /F "delims=n" %i in ('dir /B /O:D *.pdf') do set LIST=!LIST! "%i"
  pdftk %LIST% cat output combined.pdf
  exit

If you want to reverse the order, use 'dir /B /O:-D *.pdf' instead of 'dir /B /O:D *.pdf'.

Using the bash shell that comes with MSYS (instead of the Windows Command Prompt) on Windows, this task is easier:

  eval pdftk `ls -tQ *.pdf` cat output combined.pdf

Reverse order (oldest to newest):

  eval pdftk `ls -trQ *.pdf` cat output combined.pdf

Note that the bash example uses backtick characters, and the Windows Command Prompt example uses single quotes. In both cases, the quoted text is executed by the shell inline, and the output is processed by the script.

I hope this gives you a taste of what's possible with shell programming; these examples hardly scratch the surface. I prefer bash shell programming, and I use MSYS to do this on Windows. Bash shell scripts are also more portable; they also work on Linux and Mac OS X.

Also See:

10 comments



http://www.accesspdf.com/article.php/20050125101159870