PHP Class Mike42\Escpos\Printer

Copyright (c) 2014-2015 Michael Billington , incorporating modifications by: - Roni Saha - Gergely Radics - Warren Doyle Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. This class generates ESC/POS printer control commands for compatible printers. See README.md for a summary of compatible printers and supported commands, and basic usage. See example/demo.php for a detailed print-out demonstrating the range of commands implemented in this project. Note that some functions have not been implemented: - Set paper sensors - Select print colour Please direct feature requests, bug reports and contributions to escpos-php on Github: - https://github.com/mike42/escpos-php
Show file Open project: mike42/escpos-php Class Usage Examples

Public Methods

Method Description
__construct ( Mike42\Escpos\PrintConnectors\PrintConnector $connector, CapabilityProfile $profile = null ) Construct a new print object
barcode ( string $content, integer $type = Printer::BARCODE_CODE39 ) Print a barcode.
bitImage ( EscposImage $img, integer $size = Printer::IMG_DEFAULT ) Print an image, using the older "bit image" command. This creates padding on the right of the image, if its width is not divisible by 8.
bitImageColumnFormat ( EscposImage $img, integer $size = Printer::IMG_DEFAULT ) Print an image, using the older "bit image" command in column format.
close ( ) Close the underlying buffer. With some connectors, the job will not actually be sent to the printer until this is called.
cut ( integer $mode = Printer::CUT_FULL, integer $lines = 3 ) Cut the paper.
feed ( integer $lines = 1 ) Print and feed line / Print and feed n lines.
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 ( integer $lines = 1 ) Print and reverse feed n lines.
getCharacterTable ( ) : number
getPrintBuffer ( ) : Mike42\Escpos\PrintBuffers\PrintBuffer
getPrintConnector ( ) : Mike42\Escpos\PrintConnectors\PrintConnector
getPrinterCapabilityProfile ( ) : CapabilityProfile
graphics ( EscposImage $img, integer $size = Printer::IMG_DEFAULT ) Print an image to the printer.
initialize ( ) Initialize printer. This resets formatting back to the defaults.
pdf417Code ( string $content, number $width = 3, number $heightMultiplier = 3, number $dataColumnCount, real $ec = 0.1, number $options = Printer::PDF417_STANDARD ) Print a two-dimensional data code using the PDF417 standard.
pulse ( integer $pin, integer $on_ms = 120, integer $off_ms = 240 ) Generate a pulse, for opening a cash drawer if one is connected.
qrCode ( string $content, integer $ec = Printer::QR_ECLEVEL_L, integer $size = 3, integer $model = Printer::QR_MODEL_2 ) Print the given data as a QR code on the printer.
selectCharacterTable ( integer $table ) Switch character table (code page) manually. Used in conjunction with textRaw() to print special characters which can't be encoded automatically.
selectPrintMode ( integer $mode = Printer::MODE_FONT_A ) Select print mode(s).
setBarcodeHeight ( integer $height = 8 ) Set barcode height.
setBarcodeTextPosition ( $position = Printer::BARCODE_TEXT_NONE ) Set the position for the Human Readable Interpretation (HRI) of barcode characters.
setBarcodeWidth ( integer $width = 3 ) Set barcode bar width.
setColor ( integer $color = Printer::COLOR_1 ) Select print color on printers that support multiple colors.
setDoubleStrike ( boolean $on = true ) Turn double-strike mode on/off.
setEmphasis ( boolean $on = true ) Turn emphasized mode on/off.
setFont ( integer $font = Printer::FONT_A ) Select font. Most printers have two fonts (Fonts A and B), and some have a third (Font C).
setJustification ( integer $justification = Printer::JUSTIFY_LEFT ) Select justification.
setLineSpacing ( integer $height = null ) Set the height of the line.
setPrintBuffer ( Mike42\Escpos\PrintBuffers\PrintBuffer $buffer ) Attach a different print buffer to the printer. Buffers are responsible for handling text output to the printer.
setPrintLeftMargin ( integer $margin ) Set print area left margin. Reset to default with Printer::initialize()
setPrintWidth ( integer $width = 512 ) Set print area width. This can be used to add a right margin to the print area.
setReverseColors ( boolean $on = true ) Set black/white reverse mode on or off. In this mode, text is printed white on a black background.
setTextSize ( integer $widthMultiplier, integer $heightMultiplier ) Set the size of text, as a multiple of the normal size.
setUnderline ( integer $underline = Printer::UNDERLINE_SINGLE ) Set underline for printed text.
text ( string $str = "" ) Add text to the buffer.
textChinese ( string $str = "" ) Add Chinese text to the buffer. This is a specific workaround for the common Zijang printer- The printer will be switched to a two-byte mode and sent GBK-encoded text.
textRaw ( string $str = "" ) Add text to the buffer without attempting to interpret chararacter codes.

Protected Methods

Method Description
validateBoolean ( boolean $test, string $source ) Throw an exception if the argument given is not a boolean
validateFloat ( float $test, float $min, float $max, string $source, string $argument = "Argument" ) Throw an exception if the argument given is not a float within the specified range
validateInteger ( integer $test, integer $min, integer $max, string $source, string $argument = "Argument" ) Throw an exception if the argument given is not an integer within the specified range
validateIntegerMulti ( integer $test, array $ranges, string $source, string $argument = "Argument" ) Throw an exception if the argument given is not an integer within one of the specified ranges
validateString ( string $test, string $source, string $argument = "Argument" ) Throw an exception if the argument given can't be cast to a string
validateStringRegex ( string $test, string $source, string $regex, string $argument = "Argument" ) Throw an exception if the argument doesn't match the given regex.

Private Methods

Method Description
dataHeader ( array $inputs, boolean $long = true ) : string Convert widths and heights to characters. Used before sending graphics to set the size.
intLowHigh ( integer $input, integer $length ) Generate two characters for a number: In lower and higher parts, or more parts as needed.
wrapperSend2dCodeData ( string $fn, string $cn, string $data = '', string $m = '' ) Wrapper for GS ( k, to calculate and send correct data length.
wrapperSendGraphicsData ( string $m, string $fn, string $data = '' ) Wrapper for GS ( L, to calculate and send correct data length.

Method Details

__construct() public method

Construct a new print object
public __construct ( Mike42\Escpos\PrintConnectors\PrintConnector $connector, CapabilityProfile $profile = null )
$connector Mike42\Escpos\PrintConnectors\PrintConnector The PrintConnector to send data to. If not set, output is sent to standard output.
$profile CapabilityProfile Supported features of this printer. If not set, the "default" CapabilityProfile will be used, which is suitable for Epson printers.

barcode() public method

Print a barcode.
public barcode ( string $content, integer $type = Printer::BARCODE_CODE39 )
$content string The information to encode.
$type integer The barcode standard to output. Supported values are `Printer::BARCODE_UPCA`, `Printer::BARCODE_UPCE`, `Printer::BARCODE_JAN13`, `Printer::BARCODE_JAN8`, `Printer::BARCODE_CODE39`, `Printer::BARCODE_ITF`, `Printer::BARCODE_CODABAR`, `Printer::BARCODE_CODE93`, and `Printer::BARCODE_CODE128`. If not specified, `Printer::BARCODE_CODE39` will be used. Note that some barcode formats only support specific lengths or sets of characters, and that available barcode types vary between printers.

bitImage() public method

Should only be used if your printer does not support the graphics() command. See also bitImageColumnFormat().
public bitImage ( EscposImage $img, integer $size = Printer::IMG_DEFAULT )
$img EscposImage The image to print
$size integer Size modifier for the image. Must be either `Printer::IMG_DEFAULT` (default), or any combination of the `Printer::IMG_DOUBLE_HEIGHT` and `Printer::IMG_DOUBLE_WIDTH` flags.

bitImageColumnFormat() public method

Should only be used if your printer does not support the graphics() or bitImage() commands.
public bitImageColumnFormat ( EscposImage $img, integer $size = Printer::IMG_DEFAULT )
$img EscposImage The image to print
$size integer Size modifier for the image. Must be either `Printer::IMG_DEFAULT` (default), or any combination of the `Printer::IMG_DOUBLE_HEIGHT` and `Printer::IMG_DOUBLE_WIDTH` flags.

close() public method

Close the underlying buffer. With some connectors, the job will not actually be sent to the printer until this is called.
public close ( )

cut() public method

Cut the paper.
public cut ( integer $mode = Printer::CUT_FULL, integer $lines = 3 )
$mode integer Cut mode, either Printer::CUT_FULL or Printer::CUT_PARTIAL. If not specified, `Printer::CUT_FULL` will be used.
$lines integer Number of lines to feed

feed() public method

Print and feed line / Print and feed n lines.
public feed ( integer $lines = 1 )
$lines integer Number of lines to feed

feedForm() public method

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.
public feedForm ( )

feedReverse() public method

Print and reverse feed n lines.
public feedReverse ( integer $lines = 1 )
$lines integer number of lines to feed. If not specified, 1 line will be fed.

getCharacterTable() public method

public getCharacterTable ( ) : number
return number

getPrintBuffer() public method

public getPrintBuffer ( ) : Mike42\Escpos\PrintBuffers\PrintBuffer
return Mike42\Escpos\PrintBuffers\PrintBuffer

getPrintConnector() public method

public getPrintConnector ( ) : Mike42\Escpos\PrintConnectors\PrintConnector
return Mike42\Escpos\PrintConnectors\PrintConnector

getPrinterCapabilityProfile() public method

public getPrinterCapabilityProfile ( ) : CapabilityProfile
return CapabilityProfile

graphics() public method

Size modifiers are: - Printer::IMG_DEFAULT (leave image at original size) - Printer::IMG_DOUBLE_WIDTH - Printer::IMG_DOUBLE_HEIGHT See the example/ folder for detailed examples. The functions bitImage() and bitImageColumnFormat() take the same parameters, and can be used if your printer doesn't support the newer graphics commands.
public graphics ( EscposImage $img, integer $size = Printer::IMG_DEFAULT )
$img EscposImage The image to print.
$size integer Size modifier for the image. Must be either `Printer::IMG_DEFAULT` (default), or any combination of the `Printer::IMG_DOUBLE_HEIGHT` and `Printer::IMG_DOUBLE_WIDTH` flags.

initialize() public method

Initialize printer. This resets formatting back to the defaults.
public initialize ( )

pdf417Code() public method

Print a two-dimensional data code using the PDF417 standard.
public pdf417Code ( string $content, number $width = 3, number $heightMultiplier = 3, number $dataColumnCount, real $ec = 0.1, number $options = Printer::PDF417_STANDARD )
$content string Text or numbers to store in the code
$width number Width of a module (pixel) in the printed code. Default is 3 dots.
$heightMultiplier number Multiplier for height of a module. Default is 3 times the width.
$dataColumnCount number 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.
$ec real Error correction ratio, from 0.01 to 4.00. Default is 0.10 (10%).
$options number Standard code Printer::PDF417_STANDARD with start/end bars, or truncated code Printer::PDF417_TRUNCATED with start bars only.

pulse() public method

The default settings should open an Epson drawer.
public pulse ( integer $pin, integer $on_ms = 120, integer $off_ms = 240 )
$pin integer 0 or 1, for pin 2 or pin 5 kick-out connector respectively.
$on_ms integer pulse ON time, in milliseconds.
$off_ms integer pulse OFF time, in milliseconds.

qrCode() public method

Print the given data as a QR code on the printer.
public qrCode ( string $content, integer $ec = Printer::QR_ECLEVEL_L, integer $size = 3, integer $model = Printer::QR_MODEL_2 )
$content string The content of the code. Numeric data will be more efficiently compacted.
$ec integer 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.
$size integer Pixel size to use. Must be 1-16 (default 3)
$model integer 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).

selectCharacterTable() public method

Switch character table (code page) manually. Used in conjunction with textRaw() to print special characters which can't be encoded automatically.
public selectCharacterTable ( integer $table )
$table integer The table to select. Available code tables are model-specific.

selectPrintMode() public method

Several MODE_* constants can be OR'd together passed to this function's $mode argument. The valid modes are: - Printer::MODE_FONT_A - Printer::MODE_FONT_B - Printer::MODE_EMPHASIZED - Printer::MODE_DOUBLE_HEIGHT - Printer::MODE_DOUBLE_WIDTH - Printer::MODE_UNDERLINE
public selectPrintMode ( integer $mode = Printer::MODE_FONT_A )
$mode integer The mode to use. Default is Printer::MODE_FONT_A, with no special formatting. This has a similar effect to running initialize().

setBarcodeHeight() public method

Set barcode height.
public setBarcodeHeight ( integer $height = 8 )
$height integer Height in dots. If not specified, 8 will be used.

setBarcodeTextPosition() public method

Set the position for the Human Readable Interpretation (HRI) of barcode characters.
public setBarcodeTextPosition ( $position = Printer::BARCODE_TEXT_NONE )

setBarcodeWidth() public method

Set barcode bar width.
public setBarcodeWidth ( integer $width = 3 )
$width integer Bar width in dots. If not specified, 3 will be used. Values above 6 appear to have no effect.

setColor() public method

Select print color on printers that support multiple colors.
public setColor ( integer $color = Printer::COLOR_1 )
$color integer Color to use. Must be either Printer::COLOR_1 (default), or Printer::COLOR_2.

setDoubleStrike() public method

Turn double-strike mode on/off.
public setDoubleStrike ( boolean $on = true )
$on boolean true for double strike, false for no double strike

setEmphasis() public method

@param boolean $on true for emphasis, false for no emphasis
public setEmphasis ( boolean $on = true )
$on boolean

setFont() public method

Select font. Most printers have two fonts (Fonts A and B), and some have a third (Font C).
public setFont ( integer $font = Printer::FONT_A )
$font integer The font to use. Must be either Printer::FONT_A, Printer::FONT_B, or Printer::FONT_C.

setJustification() public method

Select justification.
public setJustification ( integer $justification = Printer::JUSTIFY_LEFT )
$justification integer One of Printer::JUSTIFY_LEFT, Printer::JUSTIFY_CENTER, or Printer::JUSTIFY_RIGHT.

setLineSpacing() public method

Some printers will allow you to overlap lines with a smaller line feed.
public setLineSpacing ( integer $height = null )
$height integer The height of each line, in dots. If not set, the printer will reset to its default line spacing.

setPrintBuffer() public method

Attach a different print buffer to the printer. Buffers are responsible for handling text output to the printer.
public setPrintBuffer ( Mike42\Escpos\PrintBuffers\PrintBuffer $buffer )
$buffer Mike42\Escpos\PrintBuffers\PrintBuffer The buffer to use.

setPrintLeftMargin() public method

Set print area left margin. Reset to default with Printer::initialize()
public setPrintLeftMargin ( integer $margin )
$margin integer The left margin to set on to the print area, in dots.

setPrintWidth() public method

Reset to default with Printer::initialize()
public setPrintWidth ( integer $width = 512 )
$width integer The width of the page print area, in dots.

setReverseColors() public method

Set black/white reverse mode on or off. In this mode, text is printed white on a black background.
public setReverseColors ( boolean $on = true )
$on boolean True to enable, false to disable.

setTextSize() public method

Set the size of text, as a multiple of the normal size.
public setTextSize ( integer $widthMultiplier, integer $heightMultiplier )
$widthMultiplier integer Multiple of the regular height to use (range 1 - 8)
$heightMultiplier integer Multiple of the regular height to use (range 1 - 8)

setUnderline() public method

Argument can be true/false, or one of UNDERLINE_NONE, UNDERLINE_SINGLE or UNDERLINE_DOUBLE.
public setUnderline ( integer $underline = Printer::UNDERLINE_SINGLE )
$underline integer Either true/false, or one of Printer::UNDERLINE_NONE, Printer::UNDERLINE_SINGLE or Printer::UNDERLINE_DOUBLE. Defaults to Printer::UNDERLINE_SINGLE.

text() public method

Text should either be followed by a line-break, or feed() should be called after this to clear the print buffer.
public text ( string $str = "" )
$str string Text to print

textChinese() public method

Support for this will be merged into a print buffer.
public textChinese ( string $str = "" )
$str string Text to print, as UTF-8

textRaw() public method

Text should either be followed by a line-break, or feed() should be called after this to clear the print buffer.
public textRaw ( string $str = "" )
$str string Text to print

validateBoolean() protected static method

Throw an exception if the argument given is not a boolean
protected static validateBoolean ( boolean $test, string $source )
$test boolean the input to test
$source string the name of the function calling this

validateFloat() protected static method

Throw an exception if the argument given is not a float within the specified range
protected static validateFloat ( float $test, float $min, float $max, string $source, string $argument = "Argument" )
$test float the input to test
$min float the minimum allowable value (inclusive)
$max float the maximum allowable value (inclusive)
$source string the name of the function calling this
$argument string the name of the invalid parameter

validateInteger() protected static method

Throw an exception if the argument given is not an integer within the specified range
protected static validateInteger ( integer $test, integer $min, integer $max, string $source, string $argument = "Argument" )
$test integer the input to test
$min integer the minimum allowable value (inclusive)
$max integer the maximum allowable value (inclusive)
$source string the name of the function calling this
$argument string the name of the invalid parameter

validateIntegerMulti() protected static method

Throw an exception if the argument given is not an integer within one of the specified ranges
protected static validateIntegerMulti ( integer $test, array $ranges, string $source, string $argument = "Argument" )
$test integer the input to test
$ranges array array of two-item min/max ranges.
$source string the name of the function calling this
$argument string the name of the invalid parameter

validateString() protected static method

Throw an exception if the argument given can't be cast to a string
protected static validateString ( string $test, string $source, string $argument = "Argument" )
$test string the input to test
$source string the name of the function calling this
$argument string the name of the parameter being validated

validateStringRegex() protected static method

Throw an exception if the argument doesn't match the given regex.
protected static validateStringRegex ( string $test, string $source, string $regex, string $argument = "Argument" )
$test string the input to test
$source string the name of the function calling this
$regex string valid values for this attribute, as a regex
$argument string the name of the parameter being validated