Getting a USB receipt printer working on Windows

Note:This post is a Windows adaptation of an earlier post, Getting a USB receipt printer working on Linux, mainly in response to these questions.

In this post, I’ll step through how to get a USB thermal receipt printer appearing on Windows. The aim of this is to be able to send raw text to the printer, so that we can point a driver such as escpos-php at it. The printer tested here is once again this Epson TM-T20:

2015-03-printer-back
2015-03-printer-top

The directions below are for Windows 7, so your mileage may vary if you are on an older or newer version.

If you have issues following these steps, make sure you can locate your printer in Device Manager, and that it has “USB Print Support”.

Add the printer

Find Devices and Printers and click Add a Printer.
2015-04-windowsusb-01

2015-04-windowsusb-02

Add it as a Local printer, using the USB virtual port, probably USB0001:

2015-04-windowsusb-03
2015-04-windowsusb-04

Use the Generic / Text Only driver.

2015-04-windowsusb-05

Name the printer whatever you like, and then share it under the same name:

2015-04-windowsusb-06
2015-04-windowsusb-07

At this point, it should pop up in the window in the background, and also prompt you to Print a test page.

2015-04-windowsusb-08
2015-04-windowsusb-09

The test print is plain-text, and depending on your printer, will look something like this:

2015-04-windowsusb-10

Finally, you need to verify that your printer can be accessed locally, by typing \\localhost into Windows Explorer. If all goes to plan, you will see the new printer there too:

2015-04-windowsusb-11

Run a command-line test print

We now know that your printer is working, and can be accessed via its share name (even locally).

Test printing from the command-line. Fire up cmd.exe and try to send it some text to verify that it’s working:

echo "Hello World" > testfile
print /D:"\\%COMPUTERNAME%\Receipt Printer" testfile
del testfile

Printing something useful

This is where you start to see real results. Receipt printers are not just for printing plain-text. Many of them support a standard called ESC/POS, which contains formatting commands.

The snippet below, from this earlier post, generates some basic ESC/POS commands.

Install PHP if you don’t have it already, and call the below code foo.php:

<?php
/* ASCII constants */
const ESC = "\x1b";
const GS="\x1d";
const NUL="\x00";

/* Output an example receipt */
echo ESC."@"; // Reset to defaults
echo ESC."E".chr(1); // Bold
echo "FOO CORP Ltd.\n"; // Company
echo ESC."E".chr(0); // Not Bold
echo ESC."d".chr(1); // Blank line
echo "Receipt for whatever\n"; // Print text
echo ESC."d".chr(4); // 4 Blank lines

/* Bar-code at the end */
echo ESC."a".chr(1); // Centered printing
echo GS."k".chr(4)."987654321".NUL; // Print barcode
echo ESC."d".chr(1); // Blank line
echo "987654321\n"; // Print number
echo GS."V\x41".chr(3); // Cut
exit(0);

You would send generated commands to the printer like this:

php foo.php > testfile
print /D:"\\%COMPUTERNAME%\Receipt Printer" testfile
rm testfile

Scaling this up

The correct ESC/POS codes are quite tricky to generate with manually, which is why I put together the escpos-php driver. You can find more information on that at:

A simple “Hello World” receipt to your Windows shared printer would be scripted as (call this one foo2.php):

<?php
require __DIR__ . '/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;

try {
	// Enter the share name for your USB printer here
	$connector = new WindowsPrintConnector("Receipt Printer");
	$printer = new Printer($connector);

	/* Print a "Hello world" receipt" */
	$printer -> text("Hello World!\n");
	$printer -> cut();
	
	/* Close printer */
	$printer -> close();
} catch(Exception $e) {
	echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
}

This would be sent to the printer by loading it from the web, or running the script on the command-line:

php foo2.php

The full ESC/POS snippet with formatting, coded up with escpos-php, would look like this (call this one foo3.php):

<?php
require __DIR__ . '/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
try {
	// Enter the share name for your USB printer here
	$connector = new WindowsPrintConnector("Receipt Printer");
	$printer = new Printer($connector);

	/* Print some bold text */
	$printer -> setEmphasis(true);
	$printer -> text("FOO CORP Ltd.\n");
	$printer -> setEmphasis(false);
	$printer -> feed();
	$printer -> text("Receipt for whatever\n");
	$printer -> feed(4);

	/* Bar-code at the end */
	$printer -> setJustification(Printer::JUSTIFY_CENTER);
	$printer -> barcode("987654321");
	
	/* Close printer */
	$printer -> close();
} catch(Exception $e) {
	echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
}

And again, this could be executed by loading the page through the web, or invoking the command directly:

php foo3.php

35 Replies to “Getting a USB receipt printer working on Windows”

  1. Hi Mike,
    I really glad that you create this guide,
    it really helping,
    however, my problem is,
    it cant print if it not connected to a network,
    is it possible?

  2. I purchased a generic USB receipt printer from eBay, and I’m currently running Windows 10, and the drivers go up to 8.1. I tried the 8 and 7 drivers. and I ended up using the XP ones, which worked even better than with the 7 drivers before I upgraded my OS!

  3. Please forgive me…. where is the escpos.php file? Can’t find it on GitHub, not found on composer(I am upgrading because I did see where I got a message about not all packages downloaded because of my 5.5.6 version of php. ) Actually burning up all my data downloading v7 now lol Xampp

  4. Hi, Mike.

    Thank You for the great library.

    I have an issue I cannot understand how to solve.
    When processing pdf 2 httpd.exe command prompt windows flashes for a second.

    Is it possible to get these windows running hidden?

  5. Thank you so much!!! I was trying to send commands to a STAR POS and I was about to give up!!!

    Now I will figure how to send commands from android via OTG!

  6. I purchased a generic USB receipt printer from eBay, and I’m currently running Windows 10, and the drivers go up to 8.1. I tried the 8 and 7 drivers. and I ended up using the XP ones, which worked even better than with the 7 drivers before I upgraded my OS!

  7. Hi Mike,
    I am using Eskay thermal printer. Everything is working great except it will not print barcode or QR code. Is there anything I am missing?

  8. @Simon – Your printer may not understand the ESC/POS commands for QR codes. You can send it an image instead – check out the code I’ve got at this blog post for printing software-rendered QR codes via phpqrcode.

  9. HI Mike
    I am a beginner when comes to printing. I have written a code for invoice and now i need to print this. However, my code will be running from a chrome on a android table. i.e. a waiter taking an order and printer is in kitchen.

    Can you guide me what setup i need to do ?

  10. Hi Mike
    I’m currently having trouble on setting up my Generic Printer in a Windows Network Do you have any solution on how to prevent printer from simultaneously printing when its only one pc that is printing?

  11. @Walid – If you add the printer on one computer and then share it, then Windows will take care of queuing the print jobs and sending them one at a time.

  12. Hello Mike,
    Does your driver work with the following receipt printer : Epson TM-T20 [Link removed]

    Thanks in advance!

  13. Hi Mike, I have been getting to grips with a printer and I tried your instructions but I keep getting an error ref call to undefined function for print buffer, am I missing something simple?

  14. We have a web application from that application we print receipt on thermal printer. If print from thermal printer from android and from chromer is there any support?

  15. Any idea how I can get a generic thermal printer working on Windows 10? Its a Symcode MJ-T583

  16. I have Windows 7 and followed your instructions to the letter for setting up a POS printer and verified it is shared and will print text when I use an application such as notepad. However, when I execute the command: print /D:”\%COMPUTERNAME%\POS80″ testfile
    I get the following error message “Unable to initialize device \BILLS-LAPTOP\POS80”
    Does anyone have any ideas of what I’m doing wrong?
    Note that I do not have PHP installed.
    Thanks

  17. Very good contribution! I have a generic thermal printer connected with USB port, could you help me in how to get the status of paper roll in PHP language?

  18. Muy buen aporte!, tengo una impresora térmica generica conectada con puerto USB en sistema de Windows 10, me podrias ayudar de como se hace para obtener el estado de rollo de papel en lenguaje PHP ??

  19. HI mike, I need one help.

    Have one server then two sub systems working from that server. Am connected Printer in USB with that sub system.

    How can i access that printer can you guide me…

  20. Thanks Mike this has really helped me with my problem sending ESC/POS commands to my USB receipt printer from VBA in Access 2007. I changed the command line code to:
    Open “\%Computer Name%\Receipt printer” For Output As #1

    Print #1, Chr$(&H1B); "@"; 'Initializes the printer (ESC @)

    etc.
    You might ask why I did not use the driver which came with the printer and simply print out of a report.
    I needed to print 2 types of receipt. One which required a drawer kick and one which didn’t. You could set this within the printer properties but of course then it is the same for each print event.

  21. Your Codes really work well! and it is easy to learn ~~ I do appreciate so much on your sharing..

  22. Hi Mike,
    I have two questions
    1) How to print with 2 decimal points? For eg. the price is $6 but the printer only print 6 instead of 6.00
    2) The are items which is very long and may more than one single line in the receipt. What should be changed to suit this?Any idea?

    Thanks

  23. Hello, i have been able to share the printer and it works fine with every computer connected to the same network.
    I have a usb thermal printer.
    But it doesnt work when printing from chrome.
    I want to be able to print to it through client side in chrome.
    Anyone can help me?

  24. Hello Mike,

    I am getting the below error in windows. And also want to know how it will work in iPad without installing any software.

    Couldn’t print to this printer: Failed to print. Command “smbclient ‘//ip-172-31-33-120/TM-m10’ -c ‘print -‘ -N -m SMB2” failed with exit code 1: tree connect failed: NT_STATUS_BAD_NETWORK_NAME

  25. Hi Mike!
    I’m trying to make it work, the text, emphasis and feeds are working on a thermal printer, but the barcode, qrcode, image or pdf don’t print. Is there something you can tell me to do? Every dependency is installed for PHP 7.2.
    I’ve tested the ethernet print (linux server/windows with printer) and the local through windows usb example.
    Thanks

  26. Mike Good Job done, but am having this error what is the possible cause
    fatal error
    Class ‘Mike42\Escpos\PrintConnectors\WindowsPrintConnector’ not found in
    C:\Apache24\htdocs\demos\PrintReceit.php on ine 8

  27. Meanwhile i included:
    require DIR . ‘/autoload.php’;
    use Mike42\Escpos\Printer;
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
    try {
    $connector = new WindowsPrintConnector(“ReceiptPrinter1”);
    $printer = new Printer($connector);

    But still the same error

  28. Al ejecutar el siguiente código, funciona pero no me imprime nada en la impresora.

    require DIR . ‘/autoload.php’;
    use Mike42\Escpos\Printer;
    use Mike42\Escpos\PrintConnectors\FilePrintConnector;
    use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;

    try{
    $nombre_impresora = “XP-450B”;

    $connector = new WindowsPrintConnector($nombre_impresora);
    $printer = new Printer($connector);

    print_r($connector);
    echo "<br>";
    //print_r($printer);
    $printer -> initialize();
    $printer -> setJustification(1);

    $printer -> text("Hello World!");

    $printer -> cut();
    $printer -> close();

    } catch (Exception $e) {
    echo “No hemos podido imprimir el ticket: “.$e->getMessage().”\n”;
    }

  29. HENDRY You can bypass the need of lan or wifi network by installing the Microsoft Loopback Network Adapter, you can add it in Add Network device in Microsoft adapters.

    Now you can print shared local printer freely.

Leave a Reply

Your email address will not be published. Required fields are marked *