The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Printer::ESCPOS::Profiles::Generic - Generic Profile for Printers for Printer::ESCPOS. Most common functions are included here.

VERSION

version 1.006

METHODS

init

Initializes the Printer. Clears the data in print buffer and resets the printer to the mode that was in effect when the power was turned on. This function is automatically called on creation of printer object.

enable

Enables/Disables the printer with a '_ESC =' command (Set peripheral device). When disabled, the printer ignores all commands except enable() or other real-time commands.

Pass 1 to enable, pass 0 to disable

    $device->printer->enable(0) # disabled
    $device->printer->enable(1) # enabled

qr

Prints a qr code to the printer. In Generic profile, this creates a QR Code image using GD::Barcode::QRcode. A native implementation may be created using a printer model specific profile.

    $device->printer->qr('Print this QR Code');
    $device->printer->qr('WIFI:T:WPA;S:ShantanusWifi;P:wifipasswordhere;;')  # Create a QR code for connecting to a Wifi

You may also pass in optional QR Code format parameters like Ecc, Version and moduleSize. Read more about these params at http://www.qrcode.com/en/about/version.html.

string: String to be printed as QR code.

ecc (optional, default 'L'): error correction level. There are four available error correction schemes in QR codes.

  • Level L - up to 7% damage

  • Level M - up to 15% damage

  • Level Q - up to 25% damage

  • Level H - up to 30% damage

version (optional, default 5): The symbol versions of QR Code range from Version 1 to Version 40. Each version has a different module configuration or number of modules. (The module refers to the black and white dots that make up QR Code.)

Each QR Code symbol version has the maximum data capacity according to the amount of data, character type and error correction level. In other words, as the amount of data increases, more modules are required to comprise QR Code, resulting in larger QR Code symbols.

moduleSize (optional, default 3): width of each module in pixels.

    my $ecc = 'L'; # Default value
    my $version = 5; # Default value
    my $moduleSize = 3; # Default value
    $device->printer->qr("Don't Panic!", $ecc, $version, $moduleSize);

You may also call align() before calling qr() to set alignment on the page.

utf8ImagedText

    use utf8;

    $device->printer->utf8ImagedText("Hello World\x{8A9E}",
      fontFamily => "Rubik",
      fontStyle => "Normal",
      fontSize => 25,
      lineHeight => 40
    );

This method uses native fonts to print utf8 compatible characters including international wide characters. This method is slower than direct text printing but it allows exceptional styling options allowing you to print text using system fonts in a wide range of font sizes and styles with many more choices than what a thermal printer otherwise provides.

In the background this function uses Pango and Cairo libraries to create a one line image from a given font styles, font family in a given font size. Note that you must not use this method to print more than a single line at a time. When you want to print the next line call this method again to print to the next line.

string: String to be printed in the line.

fontFamily (optional, default 'Purisa'): Font family to use. On linux systems with font config installed use the following command to choose from the list of available fonts:

    fc-list | sed 's/.*:\(.*,\|\s\)\(.*\):.*/\2/'

You may also install more fonts from https://fonts.google.com/ to your system fonts( copy the font to /usr/share/fonts )

fontStyle (optional, default 'Normal'): Font style like Bold, Normal, Italic etc.

fontSize (optional, default 20): Font size

lineHeight (optional, default 42): Line Height in pixels, make sure this is bigger than the font height in pixels for your chosen font size.

paperWidth (optional, default 500): This is set to 500 pixels by default as this is the most common width for receipt printers. Change this as per your printer specs.

image

Prints a image to the printer. Takes a GD Image object as input. <Maximum printable image dimensions are 512x255

image: GD image object for the image to be printed.

    use GD;

    my $image = newFromGif GD::Image('header.gif') || die "Error $!";
    $device->printer->image($image);

You may also call align() before calling qr() to set alignment on the page.

printAreaWidth

Sets the Print area width specified by width.

    width x basic calculated pitch

width: width is a 16 bits value range, i.e. int between 0 to 65535 specifying print area width in basic calculated pitch. This command is effective only when processed at the beginning of the line when standard mode is being used. Printable area width setting is effective until init is executed, the printer is reset, or the power is turned off.

    $device->printer->printAreaWidth( $width );

Note: If you are using Printer::ESCPOS version prior to v1.* Please check documentation for older version of this module the nL and nH syntax for this method.

tabPositions

Sets horizontal tab positions for tab stops. Upto 32 tab positions can be set in most receipt printers.

tabPositions: a list of positions for tab().

    $device->printer->tabPositions( 5, 9, 13 );

    for my $plu (@plus) {
        $device->printer->text($plu->{quantity});
        $device->printer->tab();
        $device->printer->text(' x ' . $plu->{name});
        $device->printer->tab();
        $device->printer->text('$' . $plu->{price});
    }

This would print a well aligned receipt like so:

    10 x Guiness Beer              $24.00
    2  x Pizza                     $500.50
    1  x Tandoori Chicken          $50.20

Common tab positions are usually in intervals of 8 chars (9, 17, 25) etc.

tab

moves the cursor to next horizontal tab position like a "\t". This command is ignored unless the next horizontal tab position has been set. You may substitute this command with a "\t" as well.

This

    $device->printer->text("blah blah");
    $device->printer->tab();
    $device->printer->text("blah2 blah2");

is same as this

    $device->printer->text("blah blah\tblah2 blah2");

lf

line feed. Moves to the next line. You can substitute this method with {"\n"} in your print or write method e.g. :

This

    $device->printer->text("blah blah");
    $device->printer->lf();
    $device->printer->text("blah2 blah2");

is same as this

    $device->printer->text("blah blah\nblah2 blah2");

ff

When in page mode, print data in the buffer and return back to standard mode

cr

Print and carriage return

When automatic line feed is enabled this method works the same as lf , else it is ignored.

cancel

Cancel (delete) page data in page mode

font

Set Font style, you can pass *a*, *b* or *c*. Many printers don't support style *c* and only have two supported styles.

font (optional, default 'a'): Font to set for the printer

    $device->printer->font('a');
    $device->printer->text('Writing in Font A');
    $device->printer->font('b');
    $device->printer->text('Writing in Font B');

bold

Set bold mode *0* for off and *1* for on. Also called emphasized mode in some printer manuals

bold (optional, default 0): 1 or 0 to set or unset bold.

    $device->printer->bold(1);
    $device->printer->text("This is Bold Text\n");
    $device->printer->bold(0);
    $device->printer->text("This is not Bold Text\n");

doubleStrike

Set double-strike mode *0* for off and *1* for on

doubleStrike (optional, default 0): 1 or 0 to doubleStrike or unset doubleStrike.

    $device->printer->doubleStrike(1);
    $device->printer->text("This is Double Striked Text\n");
    $device->printer->doubleStrike(0);
    $device->printer->text("This is not Double Striked  Text\n");

underline

Set underline, *0* for off, *1* for on and *2* for double thickness

underline (optional, default 0): 1 or 0 to underline or unset underline.

    $device->printer->underline(1);
    $device->printer->text("This is Underlined Text\n");
    $device->printer->underline(2);
    $device->printer->text("This is Underlined Text with thicker underline\n");
    $device->printer->underline(0);
    $device->printer->text("This is not Underlined Text\n");

invert

Reverse white/black printing mode pass *0* for off and *1* for on

invert (optional, default 0): 1 or 0 to invert or unset invert.

    $device->printer->invert(1);
    $device->printer->text("This is Inverted Text\n");
    $device->printer->invert(0);
    $device->printer->text("This is not Inverted Text\n");

color

Most thermal printers support just one color, black. Some ESCPOS printers(especially dot matrix) also support a second color, usually red. A few rarer models also support upto 7 different colors. In many models, this only works when the color is set at the beginning of a new line before any text is printed. Pass *0* or *1* to switch between the two colors.

color (optional, default 0): color number 0, 1 ...

    $device->printer->lf();
    $device->printer->color(0); #black
    $device->printer->text("black");
    $device->printer->lf();
    $device->printer->color(1); #red
    $device->printer->text("Red");
    $device->printer->print();

justify

Set Justification. Options full, left, right and center

justify (optional, default 'left'): full, left, right or center

    $device->printer->justify( 'right' );
    $device->printer->text("This is right justified");

upsideDown

Sets Upside Down Printing on/off (pass *0* or *1*)

upsideDown (optional, default 0): 0 or 1

    $device->printer->upsideDownPrinting(1);
    $device->printer->text("This text is upside down");

fontHeight

Set font height. Only supports *0* or *1* for printmode set to 1, supports values *0*, *1*, *2*, *3*, *4*, *5*, *6* and *7* for non-printmode state (default)

height (optional, default 0): 0 to 7

    $device->printer->fontHeight(1);
    $device->printer->text("double height\n");
    $device->printer->fontHeight(2);
    $device->printer->text("triple height\n");
    $device->printer->fontHeight(3);
    $device->printer->text("quadruple height\n");
    . . .

fontWidth

Set font width. Only supports *0* or *1* for printmode set to 1, supports values *0*, *1*, *2*, *3*, *4*, *5*, *6* and *7* for non-printmode state (default)

width (optional, default 0): 0 to 7

    $device->printer->fontWidth(1);
    $device->printer->text("double width\n");
    $device->printer->fontWidth(2);
    $device->printer->text("triple width\n");
    $device->printer->fontWidth(3);
    $device->printer->text("quadruple width\n");
    . . .

charSpacing

Sets character spacing takes a value between 0 and 255

charSpacing (optional, default 0): 0 to 255

    $device->printer->charSpacing(5);
    $device->printer->text("Blah Blah Blah\n");
    $device->printer->print();

lineSpacing

Sets line spacing i.e the spacing between each line of printout. Note that some printers may not support all command sets for setting a line spacing. The most commonly available commandSet('3') is used by default.

lineSpacing: ranges from 0 to 255 when commandSet is '+' or '3',

Line spacing is set to lineSpacing/360 of an inch if commandSet is '+', lineSpacing/180 of an inch if commandSet is '3' and lineSpacing/60 of an inch if commandSet is 'A' (default: 30)

commandSet: ESCPOS provides three alternate commands for setting line spacing i.e. '+', '3', 'A' (default : '3').

    $device->printer->lineSpacing($lineSpacing); # Use default commandSet '3'
    $device->printer->lineSpacing($lineSpacing, $commandSet);

selectDefaultLineSpacing

Reverts to default line spacing for the printer

    $device->printer->selectDefaultLineSpacing();

printPosition

Sets the distance from the beginning of the line to the position at which characters are to be printed.

length: ranges from 0 to 255

height: ranges from 0 to 255

    $device->printer->printPosition( $length, $height );

* 0 <= $length <= 255 * 0 <= $height <= 255

leftMargin

Sets the left margin for printing. Set the left margin at the beginning of a line. The printer ignores any data preceding this command on the same line in the buffer.

In page mode sets the left margin to leftMargin x (horizontal motion unit) from the left edge of the printable area

leftMargin: Left Margin, range: 0 to 65535. If the margin exceeds the printable area, the left margin is automatically set to the maximum value of the printable area.

    $device->printer->leftMargin($leftMargin);

Note: If you are using Printer::ESCPOS version prior to v1.* Please check documentation for older version of this module the nL and nH syntax for this method.

rot90

Rotate printout by 90 degrees

rotate (optional, default 0): 0 or 1

    $device->printer->rot90(1);
    $device->printer->text("This is rotated 90 degrees\n");
    $device->printer->rot90(0);
    $device->printer->text("This is not rotated 90 degrees\n");

barcode

This method prints a barcode to the printer. This can be bundled with other text formatting commands at the appropriate point where you would like to print a barcode on your print out. takes argument ~barcode~ as the barcode value.

In the simplest form you can use this command as follows:

    #Default barcode printed in code93 system with a width of 2 and HRI Chars printed below the barcode
    $device->printer->barcode(
        barcode     => 'SHANTANU BHADORIA',
    );

However there are several customizations available including barcode ~system~, ~font~, ~height~ etc.

    my $hripos = 'above';
    my $font   = 'a';
    my $height = 100;
    my $system = 'UPC-A';
    $device->printer->barcode(
        HRIPosition => $hripos,        # Position of Human Readable characters
                                       # 'none','above','below','aboveandbelow'
        font        => $font,          # Font for HRI characters. 'a' or 'b'
        height      => $height,        # no of dots in vertical direction
        system      => $system,        # Barcode system
        width       => 2               # 2:0.25mm, 3:0.375mm, 4:0.5mm, 5:0.625mm, 6:0.75mm
        barcode     => '123456789012', # Check barcode system you are using for allowed
                                       # characters in barcode
    );
    $device->printer->barcode(
        system      => 'CODE39',
        HRIPosition => 'above',
        barcode     => '*1-I.I/ $IA*',
    );
    $device->printer->barcode(
        system      => 'CODE93',
        HRIPosition => 'above',
        barcode     => 'Shan',
    );

HRIPosition (optional, default 'below'): 'none', 'above', 'below', 'aboveandbelow'

font (optional, default 'b'): 'a' or 'b'

height (optional, default 50): height integer between 0 and 255

width (optional, default 50): width integer between 0 and 255

system (optional, default 'CODE93'): UPC-A, UPC-B, JAN13, JAN8, CODE39, ITF, CODABAR, CODE93, CODE128

barcode: String to print as barcode.

printNVImage

Prints bit image stored in Non-Volatile (NV) memory of the printer.

    $device->printer->printNVImage($flag);

flag: height and width

* $flag = 0 # Normal width and Normal Height * $flag = 1 # Double width and Normal Height * $flag = 2 # Normal width and Double Height * $flag = 3 # Double width and Double Height

printImage

Prints bit image stored in Volatile memory of the printer. This image gets erased when printer is reset.

    $device->printer->printImage($flag);

* $flag = 0 # Normal width and Normal Height * $flag = 1 # Double width and Normal Height * $flag = 2 # Normal width and Double Height * $flag = 3 # Double width and Double Height

cutPaper

Cuts the paper,

feed (optional, default 0): if ~feed~ is set to 0 then printer doesnt feed paper to cutting position before cutting it. The default behavior is that the printer doesn't feed paper to cutting position before cutting. One pre-requisite line feed is automatically executed before paper cut though.

    $device->printer->cutPaper( feed => 0 )

While not strictly a text formatting option, in receipt printer the cut paper instruction is sent along with the rest of the text and text formatting data and the printer cuts the paper at the appropriate points wherever this command is used.

drawerKickPulse

Trigger drawer kick. Used to open cash drawer connected to the printer. In some use cases it may be used to trigger other devices by close contact.

    $device->printer->drawerKickPulse( $pin, $time );

pin (optional, default 0): $pin is either 0( for pin 2 ) and 1( for pin5 )

pin (optional, default 8): $time is a value between 1 to 8 and the pulse duration in multiples of 100ms.

For default values use without any params to kick drawer pin 2 with a 800ms pulse

    $device->printer->drawerKickPulse();

Again like cutPaper command this is obviously not a text formatting command but this command is sent along with the rest of the text and text formatting data and the printer sends the pulse at the appropriate points wherever this command is used. While originally designed for triggering a cash drawer to open, in practice this port can be used for all sorts of devices like pulsing light, or sound alarm etc.

printerStatus

Returns printer status in a hashref.

    return {
        drawer_pin3_high            => $flags[5],
        offline                     => $flags[4],
        waiting_for_online_recovery => $flags[2],
        feed_button_pressed         => $flags[1],
    };

offlineStatus

Returns a hashref for paper cover closed status, feed button pressed status, paper end stop status, and a aggregate error status either of which will prevent the printer from processing a printing request.

    return {
        cover_is_closed     => $flags[5],
        feed_button_pressed => $flags[4],
        paper_end           => $flags[2],
        error               => $flags[1],
    };

errorStatus

Returns hashref with error flags for auto_cutter_error, unrecoverable error and auto-recoverable error

    return {
        auto_cutter_error     => $flags[4],
        unrecoverable_error   => $flags[2],
        autorecoverable_error => $flags[1],
    };

paperSensorStatus

Gets printer paper Sensor status. Returns a hashref with four sensor statuses. Two paper near end sensors and two paper end sensors for printers supporting this feature. The exact returned status might differ based on the make of your printer. If any of the flags is set to 1 it implies that the paper is out or near end.

    return {
        paper_roll_near_end_sensor_1 => $flags[5],
        paper_roll_near_end_sensor_2 => $flags[4],
        paper_roll_status_sensor_1 => $flags[2],
        paper_roll_status_sensor_2 => $flags[1],
    };

inkStatusA

Only available for dot-matrix and other ink consuming printers. Gets printer ink status for inkA(usually black ink). Returns a hashref with ink statuses.

    return {
        ink_near_end          => $flags[5],
        ink_end               => $flags[4],
        ink_cartridge_missing => $flags[2],
        cleaning_in_progress  => $flags[1],
    };

inkStatusB

Only available for dot-matrix and other ink consuming printers. Gets printer ink status for inkB(usually red ink). Returns a hashref with ink statuses.

    return {
        ink_near_end          => $flags[5],
        ink_end               => $flags[4],
        ink_cartridge_missing => $flags[2],
    };

AUTHOR

Shantanu Bhadoria <shantanu@cpan.org> https://www.shantanubhadoria.com

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Shantanu Bhadoria.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.