New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print from USB connected printer under Windows #14

Closed
thryang opened this Issue Apr 6, 2015 · 21 comments

Comments

Projects
None yet
8 participants
@thryang
Copy link

thryang commented Apr 6, 2015

I'm trying to find a way to print receipts from my project. (It's SO difficult to find a good answer from the Web.):
I'm using Apache 2.4, PHP 5.6.3 and MySQL 5.6 on Windows 7 Pro 64-bit, with USB connected Epson TM-T20 II.

I'm so glad to find your wonderful work here. But when I used the "EscposTest.php" to test, I got the error msg: "Cannot initialize printer ... ".
(I did change the line in "Escpos.php" under "__construct" to=> $fp = fopen("EPSON TM-T20II Receipt", "wb");. I think I made a mistake somehow!)

My questions are:

  1. Does the codes only worked under Linux? (if it does, do you have anything works under Windows?)
  2. I saw your blog gave examples to setup Ethernet connected printer, but how can I setup the USB connected printer?
  3. How do I setup my printer under your codes in Escpos.php?
  4. I assume that with your codes, I don't need to set the "php-printer" extension under "php.ini" right?

Thanks again for your good job!

@mike42

This comment has been minimized.

Copy link
Owner

mike42 commented Apr 8, 2015

Hi,

Thanks for your query- Windows USB printing was very fiddly until I uploaded some improvements, as you're not the only one to have asked about it.

So in short:

  1. The library is generally cross-platform, so you can use it on anything that runs PHP. Previously, using Windows required you to print the data to a file, and then run a system() command to print it, but this has now been implemented as a built-in, using the new WindowsPrintConnector (example usage)
  2. The original use case for this driver was an Ethernet printer, but it's grown since. Here is a post specifically about Windows USB receipt printing which I've put together for other people who are googling this.
  3. Install the printer as generic / text only, and share it (see blog post linked above for detail). You can then connect to it using the share name:
<?php
require_once(dirname(__FILE__) . "/Escpos.php");
/**
 * Install the printer using USB printing support, and the "Generic / Text Only" driver,
 * then share it (you can use a firewall so that it can only be seen locally).
 * 
 * Use a WindowsPrintConnector with the share name to print.
 * 
 * Troubleshooting: Fire up a command prompt, and ensure that (if your printer is shared as
 * "Receipt Printer), the following commands work:
 * 
 *      echo "Hello World" > testfile
 *      print /D:"\\%COMPUTERNAME%\Receipt Printer" testfile
 *      del testfile
 */
try {
    // Enter the share name for your USB printer here
    $connector = new WindowsPrintConnector("Receipt Printer");
    /* Print a "Hello world" receipt" */
    $printer = new Escpos($connector);
    $printer -> text("Hello World!\n");
    $printer -> cut();

    /* Close printer */
    $printer -> close();
} catch(Exception $e) {
    echo "Couldn't print to this printer: " . $e -> getMessage() . "\n";
}
  1. You are quite correct- this driver is unrelated to php-printer and does not depend on it, though printer_open() with RAW mode would have been another possible way to get this working on the previous version.

Let me know how it goes!

@thryang

This comment has been minimized.

Copy link

thryang commented Apr 8, 2015

Thank you so much with such PROMPT response. I'll be right on it and test it out, and let you know soon! Really appreciate your effort!

@thryang

This comment has been minimized.

Copy link

thryang commented Apr 8, 2015

Good news is: It prints "Hello World!", and cuts the paper. What a wonderful work!

But at the same time, my browser displayed an error msg on scrn:
( ! ) Notice: Failed to print. Command 'print /D:"\i7-Main-PC\Text File Receipt Printer Share" "C:\Windows\Temp\escBF23.tmp"' returned 0: C:\Windows\Temp\escBF23.tmp is currently being printed in C:\Apache24\www\Examples\Print Driver escpos-php-master\src\WindowsPrintConnector.php on line 139

I'll continue to test the other printing features (barcode & other font styles), and keep you informed. Thanks!

@mike42

This comment has been minimized.

Copy link
Owner

mike42 commented Apr 8, 2015

Ok- thanks for the quick feedback.

I wont have a chance to test this properly for a few days, but if you fetch the latest version again, I think the incorrect error should go away.

It was basically a typo in the "looks like an error" check. 79f9a1c

@thryang

This comment has been minimized.

Copy link

thryang commented Apr 9, 2015

I used your demo.php (under folder: "example") to try out all the features specified in there. I modified the codes to add 1 line after line 11:
$connector = new WindowsPrintConnector("Text File Receipt Printer Share");
and changed line 12 to: $printer = new Escpos($connector);.

My printer did start printing but with all the ? (I'll send you the image (.jpg) by e-mail.)

Is that because the printer driver was set as "Text Only", so it can only print plan text?

@RushNetMx

This comment has been minimized.

Copy link

RushNetMx commented Apr 9, 2015

Thanks for this, is really helping, just one question, if I want open the cash box, which is connected to the printer, how can i do that under this scenario? Thanks, regards.
Other question, this Works if I have my App host in web or only locally?

@mikersee

This comment has been minimized.

Copy link

mikersee commented Apr 9, 2015

Hi RushNetMx, please try
$printer -> pulse();
This is the default pulse setting and should open an epson compatible cash drawer.

@mike42

This comment has been minimized.

Copy link
Owner

mike42 commented Apr 10, 2015

thryang, I can replicate this- I too get a stack of "?????" on the more complex output using the Windows print command. I've got a fix to the WindowsPrintConnector which I'll be uploading tomorrow.

In the meantime, you can side-step this issue by using net use LPT1 \\localhost\Printer Name on the Windows command-line, and then new FilePrintConnector("LPT1"); from PHP.

mike42 added a commit that referenced this issue Apr 11, 2015

@thryang

This comment has been minimized.

Copy link

thryang commented Apr 15, 2015

Michael - Thank you so much with your amazing job, and everything works from the test by using DEMO.PHP.

Just some minor issues:

  1. When I tested the Graphic portion, it printed the pic but displayed a message on scrn: "( ! ) Notice: Use of undefined constant imagecreatefrompng - assumed 'imagecreatefrompng' in C:\Apache24\www\Examples\Print Driver escpos-php-master\src\EscposImage.php on line 87".

2 "feedReverse" didn't work & "CUT_PARTIAL" did the same as CUT_FULL. These two shouldn't matter, because I may not even needed it.

@mike42

This comment has been minimized.

Copy link
Owner

mike42 commented Apr 15, 2015

Cool, glad the changes have worked for you.

  1. This was a bug caused by a silly coding error on my part, which has now been fixed, see #17
  2. I can confirm that the TM-T20 neither supports reverse feeding, nor different cut types, so hopefully you can print your layout without it (the types of barcodes supported are also shown in the demo to be exceedingly patchy, but by the lack of bug reports seem to indicate that CODE39 meets most people's needs).
@thryang

This comment has been minimized.

Copy link

thryang commented Apr 17, 2015

Thanks again one more time! Yes, the CODE39 is all I need.Really appreciate your work!

@RushNetMx

This comment has been minimized.

Copy link

RushNetMx commented Apr 17, 2015

I have one question, this Works if I have my App host in web or only locally? And how can I do it?, When I install I do it with the controller generic text, this affects if I want to print an image? Thanks.

@mike42

This comment has been minimized.

Copy link
Owner

mike42 commented Apr 20, 2015

Hi RushNetMx,

The Windows "Generic Text" driver simply allows us to pass through raw data to the device, so images will still work (because this driver generates and sends commands including the formatting and image data, which the driver passes on unmodified to our printer).

If I'm reading your question right, you're looking at doing a setup like this:

2015-04-print-diagram

Since the WindowsPrintConnector uses printer shares to work its magic, this is quite possible to do. On the computer with the physical printer attached, you need to share it. Then on your webserver, verify that you can print to it via the command-line, and finally hook up escpos-php to it using a smb:// URL.

I've written this up in code as an SMB print example. It has lots of comments about sending jobs to windows-shared printers from the command-line, which should help you out.

Note: Your webserver needs to be Windows or Linux, as the WindowsPrintConnector does not work on Mac at this stage.

@RushNetMx

This comment has been minimized.

Copy link

RushNetMx commented Apr 21, 2015

Hello, and thanks for your help, I'm sorry but I understand you. I have one web application in a web server, example http://www.myapp.com, so basically what i want to do is: the user make a request in that site and print a receipt in their printer, i search for days and I simply don't now how to doit. The only way i found is if my App is running locally on the user pc. Do you have any idea? Thanks again.

@mike42

This comment has been minimized.

Copy link
Owner

mike42 commented Apr 21, 2015

Hm, this is not something you can really do from a webserver on the public internet. You would need to either:

  • run escpos-php using a web stack on the actual terminals (and have them go back to a central server for database functions), or
  • run an internal server on the same network as your terminals, which can directly send jobs to each printer.

Taking the second option (just an idea), I'd have the app maintain a database table which maps internal IP addresses back to printer URL's. Minimal example of the idea:

<?php
require_once("escpos-php/Escpos.php");
/**
 * Map remote addresses to printers. You could use login names or preferences
 * from your app to produce a print connector.
 *
 * The web app must be able to contact the clients, which usually means they
 * should be on the same network.
 */
function getConnector($remote) {
    switch($remote) {
    case "192.168.122.126":
        return new WindowsPrintConnector("smb://terminal1/Printer");
    case "192.168.122.127":
        return new WindowsPrintConnector("smb://terminal2/Printer");
    default:
        throw new Exception("No known printer at your location");
    }
}

$error = "";
$input = isset($_GET['m']) ? $_GET['m'] : "";
if($input != "") {
    try {
        $connector = getConnector($_SERVER['REMOTE_ADDR']);
        $printer = new Escpos($connector);
        $printer -> text($input . "\n");
        $printer -> cut();
        $printer -> close();
    } catch(Exception $e) {
        $error = $e -> getMessage();
    }
}
?>
<html>
<head><title>Test printer</title></head>
<body>
    <form method="get">
        <div class="error"><?php echo htmlentities($error); ?></div>
        <input type="text" value="Test" name="m" />
        <input type="submit" value="Print">
    </form>
</body>
</html>

Which looks like this:

image

@Macwilcy

This comment has been minimized.

Copy link

Macwilcy commented Oct 23, 2015

mike42 Thanks so much for your help and time in making this available.

I have tried to implement your codes and I have not seem to find a headway. I would really appreciate your help.

I am using Epson TM-T20

I tried the "hello word" world test script above and it gave this error :
"Warning: copy(\MGC-LTD-PC\ESDPRT001): failed to open stream: No such file or directory in C:\xampp\htdocs\jamiykeboutique\lib\src\WindowsPrintConnector.php on line 339
Couldn't print to this printer: Failed to copy file to printer"

Please, how do I resolve this and make the printer to print. The code I used is below

testfile - print /D:"\%COMPUTERNAME%\Receipt Printer" testfile - ``` del testfile ``` _/ try { // Enter the share name for your USB printer here $connector = new WindowsPrintConnector("ESDPRT001"); /_ Print a "Hello world" receipt" */ $printer = new Escpos($connector); $printer -> text("Hello World!\n"); $printer -> cut(); /\* Close printer */ $printer -> close(); } catch(Exception $e) { echo "Couldn't print to this printer: " . $e -> getMessage() . "\n"; } ?>

It seems you have change your codes in regarding the WindowsPrintConnector(). The link to the one you gave above and the one in the master folder I downloaded are not looking alike

Please Note : I tried to share the Epson TM-T20 printer as you describe in your blog, but it kept on saying "Another printer shared already have the same name".

Please, how can I implement your code so It will work for me. Thank so much

@mike42

This comment has been minimized.

Copy link
Owner

mike42 commented Jun 13, 2016

Closing this issue off as the snippets are now out of date (the code is now namespaced, for example), and the original question was answered.

Other posters: If you have been able to print text to your printer from the command-line but can't get a PrintConnector working, then please file a new question on the bug tracker with details about your setup and somebody may be able to point you in the right direction.

@mike42 mike42 closed this Jun 13, 2016

@mylilocks

This comment was marked as off-topic.

Copy link

mylilocks commented Jul 6, 2016

I have a windows XP POS system connected to USBank A7 thermal printer via serial. I used WebClientPrint online demo and it works well wih that printer. Your code indicated serial is supported but I did not see the source code. Can you advise if you support the printer and via serial.

Also I have trouble running apache / PHP on this XP box so I thought I would run on Linux with AWS. I assume this will work as well as long as my XP browser can point to that AWS server. Appreciate your feedback.

@Christian-Lopez

This comment was marked as off-topic.

Copy link

Christian-Lopez commented Oct 3, 2016

Hi mike i have an application in a server and all the request come from a client and i need to print tickets for a sale and i want to know if with your driver it is possible, the principal question is about the printer if the driver recognizes the printer even the printer if in the client , i will hope for a answer

@mutisyajonathan

This comment was marked as off-topic.

Copy link

mutisyajonathan commented Jul 4, 2018

hello mike42. i connected epos thermal printer to a pc and am able to print receipts from the computer that has the server. if i connect the computer to another server, the printer is not able to print, it says file not found... what can i do?

@mike42

This comment has been minimized.

Copy link
Owner

mike42 commented Jul 4, 2018

One topic per issue please. This issue was closed over years ago, and is not relevant to escpos-php today. I have hidden some new questions that have been added here, and I'll lock the issue now so that it can rest in peace.

As always, please begin at the project README if you are getting started. Issues about being unable to print in a setup should be accompanied by the steps that you've used to verify that you can deliver raw data to your printer from the computer that is running PHP, which is usually done on the command-line.

Repository owner locked as resolved and limited conversation to collaborators Jul 4, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.