Skip to content

Commit c9855a7

Browse files
committed
Initial commit
0 parents  commit c9855a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+3907
-0
lines changed

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/tests export-ignore
2+
.gitignore export-ignore
3+
.travis.yml export-ignore
4+
.styleci.yml export-ignore
5+
.gitattributes export-ignore
6+
.scrutinizer.yml export-ignore

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
vendor/
3+
composer.lock

LICENSE.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright © appwilio <appwilio.com>
4+
Copyright © JhaoDa <[email protected]>
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<h1 align="center">PhpAidc LabelPrinter</h1>
2+
3+
<p align="center">
4+
<a href="https://raw.githubusercontent.com/php-aidc/label-printer/master/LICENSE.md"><img src="https://img.shields.io/badge/license-MIT-428F7E" alt="License MIT"></a>
5+
</p>
6+
7+
PhpAidc LabelPrinter is a library that help you create and print labels on printers that support
8+
Direct Protocol, Fingerprint, TSPL/TSPL2 languages (Honeywell, Intermec, TSC) via TCP/IP.
9+
10+
---
11+
12+
- [Requirements](#requirements)
13+
- [Installation](#installation)
14+
- [Basic usage](#basic-usage)
15+
16+
## Requirements
17+
- PHP 7.1+
18+
- ext-mbstring
19+
20+
## Installation
21+
22+
LabelPrinter is installed via [Composer](https://getcomposer.org/):
23+
```bash
24+
composer require php-aidc/label-printer
25+
```
26+
27+
You can of course also manually edit your composer.json file
28+
```json
29+
{
30+
"require": {
31+
"php-aidc/label-printer": "v0.1"
32+
}
33+
}
34+
```
35+
36+
## Basic usage
37+
38+
> Some TSPL2-like printers, such as Atol BP41/Rongta RP410, do not support all TSPL2 features.
39+
40+
##### Read data from printer
41+
42+
```php
43+
use PhpAidc\LabelPrinter\Printer;
44+
use PhpAidc\LabelPrinter\Connector\NetworkConnector;
45+
46+
$printer = new Printer(new NetworkConnector('192.168.x.x'));
47+
48+
\var_dump($printer->ask('? VERSION$(0)'));
49+
// "Direct Protocol 10.15.017559 \r\n"
50+
```
51+
52+
##### Create and print label
53+
```php
54+
use PhpAidc\LabelPrinter\Enum\Unit;
55+
use PhpAidc\LabelPrinter\Enum\Anchor;
56+
use PhpAidc\LabelPrinter\Enum\Charset;
57+
use PhpAidc\LabelPrinter\Printer;
58+
use PhpAidc\LabelPrinter\Label\Label;
59+
use PhpAidc\LabelPrinter\Label\Element;
60+
use PhpAidc\LabelPrinter\Connector\NetworkConnector;
61+
62+
$printer = new Printer(new NetworkConnector('192.168.x.x'));
63+
64+
$label = Label::create(Unit::MM(), 43, 25)
65+
->charset(Charset::UTF8())
66+
->add(Element::textBlock(168, 95, 'Hello!', 'Univers', 8)->box(338, 100, 0)->anchor(Anchor::CENTER()))
67+
->add(Element::barcode(10, 10, '123456', 'CODE93')->height(60));
68+
69+
$printer->print($label);
70+
```
71+
72+
##### Add elements for a specific language
73+
```php
74+
use PhpAidc\LabelPrinter\Label\Label;
75+
use PhpAidc\LabelPrinter\Label\Element;
76+
use PhpAidc\LabelPrinter\Language\Tspl;
77+
use PhpAidc\LabelPrinter\Language\Fingerprint;
78+
79+
$label = Label::create()
80+
->when(Fingerprint::class, static function (Label $label) {
81+
$label->add(Element::textLine(168, 95, 'Hello!', 'Univers', 8));
82+
})
83+
->when(Tspl::class, static function (Label $label) {
84+
$label->add(Element::textLine(10, 10, 'Hello!', 'ROMAN.TTF', 8));
85+
});
86+
```
87+
88+
##### Print images
89+
```php
90+
use PhpAidc\LabelPrinter\Label\Label;
91+
use PhpAidc\LabelPrinter\Label\Element;
92+
use PhpAidc\LabelPrinter\Language\Tspl;
93+
use PhpAidc\LabelPrinter\Language\Fingerprint;
94+
95+
$image = new \Imagick('gift.svg');
96+
$image->scaleImage(100, 100);
97+
98+
$label = Label::create()
99+
->when(Fingerprint::class, static function (Label $label) {
100+
// from printer's memory — png, bmp, pcx
101+
$label->add(Element::intImage(10, 10, 'GLOBE.1'));
102+
// from filesystem
103+
$label->add(Element::extImage(10, 10, \realpath('alien.png')));
104+
})
105+
->when(Tspl::class, static function (Label $label) {
106+
// from printer's memory — bmp, pcx
107+
$label->add(Element::intImage(10, 10, 'ALIEN.BMP'));
108+
})
109+
// from filesystem via Imagick — any supported types
110+
->add(Element::bitmap(50, 10, $image));
111+
```
112+
113+
##### Print text with emulation
114+
```php
115+
use PhpAidc\LabelPrinter\Label\Label;
116+
use PhpAidc\LabelPrinter\Label\Element;
117+
118+
$label = Label::create()
119+
->add(Element::textLine(10, 10, 'Hello!', '/path/to/font/roboto.ttf', 20)->emulate())
120+
->add(Element::textBlock(100, 10, 'Hello again!', '/path/to/font/roboto.ttf', 20)->box(300, 20)->emulate());
121+
```
122+
Text will be drawn with Imagick and printed as bitmap.
123+
124+
## License
125+
126+
The PhpAidc LabelPrinter is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).
127+
128+
> Some ideas taken from [mike42/escpos-php](https://github.com/mike42/escpos-php).

composer.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "php-aidc/label-printer",
3+
"description": "Easily create and print labels on various label printers",
4+
"keywords": [
5+
"DirectProtocol", "Fingerprint", "TSPL", "TSPL2",
6+
"Honeywell", "Intermec", "TSC",
7+
"aidc", "pos", "wms", "barcode", "label", "print", "printer"
8+
],
9+
"homepage": "https://github.com/php-aidc/label-printer",
10+
"type": "library",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "JhaoDa",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"support": {
19+
"issues": "https://github.com/php-aidc/label-printer/issues"
20+
},
21+
"require": {
22+
"php": ">=7.1",
23+
"ext-mbstring": "*",
24+
"myclabs/php-enum": "^1.7"
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "~7.5"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"PhpAidc\\LabelPrinter\\": "src/",
32+
"PhpAidc\\LabelPrinter\\Tests\\Unit\\": "tests/Unit/"
33+
}
34+
},
35+
"suggest": {
36+
"ext-imagick": "Required to print unsupported image formats, PDF and text with custom fonts."
37+
},
38+
"config": {
39+
"sort-packages": true
40+
},
41+
"prefer-stable": true
42+
}

phpunit.xml.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="tests/bootstrap.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="PhpAidc LabelPrinter Unit Tests">
14+
<directory>tests/Unit</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/Command/Barcode.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PhpAidc LabelPrinter package.
5+
*
6+
* © Appwilio (https://appwilio.com)
7+
* © JhaoDa (https://github.com/jhaoda)
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
declare(strict_types=1);
14+
15+
namespace PhpAidc\LabelPrinter\Command;
16+
17+
use PhpAidc\LabelPrinter\Contract\Command;
18+
use PhpAidc\LabelPrinter\Command\Concerns\Alignable;
19+
use PhpAidc\LabelPrinter\Command\Concerns\Rotatable;
20+
use PhpAidc\LabelPrinter\Command\Concerns\PositionAware;
21+
22+
final class Barcode implements Command
23+
{
24+
use Alignable;
25+
use Rotatable;
26+
use PositionAware;
27+
28+
/** @var string */
29+
private $data;
30+
31+
/** @var string */
32+
private $type;
33+
34+
/** @var int|null */
35+
private $height;
36+
37+
/** @var int|null */
38+
private $magnification;
39+
40+
/** @var array */
41+
private $ratio = ['narrow' => 1, 'wide' => 1];
42+
43+
/** @var bool */
44+
private $readable = false;
45+
46+
public function __construct(int $x, int $y, string $data, string $type)
47+
{
48+
$this->x = $x;
49+
$this->y = $y;
50+
$this->data = $data;
51+
$this->type = $type;
52+
}
53+
54+
public function height(int $value)
55+
{
56+
$this->height = $value;
57+
58+
return $this;
59+
}
60+
61+
public function magnify(int $value)
62+
{
63+
$this->magnification = $value;
64+
65+
return $this;
66+
}
67+
68+
public function ratio(int $narrow, int $wide)
69+
{
70+
$this->ratio = \compact('narrow', 'wide');
71+
72+
return $this;
73+
}
74+
75+
public function readable(bool $readable = true)
76+
{
77+
$this->readable = $readable;
78+
79+
return $this;
80+
}
81+
public function getType(): string
82+
{
83+
return $this->type;
84+
}
85+
86+
public function getData(): string
87+
{
88+
return $this->data;
89+
}
90+
91+
public function getHeight(): ?int
92+
{
93+
return $this->height;
94+
}
95+
96+
public function getMagnification(): ?int
97+
{
98+
return $this->magnification;
99+
}
100+
101+
public function getRatio(): array
102+
{
103+
return $this->ratio;
104+
}
105+
106+
public function isReadable(): bool
107+
{
108+
return $this->readable;
109+
}
110+
}

0 commit comments

Comments
 (0)