mike42 / escpos-php

PHP library for printing to ESC/POS-compatible thermal and impact printers
Other
2.57k stars 862 forks source link

Permission denied. Failed to write file to printer at COM4 #696

Open TomaIvanovTomov opened 5 years ago

TomaIvanovTomov commented 5 years ago

Well hi there! I am pretty new to this POS things and want to ask you for a little advice. My case is like so - I am trying to connec to Eltrade POS device ( wich is not listed as working one but I hope it will be soon :) ) via USB. I am not familiar with the hardware but I think it is connected on COM4 port so I am searching it there like so:

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

I get this information from Windows device manager from section Porst ( COM and LPT ) and this is how it looks like: ports The error I get is :

Warning: file_put_contents(COM4): failed to open stream: Permission denied in C:\xampp\htdocs\test\vendor\mike42\escpos-php\src\Mike42\Escpos\PrintConnectors\WindowsPrintConnector.php on line 384
Couldn't print to this printer: Failed to write file to printer at COM4

Where and how I should give permissions ? Is it about device permissions or some port permission ( if it has things like that ) ? I am working on Windows 10. Using XAMPP for testing purpose. Thank you very much!

velislavbg commented 5 years ago

Are you running this code via apache or via cli?

try to run the code from command prompt logged as an administrator - if the problem stems from insufficient privileges, you will succeed and need to elevate xampp rights. On the other hand if test fails again , it might be a printer issue.

BTW Question from the screen above : you have a printer on LPT1 as well. Do you print on LPT1 without any problem using the same code?

mike42 commented 5 years ago

I've had the most luck with network printing on Windows. You can share the printer to the LAN and then connecting to it over localhost (see this blog post, command-line examples here).

You can then restrict access to the printer based on user accounts (connect with smb://user:password@host/printer), or by using the machine's firewall to limit the print sharing service to localhost.

In any case, best find something that works the command-line before you start programming in PHP, since we just wrap calls to write to files and net use in the background.

As some other options:

mike42 commented 5 years ago

Also, be wary of the suggestion to elevate XAMPP.

That should not be necessary, and runs against security best practices.

TomaIvanovTomov commented 5 years ago

And how can I send instruction? I mean to row out a little bit of empty paper, I have to send instruction "2C" wich is hexdecimal instruction. Is it possible here and if so - with wich method, because I couldn't find it? Thank you mike and velislav!

mike42 commented 5 years ago

@TomaIvanovTomov You would use $printer -> feed() for that.

velislavbg commented 5 years ago

$printer -> getPrintConnector -> write(chr(1) + chr(36) + chr(60) + chr(44) + chr(5) + chr(48) + chr(48) + chr(57) + chr(49) + chr(3));

if the printer is a fiscal device.

@TomaIvanovTomov ? the model of the pos printer is ......

TomaIvanovTomov commented 5 years ago

It is ELTRADE A3. The printer is not at my place at this moment .. I am really curiouse about making it works and will try your example as soon as I get it again! Will give feedback for sure.