PHP code example of surda / zebra

1. Go to this page and download the library: Download surda/zebra library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

surda / zebra example snippets


$client = new \Surda\Zebra\Client\Client(host: '192.168.0.100');
$client->send(zpl: '^XA.......^XZ');

$label = new \Surda\Zebra\Label\DummyDataLabel();
$printer = new \Surda\Zebra\Printer\Printer(name: 'name', host: '192.168.0.100');
$dispatcher = new \Surda\Zebra\Dispatcher\Dispatcher();
$dispatcher->dispatch(label: $label, printer: $printer);

class OrderLabel implements \Surda\Zebra\Label\ILabel
{
    public function __construct(private Order $order)
    {
    }

    public function getData(): string
    {
        $template = new \Surda\Zebra\Template\Template(
            __DIR__ . '/OrderLabel.latte',
            [
                'order' => $this->order,
                'quantity' => 1,
            ]
        );
        return (new \Surda\Zebra\Label\TemplateLabel($template))->getData();
    }
}

interface OrderLabelFactory
{
    public function create(Order $order)): OrderLabel;
}

class LabelPrintManager
{
    public function __construct(
        private \Surda\Zebra\Printer\IPrinter $printer, 
        private \Surda\Zebra\Dispatcher\Dispatcher $dispatcher,
    )
    {
    }

    public function print(ILabel $label): void
    {
        try {
            $this->dispatcher->dispatch($label, $this->printer);
        } catch (DispatchException) {
            throw new \Surda\Zebra\PrintException();
        }
    }
}

#[Inject]
public OrderLabelFactory $orderLabelFactory;

#[Inject]
public LabelPrintManager $labelPrintManager;

public function print() {
    $order = new Order();
    $label = $this->orderLabelFactory->create($order);
    $this->labelPrintManager->print($label);
}