Recently a customer asked that we publish his scanned parcel maps to his Intranet. For those not up on decade old Internet lingo "an Intranet is a private computer network that uses Internet protocols". He wanted everyone in the office to be able to view and print the scanned TIFF parcel maps. Easy enough, just create share on the file server, or a virtual directory on his web server that points to the directory of scanned images.
This was not exactly what he wanted. Windows XP provides a TIFF image viewer in the form of the “Windows Picture and Fax viewer”, however it’s slow, its zoom and pan capabilities are lacking (no zoom window) and printing is limited to “print to fit”. There’s also Irfanview , good for image batch conversion but I really don’t like the viewer.
Enter the Adobe PDF viewer. Adequate zoom and pan, fast, easy printing to original scanned size as well as other sizes, and you can zoom to an area and print just the zoomed area. I converted a few using tiff2pdf from the LibTiff utilities. Not too hard, install the LibTif windows binaries as well as the jpeg an zlib dll files. Add them to the PATH and go to work.
tiff2pdf -o map.pdf map.tif
Ok, so lets convert all 4000 of the TIFF files to PDF. Gosh there must be some way to batch this process. OK, there is. For each directory:
for %f in (*.tif) do; tiff2pdf -o %~nf.pdf %f
Notice the “%~nf”? It’s replaced by the base name for the file. Therefore %~nf.pdf is replaced by map100.pdf when the TIFF file is map100.tif. That’s better but the new PDF files landed in the dame directory. A small change fixes that:
for %f in (*.tif) do; tiff2pdf -o \\SERVER\\share\%~nf.pdf %f
or if you have mapped a drive:
for %f in (*.tif) do; tiff2pdf -o X:\dir\%~nf.pdf %f
Those who are fluent in shell scripting need not comment, I’m talking to the Windows guys who have lived their entire existence by clicking somewhere.
So far so good but I have 300 directories. We could use the /D and /R parameters of the for command to recurse a directory but I wanted more control. The following does not work:
>for /r %f in (*.tif) do tiff2pdf -o .\pdfdir%~pf%~nf.pdf %f
Since the directory does not exist tiff2pdf can’t create the output file. So what now?
Scripting to the Rescue
In a perfect world we would have a good UNIX/Linux shell (bash for instance) on our Windows PCs. And we can, just install Cygwin or if you need only the bash shell (unlikely) it’s available for Windows individually.
But we do have scripting. Windows Script Host provides VBScript and Javascript. We can also download and install a number of other scripting languages including Perl, Python, and others.
A few years ago I found a little VBScript program named span.vbs that does just this. I used it for a few tasks over the following months but soon found that I needed more flexibility.
I find writing in VB and VBscript a problem. Once I get my brain used to a language (I code in Java and C# regularly) I find it hard to switch to a language syntactically different as VBscript. Therefore I converted the VBS code to JavaScript then made my improvements.
The result is spanbase.js. With this program I can batch process an entire directory of files.
cscript [path/to]spanbase.js "[Command] [parameters]" [Regex] [NewExt] [NewBaseDir]
Command: Command string to pass to command interpreter
Regex: Regular expression pattern to match filename (e.g. tif$)
!!!! Make sure to place quotes around the command and parameters !!!!
The following substitutions will be made before execution
@File - the current filename
@Ext - the current file extension
@Base - the basename of the current file
@Newfile - a filename compose of the basename + NewExt
NewExt: The new extension to use for the new file
NewBase: New Base Directory for new file, this will be prepended to the file path.
this will be appended to the basename of the original filename (e.g. t.jpg)
The solution
cscript C:\wsh\spanbase.js "tiff2pdf -o @Newfile @File" tif$ pdf MyPdfDir
Run this script from the topmost directory containing your tiff images (e.g. C:\tiff) the new PDF files will be placed in the directory C:\tiff\MyPdfDir and the file C:\tiff\spanbase.log will contain each directory entered as well as program output and program error messages.
