how to write a zpl code inside a php script and how to send it to a zebra printer for printing

瘦欲@ 提交于 2019-12-05 08:04:40

问题


I want to print a bar code on a label using zebra label printer.Barcode printer model is Zebra GK420d . Sticker print area is 5 cm by 10 cm.I want to do it from a php script.By googling I found some examples and implemented in this way

$barcode = "sometext";
$labelcode =<<<AAA
^XA
^FO100,75
^BCN, 100,Y, N,
^FD$barcode^FS
^XZ
AAA;

file_put_contents('/dev/lpt1',$labelcode);

Will it work when i connect the printer and test?What are the settings that i have to apply for this zebra printer in order to print.I have no idea on zebra printers settings.And also file_put_contents will copy the code to the printer by using the port.how to find the port of the printer that is connected to the system.if it by usb what info we have to pass to the file_put_contents.Please suggest the zebra printing process


回答1:


While you could hypothetically send the raw ZPL / EPL commands to the printer device, you might be better off not doing that if you don't already know ZPL / EPL, and if you can already generate images in your environment.

Your code implies that you're on a Unix-like system. If you're on a recent Unix-like system, then printing should be controlled by CUPS. Zebra has published unsupported but mostly functional CUPS support files.

Set up the printer in CUPS, then shell out to /usr/bin/lp with the -d flag set to the printer name, and a -o ppi=... value to set the DPI of the printer, and possibly other things to force alignment or portrait/landscape mode. That GK420s is a 203 DPI printer, so you'd want -o ppi=203 at the very minimum.

You can then print anything to that printer that CUPS can understand, including images and PDF documents. This allows you to composite anything you want PHP-side, not restricting you to command language that the printer understands. For example, we use wkhtmltoimage to build shipping labels, while we use GD and PEAR's prehistoric Image_Barcode to produce small barcode labels. There are better options for that, by the way.

Alternatively, you can set up a "Generic Raw" virtual printer in CUPS. You can then print command language text files directly through that printer. You should probably only do it that way if you are comfortable and familiar with EPL or ZPL.

The following code is a cut down section of real, live code that we use to print to all of our printers, including Zebras. Just call the function below with the data you want printed (such as the contents of an image, or text, or whatever) as the first argument.

function print_example($data) {
// You'll need to change these according to your local names and options.
    $server = 'printserver.companyname.com';
    $printer_name = 'Zebra_ZP_500_KB'; // That's effectively the same thing as your GK420d
    $options_flag = '-o position=bottom-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();
    $process = proc_open($command, $handles, $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)) {
        add_warning("Print to '$printer' failed.  Please check the printer status.");
        return false;
    }
    add_notice("Print to '$printer' succeeded.  Job $request_match[1].");
    return true;
}

The functions add_warning and add_notice are implemented in our code, you'll need to replace them as appropriate for whatever you're actually printing.



来源:https://stackoverflow.com/questions/19316982/how-to-write-a-zpl-code-inside-a-php-script-and-how-to-send-it-to-a-zebra-printe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!