Skip to content

Repository files navigation

PhpAidc LabelPrinter

Latest Version on PackagistTestingQuality ScoreCode CoverageTotal DownloadsLicense MIT

PhpAidc LabelPrinter is a library that help you create and print labels on printers that support Direct Protocol, Fingerprint, TSPL/TSPL2 languages (Honeywell, Intermec, TSC) via TCP/IP.


Requirements

  • PHP 7.1+
  • ext-mbstring

Installation

LabelPrinter is installed via Composer:

composer require php-aidc/label-printer

You can of course also manually edit your composer.json file

{
"require": {
"php-aidc/label-printer": "v0.4"
}
}

Basic usage

Some TSPL2-like printers, such as Atol BP41/Rongta RP410, do not support all TSPL2 features.

Read data from printer

usePhpAidc\LabelPrinter\Printer;
usePhpAidc\LabelPrinter\Connector\NetworkConnector;
$printer = newPrinter(newNetworkConnector('192.168.x.x'));
\var_dump($printer->ask('? VERSION$(0)'));
// "Direct Protocol 10.15.017559 \r\n"

Create and print label

usePhpAidc\LabelPrinter\Enum\Unit;
usePhpAidc\LabelPrinter\Enum\Anchor;
usePhpAidc\LabelPrinter\Enum\Charset;
usePhpAidc\LabelPrinter\Printer;
usePhpAidc\LabelPrinter\Label\Label;
usePhpAidc\LabelPrinter\Label\Element;
usePhpAidc\LabelPrinter\CompilerFactory;
usePhpAidc\LabelPrinter\Connector\NetworkConnector;
$label = Label::create(Unit::MM(), 43, 25)
->charset(Charset::UTF8())
->add(Element::textBlock(168, 95, 'Hello!', 'Univers', 8)->box(338, 100, 0)->anchor(Anchor::CENTER()))
->add(Element::barcode(10, 10, '123456', 'CODE93')->height(60))
;
(newPrinter(newNetworkConnector('192.168.x.x'), CompilerFactory::tspl()))->print($label);

Add elements only for a specific language

usePhpAidc\LabelPrinter\Label\Label;
usePhpAidc\LabelPrinter\Label\Element;
usePhpAidc\LabelPrinter\Language\Tspl;
usePhpAidc\LabelPrinter\Language\Fingerprint;
$label = Label::create()
->for(Fingerprint::class, staticfunction (Label$label) {
$label->add(Element::textLine(168, 95, 'Hello!', 'Univers', 8));
})
->for(Tspl::class, staticfunction (Label$label) {
$label->add(Element::textLine(10, 10, 'Hello!', 'ROMAN.TTF', 8));
})
;

Add elements if some value is truthy

usePhpAidc\LabelPrinter\Label\Label;
usePhpAidc\LabelPrinter\Label\Element;
$text = '';
$label = Label::create()
->when($text, staticfunction (Label$label, $text) {
// will not be added until the $text is empty$label->add(Element::textLine(168, 95, $text, 'Univers', 8));
})
;

Print images

usePhpAidc\LabelPrinter\Label\Label;
usePhpAidc\LabelPrinter\Label\Element;
usePhpAidc\LabelPrinter\Language\Tspl;
usePhpAidc\LabelPrinter\Language\Fingerprint;
$image = new \Imagick('gift.svg');
$label = Label::create()
->for(Fingerprint::class, staticfunction (Label$label) {
// from printer's memory — png, bmp, pcx$label->add(Element::intImage(10, 10, 'GLOBE.1'));
// from filesystem$label->add(Element::extImage(10, 10, \realpath('alien.png')));
})
->for(Tspl::class, staticfunction (Label$label) {
// from printer's memory — bmp, pcx$label->add(Element::intImage(10, 10, 'ALIEN.BMP'));
})
// from filesystem via Imagick — any supported types
->add(Element::bitmap(50, 10, $image))
;

Print text with emulation

usePhpAidc\LabelPrinter\Label\Label;
usePhpAidc\LabelPrinter\Label\Element;
$label = Label::create()
->add(Element::textLine(10, 10, 'Hello!', '/path/to/font/roboto.ttf', 20)->emulate())
->add(Element::textBlock(100, 10, 'Hello again!', '/path/to/font/roboto.ttf', 20)->box(300, 20)->emulate())
;

Text will be drawn with Imagick and printed as bitmap.

Specify the number of copies

usePhpAidc\LabelPrinter\Label\Label;
usePhpAidc\LabelPrinter\Label\Element;
$label = Label::create()
->add(Element::textLine(168, 95, 'Hello!', 'Univers', 8))
->copies(3)
;

Batch printing

usePhpAidc\LabelPrinter\Printer;
usePhpAidc\LabelPrinter\Label\Batch;
usePhpAidc\LabelPrinter\Label\Label;
usePhpAidc\LabelPrinter\Label\Element;
usePhpAidc\LabelPrinter\CompilerFactory;
usePhpAidc\LabelPrinter\Connector\NetworkConnector;
$batch = (newBatch())
->add(Label::create()->add(Element::textLine(168, 95, 'Hello!', 'Univers', 8)))
->add(Label::create()->add(Element::textLine(168, 95, 'Bye!', 'Univers', 8)))
;
(newPrinter(newNetworkConnector('192.168.x.x'), CompilerFactory::fingerprint()))->print($label);

License

The PhpAidc LabelPrinter is open-sourced software licensed under the MIT license.

Some ideas taken from mike42/escpos-php.

About

A PHP package for label printers (Honeywell, Intermec, TSC)

Topics

Resources

Stars

59 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages