Generate ZPL code with PHP and print to zebra chro

2019-07-09 04:24发布

问题:

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;