PHP code example of andersonls / zpl

1. Go to this page and download the library: Download andersonls/zpl 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/ */

    

andersonls / zpl example snippets



se millimeters for coordinates and dimensions
use Zpl\Enums\Unit;
use Zpl\Enums\Align;
$driver = new \Zpl\ZplBuilder(Unit::MM);

// Set encoding (optional)
$driver->setEncoding(28);

$driver->setFont('0', 16);
$driver->setXY(0, 0);
$driver->drawCell(100, 10, 'Hello World', true, true, Align::CENTER);

$zpl = $driver->toZpl();

\Zpl\Printer::printer('192.168.1.1')->send($zpl);


Zpl\Enums\Unit;
$driver = new \Zpl\ZplBuilder(Unit::MM);
$driver->setFontMapper(new \Zpl\Fonts\Generic());
$driver->setDpi(300); // change printer DPI when needed

// Draw shapes
$driver->drawRect(5, 5, 50, 30);
$driver->drawCircle(60, 5, 25);

// Text with explicit coordinates
use Zpl\Enums\Orientation;
$driver->drawText(5, 40, 'Product: ABC-123', Orientation::NORMAL);

// Code 128 barcode (x, y, height, data, print human-readable?)
$driver->drawCode128(5, 50, 20, 'ABC123456789', true);

// QR Code (x, y, data, module size)
$driver->drawQrCode(50, 50, 'https://example.com/product/ABC-123', 6);

// Add an image/graphic. width is optional and will scale the graphic field in dots
$driver->drawGraphic(120, 10, __DIR__ . '/logo.png', 200);

// Add custom/pre/post raw commands (you can use ^XA, ^XZ and any other ZPL commands)
$driver->addPreCommand('^LH0,0'); // set label home
$driver->addPostCommand('^PQ1');  // print quantity 1

// New page (adds a new label) - useful if you want to print multiple labels in one ZPL payload
$driver->newPage();
$driver->drawText(5, 5, 'Second label');

// You can call arbitrary ZPL commands using method-like calls (dynamic): CF is same as ^CF
$driver->CF('A', 30); // ^CFA,30  (be careful with arguments and expected format)

\Zpl\Printer::printer('192.168.1.100')->send($driver->toZpl());

$builder = new \Zpl\ZplBuilder('mm');
\Zpl\ZplBuilder::macro('drawCustomBox', function($x, $y, $w, $h) {
    $this->drawRect($x, $y, $w, $h);
    $this->drawText($x + 2, $y + 2, 'Custom');
});

$builder->drawCustomBox(10, 10, 50, 20);