Tuesday, 30 March 2004
Document explaining how to write a printer driver from scratch in little time.
Requirements
You should have a working installation of My Handy Restaurant on your system.
Maybe you will need root account to work on your webserver directories.
If you're not root ask the administrator to give you write permission to those directories.
Getting the specifications
First of all you have to know how works the printer you want to write a driver for.
To do this you should know the exact printer manufacturer name (normally the brand) and model. You can normally find this info directly on your printer.
After doing this you should visit your printer manufacturer web site, and find something like a developers' manual, which is a manual where it is explained in depth how to make the printer do something.
Let's take an example:
If you have a Star printer, you can find on their developers' manual for the model TSP600 that to write some text double height, you should send to the printer the following command (=character sequence):
ESCi10
note that ESC character corresponds to the ASCII code 27, so in php we'll write something like:
chr(27).'i10'
What codes am I interested in?
Of course you're not interested in any code provided with your printer, but only in some of them.
You can see which codes are used in My Handy Restaurant in the drivers/manufacturer_model.php file, which is constantly updated.
If your printer doesn't support any function, just don't write that command, and it will be ignored when using your driver.
Writing the driver: the file format
The following rules should be followed when writing a new driver:
* The driver file has to be in the drivers directory
* The driver file name should be of the type manufacturer_model.php
* The driver file must contain a PHP function called driver_manufacturer_model, where manufacturer_model has to be exactly the same as those specified in the file name (letters case is important).
So, for example, a basic driver file called mybrand_mymodel.php with no command in it will look like this:
<?php
// My manufacturer's model description
// possibly also a comment about the paper (roll size and other info)
// and about author
function driver_mybrand_mymodel($msg)
?>
It is important that your driver is very similar to this one at this stage.
Writing the driver: adding the commands
Now that you have your driver file ready, you have to add the commands, one per line, in the following format:
$msg = stri_replace ('<command>','command_characters',$msg);
So, for example, to add the command to write double height text, that in our example corresponds to characters ESCh1, you should write:
$msg = stri_replace ('<height_double>',chr(27).'h1',$msg);
Please note that this is exactly the same of writing:
$msg = stri_replace ('<height_double>',chr(0x1B).'h1',$msg);
Because decimal 27 is equal to 1B hexadecimal.
Now you just have to write the lines that your printer requires.
The driver is ready!
After adding all the required lines, you'll have something like:
<?php
// My manufacturer's model description
// possibly also a comment about the paper (roll size and other info)
// and about author
function driver_mybrand_mymodel($msg)
?>
Where sometimes the use of chr(13) is needed because some printers require a new line character (ASCII code 13) before accepting commands.
As stated before, any command not included in a driver file will simply be ignored from My Handy Restaurant.
Mastering the Drivers
If you managed to write a basic driver, here are some tips that may help you debugging and improving it.
In some cases it may be good to "see" what's being ignored, so that we know which commands we should still write for our driver.
To have all the commands printed, you should modify the file conf/config.constants.inc.php and modify this line:
define('CONF_DEBUG_PRINT_MARKUP',0);
To this:
define('CONF_DEBUG_PRINT_MARKUP',1);
This way all the commands will be printed on your printer, even if you've not yet written a driver line for them.
A second advice could be to use "command links" for commands that your printer doens't support.
For example, any sheet printed will be cut with the command <page_cut>, but many printers have no cutter.
To have them do something else, you can write a driver line like this:
$msg = stri_replace ('<paper_release>','<page_cut>',$msg);
And you'll have your printer release the paper instead of cutting it! (provided that you have correctly set the <paper_release> command)
Conclusion
I hope writing drivers has been not too difficult for you, by following this howto. Please feel free to send me (kilyerd) or to the forum any comment or question about this topic or this howto and I will be glad to help you.
If you write any working driver, please send it to me, so that I can include it in the next versions.
Kilyerd
