/vendor/mike42/escpos-php/README.md

https://bitbucket.org/cyntreksolution/pos · Markdown · 570 lines · 413 code · 157 blank · 0 comment · 0 complexity · f878742eb0192d46b844a1ad71ee17ee MD5 · raw file

  1. # ESC/POS Print Driver for PHP
  2. [![Build Status](https://travis-ci.org/mike42/escpos-php.svg?branch=master)](https://travis-ci.org/mike42/escpos-php) [![Latest Stable Version](https://poser.pugx.org/mike42/escpos-php/v/stable)](https://packagist.org/packages/mike42/escpos-php)
  3. [![Total Downloads](https://poser.pugx.org/mike42/escpos-php/downloads)](https://packagist.org/packages/mike42/escpos-php)
  4. [![License](https://poser.pugx.org/mike42/escpos-php/license)](https://packagist.org/packages/mike42/escpos-php)
  5. [![Coverage Status](https://coveralls.io/repos/github/mike42/escpos-php/badge.svg?branch=development)](https://coveralls.io/github/mike42/escpos-php?branch=development)
  6. This project implements a subset of Epson's ESC/POS protocol for thermal receipt printers. It allows you to generate and print receipts with basic formatting, cutting, and barcodes on a compatible printer.
  7. The library was developed to add drop-in support for receipt printing to any PHP app, including web-based point-of-sale (POS) applications.
  8. ## Compatibility
  9. ### Interfaces and operating systems
  10. This driver is known to work with the following OS/interface combinations:
  11. <table>
  12. <tr>
  13. <th>&nbsp;</th>
  14. <th>Linux</th>
  15. <th>Mac</th>
  16. <th>Windows</th>
  17. </tr>
  18. <tr>
  19. <th>Ethernet</th>
  20. <td><a href="https://github.com/mike42/escpos-php/tree/master/example/interface/ethernet.php">Yes</a></td>
  21. <td><a href="https://github.com/mike42/escpos-php/tree/master/example/interface/ethernet.php">Yes</a></td>
  22. <td><a href="https://github.com/mike42/escpos-php/tree/master/example/interface/ethernet.php">Yes</a></td>
  23. </tr>
  24. <tr>
  25. <th>USB</th>
  26. <td><a href="https://github.com/mike42/escpos-php/tree/master/example/interface/linux-usb.php">Yes</a></td>
  27. <td>Not tested</td>
  28. <td><a href="https://github.com/mike42/escpos-php/tree/master/example/interface/windows-usb.php">Yes</a></td>
  29. </tr>
  30. <tr>
  31. <th>USB-serial</th>
  32. <td>Yes</td>
  33. <td>Yes</td>
  34. <td>Yes</td>
  35. </tr>
  36. <tr>
  37. <th>Serial</th>
  38. <td>Yes</td>
  39. <td>Yes</td>
  40. <td>Yes</td>
  41. </tr>
  42. <tr>
  43. <th>Parallel</th>
  44. <td><a href="https://github.com/mike42/escpos-php/tree/master/example/interface/windows-lpt.php">Yes</a></td>
  45. <td>Not tested</td>
  46. <td>Yes</td>
  47. </tr>
  48. <tr>
  49. <th>SMB shared</th>
  50. <td><a href="https://github.com/mike42/escpos-php/tree/master/example/interface/smb.php">Yes</a></td>
  51. <td>No</td>
  52. <td><a href="https://github.com/mike42/escpos-php/tree/master/example/interface/smb.php">Yes</a></td>
  53. </tr>
  54. <tr>
  55. <th>CUPS hosted</th>
  56. <td><a href="https://github.com/mike42/escpos-php/tree/master/example/interface/cups.php">Yes</a></td>
  57. <td><a href="https://github.com/mike42/escpos-php/tree/master/example/interface/cups.php">Yes</a></td>
  58. <td>No</td>
  59. </tr>
  60. </table>
  61. ### Printers
  62. Many thermal receipt printers support ESC/POS to some degree. This driver has been known to work with:
  63. - 3nStrat POS-08
  64. - AURES ODP-333
  65. - AURES ODP-500
  66. - Bematech-4200-TH
  67. - Bematech LR2000E
  68. - Bixolon SRP-350III
  69. - Black Copper BC-85AC
  70. - Citizen CBM1000-II
  71. - Citizen CT-S310II
  72. - Dapper-Geyi Q583P
  73. - Daruma DR800
  74. - DR-MP200 (manufacturer unknown)
  75. - EPOS TEP 220M
  76. - Epson EU-T332C
  77. - Epson FX-890 (requires `feedForm()` to release paper).
  78. - Epson TM-T20
  79. - Epson TM-T20II
  80. - Epson TM-T70
  81. - Epson TM-T70II
  82. - Epson TM-T81
  83. - Epson TM-T82II
  84. - Epson TM-T88II
  85. - Epson TM-T88III
  86. - Epson TM-T88IV
  87. - Epson TM-T88V
  88. - Epson TM-U220
  89. - Epson TM-U295 (requires `release()` to release slip).
  90. - Epson TM-U590 and TM-U590P
  91. - Equal (EQ-IT-001) POS-58
  92. - Excelvan HOP-E200
  93. - Excelvan HOP-E58
  94. - Excelvan HOP-E801
  95. - Excelvan ZJ-8220
  96. - Gainscha GP-5890x (Also marketed as EC Line 5890x)
  97. - Gainscha GP-U80300I (Also marketed as gprinter GP-U80300I)
  98. - gprinter GP-U80160I
  99. - Hasar HTP 250
  100. - Metapace T-1
  101. - Metapace T-25
  102. - Nexa PX700
  103. - Nyear NP100
  104. - Okipos 80 Plus III
  105. - Orient BTP-R580
  106. - Partner Tech RP320
  107. - P-822D
  108. - P85A-401 (make unknown)
  109. - Rongta RP326US
  110. - Rongta RP58-U
  111. - Senor TP-100
  112. - SEYPOS PRP-300 (Also marketed as TYSSO PRP-300)
  113. - Sicar POS-80
  114. - Silicon SP-201 / RP80USE
  115. - SPRT SP-POS88V
  116. - Star BSC10
  117. - Star TSP100 ECO
  118. - Star TSP100III FuturePRNT
  119. - Star TSP-650
  120. - Star TUP-592
  121. - Venus V248T
  122. - Xprinter F-900
  123. - Xprinter XP-365B
  124. - Xprinter XP-58 Series
  125. - Xprinter XP-80C
  126. - Xprinter XP-90
  127. - XPrinter XP-Q20011
  128. - Xprinter XP-Q800
  129. - Zjiang NT-58H
  130. - Zjiang ZJ-5870
  131. - Zjiang ZJ-5890K
  132. - Zjiang ZJ-5890T (Marketed as POS 5890T)
  133. - Zjiang ZJ-8220
  134. If you use any other printer with this code, please [let us know](https://github.com/mike42/escpos-php/issues/new) so that it can be added to the list.
  135. ## Basic usage
  136. ### Include the library
  137. #### Composer
  138. If you are using composer, then add `mike42/escpos-php` as a dependency:
  139. ```bash
  140. composer require mike42/escpos-php
  141. ```
  142. In this case, you would include composer's auto-loader at the top of your source files:
  143. ```php
  144. <?php
  145. require __DIR__ . '/vendor/autoload.php';
  146. ```
  147. #### Manually
  148. If you don't have composer available, then simply download the code and include `autoload.php`:
  149. ```bash
  150. git clone https://github.com/mike42/escpos-php vendor/mike42/escpos-php
  151. ```
  152. ```php
  153. <?php
  154. require __DIR__ . '/vendor/mike42/escpos-php/autoload.php';
  155. ```
  156. #### Requirements
  157. To maintain compatibility with as many systems as possible, this driver has few
  158. hard dependencies:
  159. - PHP 5.4 or above.
  160. - `mbstring` extension, since the driver accepts UTF-8 encoding.
  161. It is also suggested that you install either `imagick` or `gd`, so that you can
  162. print images.
  163. A number of optional packages can be added to enable more specific features. These
  164. are described in the "suggest" section of [composer.json](https://github.com/mike42/escpos-php/tree/master/composer.json).
  165. ### The 'Hello World' receipt
  166. To make use of this driver, your server (where PHP is installed) must be able to communicate with your printer. Start by generating a simple receipt and sending it to your printer using the command-line.
  167. ```php
  168. <?php
  169. /* Call this file 'hello-world.php' */
  170. require __DIR__ . '/vendor/autoload.php';
  171. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  172. use Mike42\Escpos\Printer;
  173. $connector = new FilePrintConnector("php://stdout");
  174. $printer = new Printer($connector);
  175. $printer -> text("Hello World!\n");
  176. $printer -> cut();
  177. $printer -> close();
  178. ```
  179. Some examples are below for common interfaces.
  180. Communicate with a printer with an Ethernet interface using `netcat`:
  181. ```bash
  182. php hello-world.php | nc 10.x.x.x. 9100
  183. ```
  184. A USB local printer connected with `usblp` on Linux has a device file (Includes USB-parallel interfaces):
  185. ```bash
  186. php hello-world.php > /dev/usb/lp0
  187. ```
  188. A computer installed into the local `cups` server is accessed through `lp` or `lpr`:
  189. ```bash
  190. php hello-world.php > foo.txt
  191. lpr -o raw -H localhost -P printer foo.txt
  192. ```
  193. A local or networked printer on a Windows computer is mapped in to a file, and generally requires you to share the printer first:
  194. ```
  195. php hello-world.php > foo.txt
  196. net use LPT1 \\server\printer
  197. copy foo.txt LPT1
  198. del foo.txt
  199. ```
  200. If you have troubles at this point, then you should consult your OS and printer system documentation to try to find a working print command.
  201. ### Using a PrintConnector
  202. To print receipts from PHP, use the most applicable [PrintConnector](https://github.com/mike42/escpos-php/tree/master/src/Mike42/Escpos/PrintConnectors) for your setup. The connector simply provides the plumbing to get data to the printer.
  203. For example, a `NetworkPrintConnector` accepts an IP address and port:
  204. ```php
  205. use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
  206. use Mike42\Escpos\Printer;
  207. $connector = new NetworkPrintConnector("10.x.x.x", 9100);
  208. $printer = new Printer($connector);
  209. try {
  210. // ... Print stuff
  211. } finally {
  212. $printer -> close();
  213. }
  214. ```
  215. While a serial printer might use:
  216. ```php
  217. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  218. use Mike42\Escpos\Printer;
  219. $connector = new FilePrintConnector("/dev/ttyS0");
  220. $printer = new Printer($connector);
  221. ```
  222. For each OS/interface combination that's supported, there are examples in the compatibility section of how a `PrintConnector` would be constructed. If you can't get a `PrintConnector` to work, then be sure to include the working print command in bug.
  223. ### Using a CapabilityProfile
  224. Support for commands and code pages varies between printer vendors and models. By default, the driver will accept UTF-8, and output commands that are suitable for Epson TM-series printers.
  225. When trying out a new brand of printer, it's a good idea to use the "simple" `CapabilityProfile`, which instructs the driver to avoid the use of advanced features (generally simpler image handling, ASCII-only text).
  226. ```php
  227. use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
  228. use Mike42\Escpos\CapabilityProfile;
  229. $profile = CapabilityProfile::load("simple");
  230. $connector = new WindowsPrintConnector("smb://computer/printer");
  231. $printer = new Printer($connector, $profile);
  232. ```
  233. As another example, Star-branded printers use different commands:
  234. ```php
  235. use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
  236. use Mike42\Escpos\CapabilityProfile;
  237. $profile = CapabilityProfile::load("SP2000")
  238. $connector = new WindowsPrintConnector("smb://computer/printer");
  239. $printer = new Printer($connector, $profile);
  240. ```
  241. For a list of available profiles, or to have support for your printer improved, please see the upstream [receipt-print-hq/escpos-printer-db](https://github.com/receipt-print-hq/escpos-printer-db) project.
  242. ### Tips & examples
  243. On Linux, your printer device file will be somewhere like `/dev/lp0` (parallel), `/dev/usb/lp1` (USB), `/dev/ttyUSB0` (USB-Serial), `/dev/ttyS0` (serial).
  244. On Windows, the device files will be along the lines of `LPT1` (parallel) or `COM1` (serial). Use the `WindowsPrintConnector` to tap into system printing on Windows (eg. [Windows USB](https://github.com/mike42/escpos-php/tree/master/example/interface/windows-usb.php), [SMB](https://github.com/mike42/escpos-php/tree/master/example/interface/smb.php) or [Windows LPT](https://github.com/mike42/escpos-php/tree/master/example/interface/windows-lpt.php)) - this submits print jobs via a queue rather than communicating directly with the printer.
  245. A complete real-world receipt can be found in the code of [Auth](https://github.com/mike42/Auth) in [ReceiptPrinter.php](https://github.com/mike42/Auth/blob/master/lib/misc/ReceiptPrinter.php). It includes justification, boldness, and a barcode.
  246. Other examples are located in the [example/](https://github.com/mike42/escpos-php/blob/master/example/) directory.
  247. ## Available methods
  248. ### __construct(PrintConnector $connector, CapabilityProfile $profile)
  249. Construct new print object.
  250. Parameters:
  251. - `PrintConnector $connector`: The PrintConnector to send data to.
  252. - `CapabilityProfile $profile` Supported features of this printer. If not set, the "default" CapabilityProfile will be used, which is suitable for Epson printers.
  253. See [example/interface/](https://github.com/mike42/escpos-php/tree/master/example/interface/) for ways to open connections for different platforms and interfaces.
  254. ### barcode($content, $type)
  255. Print a barcode.
  256. Parameters:
  257. - `string $content`: The information to encode.
  258. - `int $type`: The barcode standard to output. If not specified, `Printer::BARCODE_CODE39` will be used.
  259. Currently supported barcode standards are (depending on your printer):
  260. - `BARCODE_UPCA`
  261. - `BARCODE_UPCE`
  262. - `BARCODE_JAN13`
  263. - `BARCODE_JAN8`
  264. - `BARCODE_CODE39`
  265. - `BARCODE_ITF`
  266. - `BARCODE_CODABAR`
  267. Note that some barcode standards can only encode numbers, so attempting to print non-numeric codes with them may result in strange behaviour.
  268. ### bitImage(EscposImage $image, $size)
  269. See [graphics()](#graphicsescposimage-image-size) below.
  270. ### cut($mode, $lines)
  271. Cut the paper.
  272. Parameters:
  273. - `int $mode`: Cut mode, either `Printer::CUT_FULL` or `Printer::CUT_PARTIAL`. If not specified, `Printer::CUT_FULL` will be used.
  274. - `int $lines`: Number of lines to feed before cutting. If not specified, 3 will be used.
  275. ### feed($lines)
  276. Print and feed line / Print and feed n lines.
  277. Parameters:
  278. - `int $lines`: Number of lines to feed
  279. ### feedForm()
  280. Some printers require a form feed to release the paper. On most printers, this command is only useful in page mode, which is not implemented in this driver.
  281. ### feedReverse($lines)
  282. Print and reverse feed n lines.
  283. Parameters:
  284. - `int $lines`: number of lines to feed. If not specified, 1 line will be fed.
  285. ### graphics(EscposImage $image, $size)
  286. Print an image to the printer.
  287. Parameters:
  288. - `EscposImage $img`: The image to print.
  289. - `int $size`: Output size modifier for the image.
  290. Size modifiers are:
  291. - `IMG_DEFAULT` (leave image at original size)
  292. - `IMG_DOUBLE_WIDTH`
  293. - `IMG_DOUBLE_HEIGHT`
  294. A minimal example:
  295. ```php
  296. <?php
  297. $img = EscposImage::load("logo.png");
  298. $printer -> graphics($img);
  299. ```
  300. See the [example/](https://github.com/mike42/escpos-php/blob/master/example/) folder for detailed examples.
  301. The function [bitImage()](#bitimageescposimage-image-size) takes the same parameters, and can be used if your printer doesn't support the newer graphics commands. As an additional fallback, the `bitImageColumnFormat()` function is also provided.
  302. ### initialize()
  303. Initialize printer. This resets formatting back to the defaults.
  304. ### pdf417Code($content, $width, $heightMultiplier, $dataColumnCount, $ec, $options)
  305. Print a two-dimensional data code using the PDF417 standard.
  306. Parameters:
  307. - `string $content`: Text or numbers to store in the code
  308. - `number $width`: Width of a module (pixel) in the printed code. Default is 3 dots.
  309. - `number $heightMultiplier`: Multiplier for height of a module. Default is 3 times the width.
  310. - `number $dataColumnCount`: Number of data columns to use. 0 (default) is to auto-calculate. Smaller numbers will result in a narrower code, making larger pixel sizes possible. Larger numbers require smaller pixel sizes.
  311. - `real $ec`: Error correction ratio, from 0.01 to 4.00. Default is 0.10 (10%).
  312. - `number $options`: Standard code `Printer::PDF417_STANDARD` with start/end bars, or truncated code `Printer::PDF417_TRUNCATED` with start bars only.
  313. ### pulse($pin, $on_ms, $off_ms)
  314. Generate a pulse, for opening a cash drawer if one is connected. The default settings (0, 120, 240) should open an Epson drawer.
  315. Parameters:
  316. - `int $pin`: 0 or 1, for pin 2 or pin 5 kick-out connector respectively.
  317. - `int $on_ms`: pulse ON time, in milliseconds.
  318. - `int $off_ms`: pulse OFF time, in milliseconds.
  319. ### qrCode($content, $ec, $size, $model)
  320. Print the given data as a QR code on the printer.
  321. - `string $content`: The content of the code. Numeric data will be more efficiently compacted.
  322. - `int $ec` Error-correction level to use. One of `Printer::QR_ECLEVEL_L` (default), `Printer::QR_ECLEVEL_M`, `Printer::QR_ECLEVEL_Q` or `Printer::QR_ECLEVEL_H`. Higher error correction results in a less compact code.
  323. - `int $size`: Pixel size to use. Must be 1-16 (default 3)
  324. - `int $model`: QR code model to use. Must be one of `Printer::QR_MODEL_1`, `Printer::QR_MODEL_2` (default) or `Printer::QR_MICRO` (not supported by all printers).
  325. ### selectPrintMode($mode)
  326. Select print mode(s).
  327. Parameters:
  328. - `int $mode`: The mode to use. Default is `Printer::MODE_FONT_A`, with no special formatting. This has a similar effect to running `initialize()`.
  329. Several MODE_* constants can be OR'd together passed to this function's `$mode` argument. The valid modes are:
  330. - `MODE_FONT_A`
  331. - `MODE_FONT_B`
  332. - `MODE_EMPHASIZED`
  333. - `MODE_DOUBLE_HEIGHT`
  334. - `MODE_DOUBLE_WIDTH`
  335. - `MODE_UNDERLINE`
  336. ### setBarcodeHeight($height)
  337. Set barcode height.
  338. Parameters:
  339. - `int $height`: Height in dots. If not specified, 8 will be used.
  340. ### setBarcodeWidth($width)
  341. Set barcode bar width.
  342. Parameters:
  343. - `int $width`: Bar width in dots. If not specified, 3 will be used. Values above 6 appear to have no effect.
  344. ### setColor($color)
  345. Select print color - on printers that support multiple colors.
  346. Parameters:
  347. - `int $color`: Color to use. Must be either `Printer::COLOR_1` (default), or `Printer::COLOR_2`
  348. ### setDoubleStrike($on)
  349. Turn double-strike mode on/off.
  350. Parameters:
  351. - `boolean $on`: true for double strike, false for no double strike.
  352. ### setEmphasis($on)
  353. Turn emphasized mode on/off.
  354. Parameters:
  355. - `boolean $on`: true for emphasis, false for no emphasis.
  356. ### setFont($font)
  357. Select font. Most printers have two fonts (Fonts A and B), and some have a third (Font C).
  358. Parameters:
  359. - `int $font`: The font to use. Must be either `Printer::FONT_A`, `Printer::FONT_B`, or `Printer::FONT_C`.
  360. ### setJustification($justification)
  361. Select justification.
  362. Parameters:
  363. - `int $justification`: One of `Printer::JUSTIFY_LEFT`, `Printer::JUSTIFY_CENTER`, or `Printer::JUSTIFY_RIGHT`.
  364. ### setLineSpacing($height)
  365. Set the height of the line.
  366. Some printers will allow you to overlap lines with a smaller line feed.
  367. Parameters:
  368. - `int $height`: The height of each line, in dots. If not set, the printer will reset to its default line spacing.
  369. ### setPrintLeftMargin($margin)
  370. Set print area left margin. Reset to default with `Printer::initialize()`.
  371. Parameters:
  372. - `int $margin`: The left margin to set on to the print area, in dots.
  373. ### setPrintWidth($width)
  374. Set print area width. This can be used to add a right margin to the print area. Reset to default with `Printer::initialize()`.
  375. Parameters:
  376. - `int $width`: The width of the page print area, in dots.
  377. ### setReverseColors($on)
  378. Set black/white reverse mode on or off. In this mode, text is printed white on a black background.
  379. Parameters:
  380. - `boolean $on`: True to enable, false to disable.
  381. ### setTextSize($widthMultiplier, $heightMultiplier)
  382. Set the size of text, as a multiple of the normal size.
  383. Parameters:
  384. - `int $widthMultiplier`: Multiple of the regular height to use (range 1 - 8).
  385. - `int $heightMultiplier`: Multiple of the regular height to use (range 1 - 8).
  386. ### setUnderline($underline)
  387. Set underline for printed text.
  388. Parameters:
  389. - `int $underline`: Either `true`/`false`, or one of `Printer::UNDERLINE_NONE`, `Printer::UNDERLINE_SINGLE` or `Printer::UNDERLINE_DOUBLE`. Defaults to `Printer::UNDERLINE_SINGLE`.
  390. ### text($str)
  391. Add text to the buffer. Text should either be followed by a line-break, or `feed()` should be called after this.
  392. Parameters:
  393. - `string $str`: The string to print.
  394. # Further notes
  395. Posts I've written up for people who are learning how to use receipt printers:
  396. * [What is ESC/POS, and how do I use it?](https://mike42.me/blog/what-is-escpos-and-how-do-i-use-it), which documents the output of `example/demo.php`.
  397. * [Setting up an Epson receipt printer](https://mike42.me/blog/2014-20-26-setting-up-an-epson-receipt-printer)
  398. * [Getting a USB receipt printer working on Linux](https://mike42.me/blog/2015-03-getting-a-usb-receipt-printer-working-on-linux)
  399. # Development
  400. This code is MIT licensed, and you are encouraged to contribute any modifications back to the project.
  401. For development, it's suggested that you load `imagick`, `gd` and `Xdebug` PHP exensions, and install `composer`.
  402. The tests are executed on [Travis CI](https://travis-ci.org/mike42/escpos-php) over PHP 5.4, 5.5, 5.6, 7.0, 7.1 and 7.2, plus the latest LTS version of HHVM, 3.21. Older versions of PHP are not supported in current releases.
  403. Fetch a copy of this code and load dependencies with composer:
  404. git clone https://github.com/mike42/escpos-php
  405. cd escpos-php/
  406. composer install
  407. Execute unit tests via `phpunit`:
  408. php vendor/bin/phpunit --coverage-text
  409. This project uses the PSR-2 standard, which can be checked via [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer):
  410. php vendor/bin/phpcs --standard=psr2 src/ -n
  411. The developer docs are build with [doxygen](https://github.com/doxygen/doxygen). Re-build them to check for documentation warnings:
  412. make -C doc clean && make -C doc
  413. Pull requests and bug reports welcome.