As part of
West Wind Web Connection I have a few samples that generate PDF documents automatically. Although there are a number of different ways to generate PDF output, the one most people use ends up using either Distiller or Ghostscript from a Post script file.
In order to do this though it's a requirement that you actually have a PostScript printer installed on your machine and this becomes an installation hassle. So, it would be nice to automate this process and install a printer driver programmatically. While programmatically is stretching this solution a bit, it turns out that Windows has a utility that allows automating many of the printer installation tasks through the PrintUi.dll utility.
You can run:
rundll32 printui.dll,PrintUIEntry /?
To get the full functionality listing which includes adding and deleting printers both through the Wizards or through non-interactive installs. You can install printers locally or over the network, display status, print test pages, bring up property pages and more.
I ran into a Web board post here that describes some of the options and provides a few examples that make for a great quick start:
http://www.codeguru.com/forum/showthread.php?t=220033&goto=nextoldest
There's also a TechNet entry here:
http://support.microsoft.com/kb/q189105/
For my purpose I want to install a generic PostScript driver for which I've used the Apple Color LW 12/660 PS because it's one that's available on most Windows versions as one of the built in drivers. I was actually thrown by the examples provided in the first article because it takes two steps to register a printer: Load the Printer driver and then install the driver to the local Printers installation.
To register a new printer with the name of Apple Postscript Driver you can use the following (in a batch file or individually):
rundll32 printui.dll,PrintUIEntry /ia /m "Apple Color LW 12/660 PS" /h "Intel" /v "Windows 2000 or XP" /f %windir%\inf\ntprint.inf /u
rundll32 printui.dll,PrintUIEntry /if /b "Apple Postscript Driver" /f "%windir%\inf\ntprint.inf" /r "lpt1:" /m "Apple Color LW 12/660 PS"
Works great and provides minimal fuss. To do this programmatically all you need to do is run a Shell process to execute the above commands or execute a batch file. I created a simple batch file called installprinter.bat that does basically this:
rundll32 printui.dll,PrintUIEntry /ia /m "%1" /h "Intel" /v "Windows 2000 or XP" /f %windir%\inf\ntprint.inf /u
rundll32 printui.dll,PrintUIEntry /if /b "%2" /f "%windir%\inf\ntprint.inf" /r "lpt1:" /m "%1"
Note you need to change the server type for Windows NT or Windows 98 systems with the /v flag. The CodeGuru link has a few examples of this and the /? flag describes the various options available when you run it.
Very cool! I'm just bummed it took me so long to find this.
Other Posts you might also like