0

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

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I think i need your solution "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" as in my case I as a noob thought I just could spit out a badge pdf layout I generated with PHP (see my question with badge layout: stackoverflow.com/questions/34771587/…), but is this also available for windows and can I print to Google Chrome ZPL addon to test?
sorry i meant this question: stackoverflow.com/questions/34782567/…
Unfortunately, I know nothing about getting PHP to print from Windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.