0%

Zpl print in Laravel

The method for printing barcodes in Laravel is to use the Zebra ZPL. The Zebra ZPL is a standard for printing barcodes, you can simplely treat it as that you manually build some plain text and send it to the printer via socket.

Zebra ZPL

You can easily design your barcode by this online editor
http://staging.advanced-technology-group.com/

Or you can download a free tool to design your barcode, personal recommend this method
https://zpldesigner.com/

after that just copy your zpl code and treat it as your barcode template, later just replace the barcode data with your own data in code.

Sample code of Zebra ZPL

1
2
3
4
5
6
^XA
^CF0,100
^FO220,50^FDPantech-A02-06^FS
^BY4,3,70
^FO150,210^BCN,280,N^FDSLF-Pantech-A02-06^FS
^XZ

the printed out barcode is like this

Laravel

Zpl print helper

create a simple helper ZplPrinter.php in your App/Helper folder for the ZPL printing in Laravel.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

namespace App\Helper;

class ZplPrinter
{

/**
* socket
*
* @var mixed
*/
protected $socket;

/**
* constructor
*
* @param mixed $host
* @param mixed $port
* @return void
*/
public function __construct(string $host, int $port = 9100)
{
$this->connect($host, $port);
}

/**
* destructor
*
* @return void
*/
public function __destruct()
{
$this->disconnect();
}

/**
* create an instance to manipulate printer
*
* @param mixed $host
* @param mixed $port
* @return self
*/
public static function printer(string $host, int $port = 9100): self
{
return new static($host, $port);
}

/**
* connect to the printer
*
* @param mixed $host
* @param mixed $port
* @return void
*/
protected function connect(string $host, int $port): void
{
$this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$this->socket || !@socket_connect($this->socket, $host, $port)) {
$error = $this->getLastError();
report($error);
}
}

/**
* disconnect to the printer
*
* @return void
*/
protected function disconnect(): void
{
@socket_close($this->socket);
}

/**
* send ZPL data to printer.
*
* @param mixed $zpl
* @return void
*/
public function send(string $zpl): void
{
if (!@socket_write($this->socket, $zpl)) {
$error = $this->getLastError();
report($error);
}
}

/**
* getLastError
*
* @return array
*/
protected function getLastError(): array
{
$code = socket_last_error($this->socket);
$message = socket_strerror($code);
return compact('code', 'message');
}
}

Combine Zpl helper to your main helper

Create a Helper.php file in your helper folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php

namespace App\Helper;

use Illuminate\Support\Facades\Auth;
use App\Helper\ZplPrinter;
use Illuminate\Support\Facades\Log;

class Helper
{
//print barcode function
public static function printBarcode(string $content)
{
$zpl = "";
//get the type from barcode from front 4 caracters
$type = substr($content, 0, 4);
if ($type == "SLF-") {
$zpl = "^XA
^CF0,100
^FO220,50^FD" . substr($content, 4) . "^FS
^BY4,3,70
^FO150,210^BCN,280,N^FD" . $content . "^FS
^XZ";
} else {
$zpl = "";
}

//send to printer
$zpl_ip = "your printer ip";
//trim zpl
$zpl = trim($zpl);
//Log::info("IP:" . $zpl_ip . " Barcode:" . $zpl);
if (!empty($zpl_ip) and !empty($zpl)) {
ZplPrinter::printer($zpl_ip)->send($zpl);
}
}
}

1
2
3
4
//print barcode 
//as the upper sample code , I pass "SLF-Pantech-A02-06" as the barcode data
$barcode = "SLF-" . $warehouse_name . "-" . $shelf->name . "-" . sprintf('%02d', $i);
Helper::printBarcode($barcode);