php-aidc/标签打印机

轻松在多种标签打印机上创建和打印标签

v0.4 2021-11-17 13:36 UTC

This package is auto-updated.

Last update: 2024-09-17 20:22:53 UTC


README

Latest Version on Packagist Testing Quality Score Code Coverage Total Downloads License MIT

PhpAidc LabelPrinter 是一个库,帮助您通过 TCP/IP 在支持直接协议、指纹、TSPL/TSPL2 语言(Honeywell、Intermec、TSC)的打印机上创建和打印标签。

需求

  • PHP 7.1+
  • ext-mbstring

安装

标签打印机通过 Composer 安装

composer require php-aidc/label-printer

当然,您也可以手动编辑您的 composer.json 文件

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

基本用法

一些类似 TSPL2 的打印机,如 Atol BP41/Rongta RP410,不支持所有 TSPL2 功能。

从打印机读取数据

use PhpAidc\LabelPrinter\Printer;
use PhpAidc\LabelPrinter\Connector\NetworkConnector;

$printer = new Printer(new NetworkConnector('192.168.x.x'));

\var_dump($printer->ask('? VERSION$(0)'));

// "Direct Protocol  10.15.017559   \r\n"

创建和打印标签

use PhpAidc\LabelPrinter\Enum\Unit;
use PhpAidc\LabelPrinter\Enum\Anchor;
use PhpAidc\LabelPrinter\Enum\Charset;
use PhpAidc\LabelPrinter\Printer;
use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;
use PhpAidc\LabelPrinter\CompilerFactory;
use PhpAidc\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))
;

(new Printer(new NetworkConnector('192.168.x.x'), CompilerFactory::tspl()))->print($label);

仅对特定语言添加元素

use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;
use PhpAidc\LabelPrinter\Language\Tspl;
use PhpAidc\LabelPrinter\Language\Fingerprint;

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

如果某些值是 truthy,则添加元素

use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;

$text = '';

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

打印图像

use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;
use PhpAidc\LabelPrinter\Language\Tspl;
use PhpAidc\LabelPrinter\Language\Fingerprint;

$image = new \Imagick('gift.svg');

$label = Label::create()
    ->for(Fingerprint::class, static function (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, static function (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))
;

使用仿真打印文本

use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\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())
;

文本将使用 Imagick 绘制并以位图形式打印。

指定副本数量

use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;

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

批量打印

use PhpAidc\LabelPrinter\Printer;
use PhpAidc\LabelPrinter\Label\Batch;
use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;
use PhpAidc\LabelPrinter\CompilerFactory;
use PhpAidc\LabelPrinter\Connector\NetworkConnector;

$batch = (new Batch())
    ->add(Label::create()->add(Element::textLine(168, 95, 'Hello!', 'Univers', 8)))
    ->add(Label::create()->add(Element::textLine(168, 95, 'Bye!', 'Univers', 8)))
;

(new Printer(new NetworkConnector('192.168.x.x'), CompilerFactory::fingerprint()))->print($label);

许可证

PhpAidc LabelPrinter 是开源软件,许可协议为 MIT 协议

一些想法来自 mike42/escpos-php