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 editorhttp://staging.advanced-technology-group.com/
Or you can download a free tool to design your barcode, personal recommend this methodhttps://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 { protected $socket ; public function __construct (string $host , int $port = 9100 ) { $this ->connect($host , $port ); } public function __destruct ( ) { $this ->disconnect(); } public static function printer (string $host , int $port = 9100 ): self { return new static ($host , $port ); } 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 ); } } protected function disconnect ( ): void { @socket_close($this ->socket); } public function send (string $zpl ): void { if (!@socket_write($this ->socket, $zpl )) { $error = $this ->getLastError(); report($error ); } } 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 { public static function printBarcode (string $content ) { $zpl = "" ; $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 = "" ; } $zpl_ip = "your printer ip" ; $zpl = trim($zpl ); if (!empty ($zpl_ip ) and !empty ($zpl )) { ZplPrinter::printer($zpl_ip )->send($zpl ); } } }
Print barcode from your cotroller or whatever place you want 1 2 3 4 $barcode = "SLF-" . $warehouse_name . "-" . $shelf ->name . "-" . sprintf('%02d' , $i );Helper::printBarcode($barcode );