├── README.md ├── php-print.php └── pi.php /README.md: -------------------------------------------------------------------------------- 1 | # EpsonESCPOS-PHP 2 | This project is to allow you to print to your EPSON Thermal Printer via PHP. 3 | This has been tested on Windows XP, Windows 7, Centos and Debian as well as a Raspberry PI 4 | 5 | My current setup is a Raspberry PI 2 with a PI Serial adapter from ABElectronics 6 | https://www.abelectronics.co.uk/products/3/Raspberry-Pi/29/Serial-Pi 7 | 8 | In terms of software I have had this working on Apache2 on Linux, XAMPP and WAMP on Windows - All other web servers should support this! 9 | 10 | This supports all versions of EPSON Serial Printers. During the main part of my testing I was using TM88III, TM88IV printers. 11 | 12 | 13 | #Installation and Configuration 14 | #Windows: 15 | COM1 16 | 17 | #LINUX: 18 | /dev/ttyS1 19 | 20 | For linux devices it is best to do: dmesg | grep tty (This will show you your available serial devices) 21 | 22 | #RASPBERRY PI with PISERIAL 23 | /dev/ttyAMA0 24 | 25 | #Common Issues 26 | If you have issues with the font be garbage on the page then you will need to change Port Speed 27 | 28 | #Windows 29 | Open Start -> Computer -> Right Click -> Manage Computer -> Devices -> Ports -> Find your port and click Properties -> Click Configure -> Edit the Settings 30 | 31 | OR 32 | 33 | Open CMD -> Windows Key and R -> CMD -> Enter -> MODE [SERIALPORT]:[PORT SPEED],N,8,1,P 34 | For reference: http://www.computerhope.com/issues/ch000245.htm 35 | 36 | 37 | #LINUX 38 | You might want to try: stty -F /dev/tty[PORT] [SPEED] however linux is usually out the box and it works. 39 | PLEASE NOTE: On Linux you may need to do: chmod a+x /dev/tty[DEVICE] as by default you only have Read Access to the Coms port 40 | 41 | #Youtube Video 42 | This is a link to show you what it looks like when printed: 43 | http://www.youtube.com/watch?v=k1KTLC01mtM 44 | 45 | #Sample Code 46 | 47 | //For this example I am using my Raspberry PI 48 | $printer = new phpprint("/dev/ttyAMA0"); 49 | //We put a space between the top of the page and the content 50 | $printer->newline(); 51 | // We set the font to something that I would define as slightly more sexier 52 | $printer->set_font(phpprint::FONT_B); 53 | // We then set the justification to CENTER - This will print everything center aligned 54 | $printer->set_justification(phpprint::JUSTIFY_CENTER); 55 | // We now set the font larger for our Text Logo 56 | $printer->enlargePrint(true); 57 | // We simply add our text for the logo (Note: The \n will ensure the next piece of content will go to a new line 58 | $printer->text("FuelledUK EPOS\n"); 59 | // We are changing the font to show you the difference 60 | $printer->set_font(phpprint::FONT_A); 61 | // We print some more text to fill the page 62 | $printer->text("www.fuelled.co\n"); 63 | // Lets add some space before we start placing with Colours 64 | $printer->newline(); 65 | // We are now telling the printer we want everything to be printed in opposite. So black background and white text 66 | $printer->reverse_mode(1); 67 | // Generate Random ID (Test only) 68 | $rand = rand(200000,999999); 69 | // As you will see there is slightly bigger space between the text so that it looks good on the print out 70 | $printer->text(" CUSTOMER ID: ".$rand." \n"); 71 | // We now reset the printer back to normal printing mode 72 | $printer->reverse_mode(0); 73 | $printer->enlargePrint(); 74 | // We then apply 2 lines of space. Feed is best for when you want bigger gaps 75 | $printer->feed(2); 76 | // This function will generate you a barcode - Simply specify the content, barcode type and height 77 | $printer->generateBarcode($rand, phpprint::BARCODE_CODE39, 80); 78 | // More spacing 79 | $printer->feed(2); 80 | // Add some normal text to the page with different font 81 | $printer->set_font(phpprint::FONT_C); 82 | $printer->text("NEW CUSTOMER: WARREN DOYLE\n"); 83 | // Always ensure you add space at the end of the print so you can easily collect it from the printer 84 | $printer->newline(); 85 | // Finally cut the page 86 | $printer->cut(); 87 | // If you are creating multiple pages, I would ensure you put 88 | // sleep(1); so that the print doesn't get congested and stop working 89 | 90 | # Attribution 91 | This library is a modified version of escpos-php, a Library to work with ESC/POS thermal printers, implemented by Michael Billington. Further documentation is available at [https://github.com/mike42/escpos-php](https://github.com/mike42/escpos-php). 92 | -------------------------------------------------------------------------------- /php-print.php: -------------------------------------------------------------------------------- 1 | , 4 | * with additions by Warren Doyle (wdoyle) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | * If you wish to expand this library you can use the below link: 25 | * http://content.epson.de/fileadmin/content/files/RSD/downloads/escpos.pdf 26 | */ 27 | define("DEBUG_MODE", 0); 28 | 29 | if(DEBUG_MODE == 1) 30 | { 31 | error_reporting(E_ALL); 32 | ini_set("display_errors", 1); 33 | } 34 | 35 | class phpprint { 36 | /* ASCII codes */ 37 | const NUL = "\x00"; 38 | const LF = "\x0a"; 39 | const ESC = "\x1b"; 40 | const GS = "\x1d"; 41 | 42 | /* Print mode constants */ 43 | const MODE_FONT_A = 0; 44 | const MODE_FONT_B = 1; 45 | const MODE_EMPHASIZED = 8; 46 | const MODE_DOUBLE_HEIGHT = 16; 47 | const MODE_DOUBLE_WIDTH = 32; 48 | const MODE_UNDERLINE = 128; 49 | 50 | /* Fonts */ 51 | const FONT_A = 0; 52 | const FONT_B = 1; 53 | const FONT_C = 2; 54 | 55 | /* Justifications */ 56 | const JUSTIFY_LEFT = 0; 57 | const JUSTIFY_CENTER = 1; 58 | const JUSTIFY_RIGHT = 2; 59 | 60 | /* Cut types */ 61 | const CUT_FULL = 65; 62 | const CUT_PARTIAL = 66; 63 | 64 | /* Barcode types */ 65 | const BARCODE_UPCA = 0; 66 | const BARCODE_UPCE = 1; 67 | const BARCODE_JAN13 = 2; 68 | const BARCODE_JAN8 = 3; 69 | const BARCODE_CODE39 = 4; 70 | const BARCODE_ITF = 5; 71 | const BARCODE_CODABAR = 6; 72 | private $fp; 73 | 74 | function __construct($head = null) { 75 | //THIS IS THE SERIAL PORT YOU ARE OPENING 76 | $fp = fopen($head,"w"); 77 | $this -> fp = $fp; 78 | $this -> initialize(); 79 | } 80 | /** 81 | * Add text to the buffer 82 | * 83 | * @param string $str Text to print 84 | */ 85 | function text($str = "") { 86 | if(DEBUG_MODE == 1) 87 | echo $str; 88 | 89 | fwrite($this -> fp, $str); 90 | } 91 | /** 92 | * Print and feed line / Print and feed n lines 93 | * 94 | * @param int $lines Number of lines to feed 95 | */ 96 | function feed($lines = 1) { 97 | if($lines <= 1) { 98 | fwrite($this -> fp, self::LF); 99 | } else { 100 | fwrite($this -> fp, self::ESC . "d" . chr($lines)); 101 | } 102 | } 103 | /** 104 | * Select print mode(s). 105 | * 106 | * Arguments should be OR'd together MODE_* constants: 107 | * MODE_FONT_A 108 | * MODE_FONT_B 109 | * MODE_EMPHASIZED 110 | * MODE_DOUBLE_HEIGHT 111 | * MODE_DOUBLE_WIDTH 112 | * MODE_UNDERLINE 113 | * 114 | * @param int $mode 115 | */ 116 | function select_print_mode($mode = self::NUL) { 117 | fwrite($this -> fp, self::ESC . "!" . chr($mode)); 118 | } 119 | function reverse_mode($rev = 0) { 120 | fwrite($this -> fp, self::GS . "B" . chr($rev)); 121 | } 122 | /** 123 | * Turn underline mode on/off 124 | * 125 | * @param int $underline 0 for no underline, 1 for underline, 2 for heavy underline 126 | */ 127 | function set_underline($underline = 1) { 128 | fwrite($this -> fp, self::ESC . "-". chr($underline)); 129 | } 130 | /** 131 | * Initialize printer 132 | */ 133 | function initialize() { 134 | fwrite($this -> fp, self::ESC . "@"); 135 | } 136 | /** 137 | * Turn emphasized mode on/off 138 | * 139 | * @param boolean $on true for emphasis, false for no emphasis 140 | */ 141 | function set_emphasis($on = false) { 142 | fwrite($this -> fp, self::ESC . "E". ($on ? chr(1) : chr(0))); 143 | } 144 | /** 145 | * Turn double-strike mode on/off 146 | * 147 | * @param boolean $on true for double strike, false for no double strike 148 | */ 149 | function set_double_strike($on = false) { 150 | fwrite($this -> fp, self::ESC . "G". ($on ? chr(1) : chr(0))); 151 | } 152 | /** 153 | * Select character font. 154 | * Font must be FONT_A, FONT_B, or FONT_C. 155 | * 156 | * @param int $font 157 | */ 158 | function set_font($font = self::FONT_A) { 159 | fwrite($this -> fp, self::ESC . "M" . chr($font)); 160 | } 161 | /** 162 | * Select justification 163 | * Justification must be JUSTIFY_LEFT, JUSTIFY_CENTER, or JUSTIFY_RIGHT. 164 | */ 165 | function set_justification($justification = self::JUSTIFY_LEFT) { 166 | fwrite($this -> fp, self::ESC . "a" . chr($justification)); 167 | } 168 | /** 169 | * Print and reverse feed n lines 170 | * 171 | * @param int $lines number of lines to feed 172 | */ 173 | function feed_reverse($lines = 1) { 174 | fwrite($this -> fp, self::ESC . "e" . chr($lines)); 175 | } 176 | /** 177 | * Cut the paper 178 | * 179 | * @param int $mode Cut mode, either CUT_FULL or CUT_PARTIAL 180 | * @param int $lines Number of lines to feed 181 | */ 182 | function cut($mode = self::CUT_FULL, $lines = 3) { 183 | fwrite($this -> fp, self::GS . "V" . chr($mode) . chr($lines)); 184 | } 185 | /** 186 | * Set barcode height 187 | * 188 | * @param int $height Height in dots 189 | */ 190 | function set_barcode_height($height = 8) { 191 | fwrite($this -> fp, self::GS . "h" . chr($height)); 192 | } 193 | /** 194 | * Print a barcode 195 | * 196 | * @param string $content 197 | * @param int $type 198 | */ 199 | function barcode($content, $type = self::BARCODE_CODE39) { 200 | fwrite($this -> fp, self::GS . "k" . chr($type) . $content . self::NUL); 201 | } 202 | /** 203 | * This will generate a Barcode and print it directly to the Printer 204 | * 205 | * @param string $content 206 | * @param int $type 207 | * @param int $height 208 | * 209 | */ 210 | function generateBarcode($content, $type, $height) 211 | { 212 | $this->set_barcode_height($height); 213 | $this->barcode($content, $type); 214 | $this->feed(); 215 | } 216 | /* 217 | * 218 | * This will increase the font used on the page whilst it is true 219 | * @param bool $on 220 | * 221 | */ 222 | function enlargePrint($on = false) 223 | { 224 | if($on == true) 225 | $this->select_print_mode(self::MODE_DOUBLE_HEIGHT); 226 | else 227 | $this->select_print_mode(); 228 | 229 | } 230 | 231 | /* 232 | * This will print an empty line 233 | */ 234 | function newline() 235 | { 236 | $this->text("\n"); 237 | } 238 | 239 | /** 240 | * Generate a pulse, for opening a cash drawer if one is connected. 241 | * The default settings should open an Epson drawer. 242 | * 243 | * @param int $pin 0 or 1, for pin 2 or pin 5 kick-out connector respectively. 244 | * @param int $on_ms pulse ON time, in milliseconds. 245 | * @param int $off_ms pulse OFF time, in milliseconds. 246 | */ 247 | function pulse($pin = 0, $on_ms = 120, $off_ms = 240) { 248 | fwrite($this -> fp, self::ESC . "p" . chr($m + 48) . chr($t1 / 2) . chr($t2 / 2)); 249 | } 250 | } 251 | -------------------------------------------------------------------------------- /pi.php: -------------------------------------------------------------------------------- 1 | Computer -> Right Click -> Manage Computer -> Devices -> Ports -> Find your port and click Properties -> Click Configure -> Edit the Settings 26 | * OR 27 | * Open CMD -> Windows Key and R -> CMD -> Enter -> MODE [SERIALPORT]:[PORT SPEED],N,8,1,P 28 | * For reference: http://www.computerhope.com/issues/ch000245.htm 29 | * 30 | * 31 | * //LINUX 32 | * You might want to try: stty -F /dev/tty[PORT] [SPEED] however linux is usually out the box and it works. 33 | * 34 | * PLEASE NOTE: On Linux you may need to do: chmod a+x /dev/tty[DEVICE] as by default you only have Read Access to the Coms port 35 | * 36 | */ 37 | 38 | //For this example I am using my Raspberry PI 39 | $printer = new phpprint("/dev/ttyAMA0"); 40 | //We put a space between the top of the page and the content 41 | $printer->newline(); 42 | // We set the font to something that I would define as slightly more sexier 43 | $printer->set_font(phpprint::FONT_B); 44 | // We then set the justification to CENTER - This will print everything center aligned 45 | $printer->set_justification(phpprint::JUSTIFY_CENTER); 46 | // We now set the font larger for our Text Logo 47 | $printer->enlargePrint(true); 48 | // We simply add our text for the logo (Note: The \n will ensure the next piece of content will go to a new line 49 | $printer->text("FuelledUK EPOS\n"); 50 | // We are changing the font to show you the difference 51 | $printer->set_font(phpprint::FONT_A); 52 | // We print some more text to fill the page 53 | $printer->text("www.fuelled.co\n"); 54 | // Lets add some space before we start placing with Colours 55 | $printer->newline(); 56 | // We are now telling the printer we want everything to be printed in opposite. So black background and white text 57 | $printer->reverse_mode(1); 58 | // Generate Random ID (Test only) 59 | $rand = rand(200000,999999); 60 | // As you will see there is slightly bigger space between the text so that it looks good on the print out 61 | $printer->text(" CUSTOMER ID: ".$rand." \n"); 62 | // We now reset the printer back to normal printing mode 63 | $printer->reverse_mode(0); 64 | $printer->enlargePrint(); 65 | // We then apply 2 lines of space. Feed is best for when you want bigger gaps 66 | $printer->feed(2); 67 | // This function will generate you a barcode - Simply specify the content, barcode type and height 68 | $printer->generateBarcode($rand, phpprint::BARCODE_CODE39, 80); 69 | // More spacing 70 | $printer->feed(2); 71 | // Add some normal text to the page with different font 72 | $printer->set_font(phpprint::FONT_C); 73 | $printer->text("NEW CUSTOMER: WARREN DOYLE\n"); 74 | // Always ensure you add space at the end of the print so you can easily collect it from the printer 75 | $printer->newline(); 76 | // Finally cut the page 77 | $printer->cut(); 78 | // If you are creating multiple pages, I would ensure you put 79 | // sleep(1); so that the print doesn't get congested and stop working 80 | 81 | echo "Print succesful"; 82 | 83 | --------------------------------------------------------------------------------