I am trying to set up a web based POS system and have got everything working apart from printing directly to a barcode / label printer.

When printing to the barode printer I need to send it a text stream of ascii characters with none printable control characters. Here's an example <STX> is the ascii stx character (HEX 0x02) <CR> is the ascii line termination character (HEX 0x0D)

<STX>L<CR>
<STX>Kc<CR>
1234512345example<CR>
E

Does anyone know how I could generate this stream and send it to the printer (or windows print spool)?

Thanks for any help.
Mike

Dani AI

Generated

For : the task is two parts — produce the exact binary stream the printer expects, and get those bytes to the device without a driver or spooler altering them. 's pointer to the Windows printer API and 's UNC suggestion are both valid routes; choose based on whether the printer is networked or shared from a Windows host and on what account the web server runs under.

A common and reliable method for networked label printers is to open a raw TCP connection (most accept port 9100) and send the binary payload built with chr() or \x escapes. Example:

$fp = stream_socket_client("tcp://192.168.1.50:9100", $errno, $errstr, 3);
if ($fp) {
    $raw = chr(2) . "YOUR_PRINTER_COMMANDS" . chr(13);
    fwrite($fp, $raw);
    fclose($fp);
}

If the printer is shared from Windows, you can often write directly to the share from PHP (the webserver account must have print permission). Example:

$share = '\\\\SERVER\\PRINTERNAME';
$fh = fopen($share, 'wb');   // binary mode
fwrite($fh, $rawBytes);
fclose($fh);

Troubleshooting tips: test connectivity with telnet/nc to the printer IP and port 9100; save the exact bytes to a file and inspect with bin2hex() to verify control characters; run a PHP CLI test under the same user as the webserver to isolate permission issues. If you want a spooling API on Windows instead of raw writes, the PECL printer package is an option. See the PHP socket docs for the network approach and the PECL package for the Windows API option: stream_socket_client manual pecl printer package.

Recommended Answers

All 2 Replies

Hi there, I have no experience with printing directly from PHP but here's what I found in PHP manual. I hope it helps.

$handle=printer_open("EPSON TM-T88III Receipt");
printer_set_option($handle, PRINTER_MODE, "RAW");
printer_write($handle, $yourRawData);
printer_close($handle);

You may need to specify the server name before the printer name.

printer_open('\\SERVERNAME\PRINTERNAME');

More information on the printer functions can be found here:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.