In Linux (and I suspect in other OS's), depending on the type of printer device you use, you can pass data directly to it by simply catting some text to the device itself. My script looks something like this ...
exec("cat print.txt > /dev/ttyS0");
Some printers require you to pass hex code instead of ascii. In this case, I am running on Linux and Apache with PHP as an Apache mod, and I have PHP write the text file, then exec() the cat command to send the data to the printer. It is in ascii format.