Tuesday, May 22, 2012

As usual I post very few, because the time is very short, but has today I made an achievement I was looking to a long time, it deserves a HOW-TO...

Here it goes....

The Problem:

Our office only has one central printer!! (well it has other, but our boss don't wanna buy tonners, I warned him on the buy, that the 2000€ was better buy, but 600€ looked better in that moment.... no comments). This printer we use is a Windows Printer, which means the developer only has drivers for windows. So no Postscript drivers available, what means a great PITA for our PC's with Linux. So since our 600€ printer went out of service me and 3 other colleagues had to bother one of the other windows users every time we needed to print something.
As this don't look a problem, imagine the hours that are lost.... And we are all engineers.... There are better ways to spend the money with our time.... So I decided to do it.... I had tried before with REDMON (http://pages.cs.wisc.edu/~ghost/redmon/index.htm), but with no success, but this time I made a decision to loose a few hours and make it...

The Solution:

So what I had in mind, was a simple solution.... To have a service running in our Windows SBS2003 that would monitor some folder change, where we would put PDF files and then print them.
Linux handles PDF nice, and the users would print to PDF and then place the files on that folder.
It looked really nice and as I thought a lot of free software was available, but......

Tryouts:

To begin I needed a command, from command line that would execute the print command, and after trying it with Acrobat Reader, I realized that it will always leave one window open with the program, and I didn't wanted that. So I had to find another solution.....

Then I needed the software to watch the folder changes.... From a dozen windows software the best one, that was able to execute anything on changes was Watch 4 Folder 2.3 (http://leelusoft.blogspot.pt/), but it didn't worked as expected, and I realized that it would only work on file changes from MS computers. After a few emails with the developer, and as I suspected, it uses theMicrosoft Windows Shell API Notifications, and it is not relieable on network shares! Thank you for the support Mr. Zvika Israeli!!!!So I had to find another solution....

Note: The path to the executable depends on your system, this is the path to a Portuguese Windows SBS2003!!!!!!!!!

3 - Create the python script file - Open a notepad and call it dirwatch.py (or other name you want) and save it on c:\ and fill it with the following code:

import os, timeimport win32api

path_to_watch = "z:\\zz_Scanner\\Imprimir" ##This is the folder where you want to put the files for printing double slash are needed because the slash is a special caracter in python!!!!before = dict ((f, None) for f in os.listdir (path_to_watch))while 1: time.sleep (1) after = dict ((f, None) for f in os.listdir (path_to_watch)) added = [f for f in after if not f in before] removed = [f for f in before if not f in after] if added: cmd = str("print.bat "+ "\"" + path_to_watch + "\\" + " ".join (added)+"\"") print cmd win32api.WinExec(str(cmd)) before = after

4 - Now run the dirwatch.py and one command line will open without any info. If you place a PDF in the path_to_watch dir, you will see the script finding the file, executing the command and exiting the batch file, and waiting for another file.
The script uses about 5Mb of Ram, and if you want to get less cpu usage you can replace time.sleep(1) for a higher value. The value is seconds so now you choose how much time you want to wait for the printing and balance it with your server load.

5 - I wanted this script to run as a service (seamless) so I place a link to it on startup.
If you don't want to have a command line floating around your server, rename the dirwatch.py to dirwatch.pyw
If you get more then one print, just close python instances on Task Manager.

Future Improvements:

1 - Get rid of win32api to run commands, I'malmost sure that this could be done with os, help please!!! It would execute faster with less Ram usage. And without the need to have pywin32 installed.
2 - Compare also with existing filenames, because a user could replace a file with the same name and the script only recognize files added!!!
3 - Delete the file after printing (if I do this I don't need the point above)
4 - Configure cups-pdf on Linux computer to place files directly on desired directory - I have to deal with file permissions on both linux machines and our NAS server where the folder is.....

So this is it, it worked for me, and a lot can be done so this work better, but the solution is simple and easy, and it will help us to gain time more then important on other tasks....