2

Ok I downloaded the Chrome ZPL addon for testing with zebra printers. But now I like to print the following:

         $qrcode = "012039444";
        $name = "Matthew Pitt";
        $jobtitle = "CTO funny man";
        $company = "Google light company";
        $labelcode =<<<AAA
        ^XA
        ^FX Left section with QR code.this part doesn't seem to work in simulator
        ^FO100,100
        ^BQN,2,10
        ^FD$qrcode^FS

        ^FX Right section with name and job title.
        ^CF0,35,35^FO330,50
        ^FB300,7,,
        ^FD$name\&\&$jobtitle^FS
        ^FO50,500^GB700,1,3^FS

        ^FX Bottom section only company name 
        ^CF0,35,35^FO30,350
        ^FB400,2,,
        ^FD$company^FS
        ^XZ
        AAA;
        print_example($labelcode);
    //print_example('Hello world');
        function print_example($data) {
        // You'll need to change these according to your local names and options.
            $server = '127.0.0.1:9100';
            $printer_name = 'zpl'; // That's effectively the same thing as your GK420d
            $options_flag = '-o position=top-left,ppi=203,landscape';
            $process_name = 'LC_ALL=en_US.UTF-8 /usr/bin/lp -h %s -d %s %s';
            $command = sprintf($process_name, $server, $printer_name, (string)$options_flag);
            $pipes = array();
            $descriptors = array(
    0 => array("pipe", "r"),  // STDIN
    1 => array("pipe", "w"),  // STDOUT
    2 => array("pipe", "w")   // STDERR
);
            $process = proc_open($command, $descriptors, $pipes);
        // Couldn't open the pipe -- shouldn't happen
            if (!is_resource($process))
                trigger_error('Printing failed, proc_open() did not return a valid resource handle', E_USER_FATAL);
        // $pipes now looks like this:
        // 0 => writeable handle connected to child stdin
        // As we've been given data to write directly, let's kinda like do that.
            fwrite($pipes[0], $data);
            fclose($pipes[0]);
        // 1 => readable handle connected to child stdout
            $stdout = fgets($pipes[1]);
            fclose($pipes[1]);
        // 2 => readable handle connected to child stderr
            $stderr = fgets($pipes[2]);
            fclose($pipes[2]);
        // It is important that you close any pipes before calling
        // proc_close in order to avoid a deadlock
            $return_value = proc_close($process);
        // We've asked lp not to be quiet about submitting jobs so we can make
        // sure that the print job was submitted.
            $request_match = array();
            if (!preg_match('/request id is\b(.+)/', $stdout, $request_match)) {
                echo ("Print to '$printer_name' failed.  Please check the printer status.");
                return false;
            }
            echo ("Print to '$printer_name' succeeded.  Job $request_match[1].");
            return true;
        }

[EDIT] is this the right command to call the printer

LC_ALL=en_US.UTF-8 /usr/bin/lp -h zpl -d 127.0.0.1:9100 -o position=top-left,ppi=203,landscape

[EDIT 2] I am trying with this php

     $command = 'lp -h 127.0.0.1:9100 -d zpl ' . $labelcode . ' -o position=top-left,ppi=203,landscape';
$pipes = array();
$descriptors = array(
    0 => array("pipe", "r"), // STDIN
    1 => array("pipe", "w"), // STDOUT
    2 => array("pipe", "w")   // STDERR
);
$process = proc_open($command, $descriptors, $pipes);
echo 'test: ' . $process;
alex
  • 4,804
  • 14
  • 51
  • 86
  • The [answer that you got `print_example()` from](http://stackoverflow.com/a/20294527/168868) was intended for a processed image, with have CUPS using the Zebra driver to transform the image into ZPL for the printer to consume. If you have ZPL, you can (should?) communicate *directly* with the printer, as there's no need for the driver to transform it. – Charles Jan 14 '16 at 21:03
  • hmm, ok but could i sent $labelcode directly to the Zpl printer by using proc_open and the compiled $command variable ? I like to test with sending the $labelcode to the Zpl printer – alex Jan 14 '16 at 21:30
  • The Chrome ZPL emulator works great, you should be able to print directly to it. The default buffer of 4K is a bit low, I'd recommend bumping that up. If the web server is on a different host, you may need to disable your firewall or white-list port 9100. Also, the IP address of the `Zpl Printer` should match in the config and PHP code. If you need to talk to a physical raw device that's not on the network here are some other (client-side) solutions https://stackoverflow.com/a/28783269/3196753 – tresf Mar 21 '19 at 15:02

0 Answers0