NOTE: This is an older article, so the information provided may no longer be accurate.

Windows Python Network Printing Woes

I like to use and broaden my understanding of several different programming and scripting languages whenever I can. Afterall, the more tools you have in your toolbox, the better, right? Well, I have a customer who wanted a special type of report system for their office. The VB 6 app they were using previously was also written by me, but many many years ago, and I wasn't about to use VB 6 again to make the necessary updates. VB 6 is dead...let's leave it buried, where it belongs. What's cross-platform and doesn't need a bunch of frameworks or libraries installed? A web app!


God loves geeks too!
Why Jesus?


I'm not trying to make this a long article, so I'll get down to the nitty gritty: The customer's server is running Windows 7 Pro. I set up an HTML front-end for this new report system delivered with PHP on Apache. They wanted the web app to print to a printer being shared on a Windows XP Pro box elsewhere on their network. Easy, right?, not when you're running Apache and starting a Python script through PHP with an exec(); call.

Long-story-short, if you want to use a Python function like: DC.CreatePrinterDC("\\\\workstation\\hpinkjet") it's not going to work 'out of the box' if you're calling the Python script from PHP via Apache on a post-Vista system. I found the only way I could get it to work is by starting the Apache service as a normal user login instead of the system service login, probably because of Vista/Windows 7's 'session 0 isolation'.

Since this customer's system isn't on a public-facing web-server, I'm not overly concerned about attacks, so changing to a user login instead of the system service login may have security implications if you're running on a public-facing box, but I thought I would share my findings with everyone in case someone else out there is pounding their head against the wall, trying to get Python network printing to work in this kind of situation.

Update 1 - 2012-03-13
In my rush to publish this (actually, because the baby was needing my attention :-P), I forgot to mention a couple of things. First, here is the basic flow of this report system:
[Web page data entry]-->[PHP]-->[Python - print engine]
Because my main OS is Linux, I do nearly all of my Windows development in Windows XP virtual machines, so when I ran the report system on XP, printing was fine and I didn't have problems. When I installed everything on the customer's Windows 7 Pro box, everything worked fine except that no printing took place and there was a Python backtrace entry in the Apache error log that said 'Could not open printer' when it hit the CreatePrinterDC function.

After a long while of troubleshooting, including making sure the network printer I was trying to print to had the proper permissions, trying an IP address instead of a computer name, etc, I actually migrated all of the Python code to Windows API. Again, testing in XP worked perfectly, but again, when installed on the Windows 7 Pro box, no dice. The failure wasn't fun, but the Spirit directed me to open the Services screen and change the login for the Apache service to a normal user account instead of the normal Local System account, and printing works now! Yay! :-D

Update 2 - 2016-07-26
I recently had to set up this customer's web app all over again, but found that it wouldn't print again. I made sure that I was running Apache as a user account and that the remote printer had the right permissions set. Turns out I had forgotten to install the 'win32api' module for Python. If you have pip installed, you can install the necessary module like this:
   pip install pypiwin32
...or you can download it from:


Post A Comment

Your name:

Your e-mail address: (Will not be seen or used by anyone else but me)

To help cut down on spam, what do you get when you add two and four?:

Please type your message below: (Please limit message to less than 1,000 characters)


My Story   |   Business site   |   TGIF2   |   JWM   |   Openbox   |   Autism