Bagi kita developer berbasis web seperti php, ruby, python, dot net, dll terkadang terpikirkan bagaimana melakukan direct printing hanya dengan menekan tombol di browser dan otomatis akan mengeluarkan hasil printer. umumnya yang kita kenal adalah dengan cara menekan shift + P untuk melakukan print dan muncul window dialog preview. Show
untuk Direct printing yang pernah kami coba adalah dalam kasus develop sebuah aplikasi kasir berbasis web dimana diminta langsung print ke printer POS. Demo dapat dicoba langsung di : https://demo.qz.io/ Karena ini memerlukan instalasi QZ Tray, sehingga untuk sementara hanya mendukung OS linux, windows dan MAC, sedangkan Android dan iOS belum didukung. License pun tidak gratis, untuk trial disediakan tetapi ada batasan waktu.
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. The library was developed to add drop-in support for receipt printing to any PHP app, including web-based point-of-sale (POS) applications. CompatibilityInterfaces and operating systemsThis driver is known to work with the following OS/interface combinations: LinuxMacWindowsEthernetYesYesYesUSBYesNot testedYesUSB-serialYesYesYesSerialYesYesYesParallelYesNot testedYesSMB sharedYesNoYesCUPS hostedYesYesNoPrintersMany thermal receipt printers support ESC/POS to some degree. This driver has been known to work with:
If you use any other printer with this code, please let us know so that it can be added to the list. Basic usageInclude the libraryComposerThis library is designed for use with the <?php /* Call this file 'hello-world.php' */ require __DIR__ . '/vendor/autoload.php'; use Mike42\Escpos\PrintConnectors\FilePrintConnector; use Mike42\Escpos\Printer; $connector = new FilePrintConnector("php://stdout"); $printer = new Printer($connector); $printer -> text("Hello World!\n"); $printer -> cut(); $printer -> close();7 PHP dependency manager. Simply add the <?php /* Call this file 'hello-world.php' */ require __DIR__ . '/vendor/autoload.php'; use Mike42\Escpos\PrintConnectors\FilePrintConnector; use Mike42\Escpos\Printer; $connector = new FilePrintConnector("php://stdout"); $printer = new Printer($connector); $printer -> text("Hello World!\n"); $printer -> cut(); $printer -> close();8 package to get started: composer require mike42/escpos-php If you haven't used <?php /* Call this file 'hello-world.php' */ require __DIR__ . '/vendor/autoload.php'; use Mike42\Escpos\PrintConnectors\FilePrintConnector; use Mike42\Escpos\Printer; $connector = new FilePrintConnector("php://stdout"); $printer = new Printer($connector); $printer -> text("Hello World!\n"); $printer -> cut(); $printer -> close();7 before, you can read about it at getcomposer.org. RequirementsThis project has few hard dependencies:
It is also suggested that you install either php hello-world.php | nc 10.x.x.x. 9100 3 or php hello-world.php | nc 10.x.x.x. 9100 4, as these can be used to speed up image processing.A number of optional extensions can be added to enable more specific features. These are described in the "suggest" section of composer.json. The 'Hello World' receiptTo 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. <?php /* Call this file 'hello-world.php' */ require __DIR__ . '/vendor/autoload.php'; use Mike42\Escpos\PrintConnectors\FilePrintConnector; use Mike42\Escpos\Printer; $connector = new FilePrintConnector("php://stdout"); $printer = new Printer($connector); $printer -> text("Hello World!\n"); $printer -> cut(); $printer -> close(); Some examples are below for common interfaces. Communicate with a printer with an Ethernet interface using php hello-world.php | nc 10.x.x.x. 9100 5:php hello-world.php | nc 10.x.x.x. 9100 A USB local printer connected with php hello-world.php | nc 10.x.x.x. 9100 6 on Linux has a device file (Includes USB-parallel interfaces):php hello-world.php > /dev/usb/lp0 A computer installed into the local php hello-world.php | nc 10.x.x.x. 9100 7 server is accessed through php hello-world.php | nc 10.x.x.x. 9100 8 or php hello-world.php | nc 10.x.x.x. 9100 9:php hello-world.php > foo.txt
lpr -o raw -H localhost -P printer foo.txt A local or networked printer on a Windows computer is mapped in to a file, and generally requires you to share the printer first:
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. Using a PrintConnectorTo print receipts from PHP, use the most applicable PrintConnector for your setup. The connector simply provides the plumbing to get data to the printer. For example, a php hello-world.php > /dev/usb/lp0 0 accepts an IP address and port:use Mike42\Escpos\PrintConnectors\NetworkPrintConnector; use Mike42\Escpos\Printer; $connector = new NetworkPrintConnector("10.x.x.x", 9100); $printer = new Printer($connector); try { // ... Print stuff } finally { $printer -> close(); } While a serial printer might use: use Mike42\Escpos\PrintConnectors\FilePrintConnector; use Mike42\Escpos\Printer; $connector = new FilePrintConnector("/dev/ttyS0"); $printer = new Printer($connector); For each OS/interface combination that's supported, there are examples in the compatibility section of how a php hello-world.php > /dev/usb/lp0 1 would be constructed. If you can't get a php hello-world.php > /dev/usb/lp0 1 to work, then be sure to include the working print command in your issue.Using a CapabilityProfileSupport 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. When trying out a new brand of printer, it's a good idea to use the "simple" php hello-world.php > /dev/usb/lp0 3, which instructs the driver to avoid the use of advanced features (generally simpler image handling, ASCII-only text).use Mike42\Escpos\PrintConnectors\WindowsPrintConnector; use Mike42\Escpos\CapabilityProfile; $profile = CapabilityProfile::load("simple"); $connector = new WindowsPrintConnector("smb://computer/printer"); $printer = new Printer($connector, $profile); As another example, Star-branded printers use different commands: use Mike42\Escpos\PrintConnectors\WindowsPrintConnector; use Mike42\Escpos\CapabilityProfile; $profile = CapabilityProfile::load("SP2000") $connector = new WindowsPrintConnector("smb://computer/printer"); $printer = new Printer($connector, $profile); For a list of available profiles, or to have support for your printer improved, please see the upstream receipt-print-hq/escpos-printer-db project. Tips & examplesOn Linux, your printer device file will be somewhere like php hello-world.php > /dev/usb/lp0 4 (parallel), php hello-world.php > /dev/usb/lp0 5 (USB), php hello-world.php > /dev/usb/lp0 6 (USB-Serial), php hello-world.php > /dev/usb/lp0 7 (serial).On Windows, the device files will be along the lines of php hello-world.php > /dev/usb/lp0 8 (parallel) or php hello-world.php > /dev/usb/lp0 9 (serial). Use the php hello-world.php > foo.txt
lpr -o raw -H localhost -P printer foo.txt 0 to tap into system printing on Windows (eg. Windows USB, SMB or Windows LPT) - this submits print jobs via a queue rather than communicating directly with the printer.A complete real-world receipt can be found in the code of Auth in ReceiptPrinter.php. It includes justification, boldness, and a barcode. Other examples are located in the example/ directory. Available methods__construct(PrintConnector $connector, CapabilityProfile $profile)Construct new print object. Parameters:
See example/interface/ for ways to open connections for different platforms and interfaces. barcode($content, $type)Print a barcode. Parameters:
Currently supported barcode standards are (depending on your printer):
Note that some barcode standards can only encode numbers, so attempting to print non-numeric codes with them may result in strange behaviour. bitImage(EscposImage $image, $size)See below. cut($mode, $lines)Cut the paper. Parameters:
feed($lines)Print and feed line / Print and feed n lines. Parameters:
feedForm()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. feedReverse($lines)Print and reverse feed n lines. Parameters:
graphics(EscposImage $image, $size)Print an image to the printer. Parameters:
Size modifiers are:
A minimal example: <?php /* Call this file 'hello-world.php' */ require __DIR__ . '/vendor/autoload.php'; use Mike42\Escpos\PrintConnectors\FilePrintConnector; use Mike42\Escpos\Printer; $connector = new FilePrintConnector("php://stdout"); $printer = new Printer($connector); $printer -> text("Hello World!\n"); $printer -> cut(); $printer -> close();0 See the example/ folder for detailed examples. The function takes the same parameters, and can be used if your printer doesn't support the newer graphics commands. As an additional fallback, the use Mike42\Escpos\PrintConnectors\NetworkPrintConnector; use Mike42\Escpos\Printer; $connector = new NetworkPrintConnector("10.x.x.x", 9100); $printer = new Printer($connector); try { // ... Print stuff } finally { $printer -> close(); }5 function is also provided. initialize()Initialize printer. This resets formatting back to the defaults. pdf417Code($content, $width, $heightMultiplier, $dataColumnCount, $ec, $options)Print a two-dimensional data code using the PDF417 standard. Parameters:
pulse($pin, $on_ms, $off_ms)Generate a pulse, for opening a cash drawer if one is connected. The default settings (0, 120, 240) should open an Epson drawer. Parameters:
qrCode($content, $ec, $size, $model)Print the given data as a QR code on the printer.
selectPrintMode($mode)Select print mode(s). Parameters:
Several MODE_* constants can be OR'd together passed to this function's use Mike42\Escpos\PrintConnectors\WindowsPrintConnector; use Mike42\Escpos\CapabilityProfile; $profile = CapabilityProfile::load("SP2000") $connector = new WindowsPrintConnector("smb://computer/printer"); $printer = new Printer($connector, $profile);1 argument. The valid modes are:
setBarcodeHeight($height)Set barcode height. Parameters:
setBarcodeWidth($width)Set barcode bar width. Parameters:
setColor($color)Select print color - on printers that support multiple colors. Parameters:
setDoubleStrike($on)Turn double-strike mode on/off. Parameters:
setEmphasis($on)Turn emphasized mode on/off. Parameters:
setFont($font)Select font. Most printers have two fonts (Fonts A and B), and some have a third (Font C). Parameters:
setJustification($justification)Select justification. Parameters:
setLineSpacing($height)Set the height of the line. Some printers will allow you to overlap lines with a smaller line feed. Parameters:
setPrintLeftMargin($margin)Set print area left margin. Reset to default with <?php /* Call this file 'hello-world.php' */ require __DIR__ . '/vendor/autoload.php'; use Mike42\Escpos\PrintConnectors\FilePrintConnector; use Mike42\Escpos\Printer; $connector = new FilePrintConnector("php://stdout"); $printer = new Printer($connector); $printer -> text("Hello World!\n"); $printer -> cut(); $printer -> close();14. Parameters:
setPrintWidth($width)Set print area width. This can be used to add a right margin to the print area. Reset to default with <?php /* Call this file 'hello-world.php' */ require __DIR__ . '/vendor/autoload.php'; use Mike42\Escpos\PrintConnectors\FilePrintConnector; use Mike42\Escpos\Printer; $connector = new FilePrintConnector("php://stdout"); $printer = new Printer($connector); $printer -> text("Hello World!\n"); $printer -> cut(); $printer -> close();14. Parameters:
setReverseColors($on)Set black/white reverse mode on or off. In this mode, text is printed white on a black background. Parameters:
setTextSize($widthMultiplier, $heightMultiplier)Set the size of text, as a multiple of the normal size. Parameters:
setUnderline($underline)Set underline for printed text. Parameters:
text($str)Add text to the buffer. Text should either be followed by a line-break, or <?php /* Call this file 'hello-world.php' */ require __DIR__ . '/vendor/autoload.php'; use Mike42\Escpos\PrintConnectors\FilePrintConnector; use Mike42\Escpos\Printer; $connector = new FilePrintConnector("php://stdout"); $printer = new Printer($connector); $printer -> text("Hello World!\n"); $printer -> cut(); $printer -> close();28 should be called after this. Parameters:
Posts I've written up for people who are learning how to use receipt printers:
This code is MIT licensed, and you are encouraged to contribute any modifications back to the project. For development, it's suggested that you load php hello-world.php | nc 10.x.x.x. 9100 3, php hello-world.php | nc 10.x.x.x. 9100 4 and <?php /* Call this file 'hello-world.php' */ require __DIR__ . '/vendor/autoload.php'; use Mike42\Escpos\PrintConnectors\FilePrintConnector; use Mike42\Escpos\Printer; $connector = new FilePrintConnector("php://stdout"); $printer = new Printer($connector); $printer -> text("Hello World!\n"); $printer -> cut(); $printer -> close();33 PHP extensions. The tests are executed on Travis CI over PHP 7.3, 7.4 and 8.0. Older versions of PHP are not supported in the current release, nor is HHVM. |