5

I've build a POS (Point of Sale) application in PHP that can print directly to a thermal printer. In most cases i run the application on a local webserver using WAMP.

Part of the printing code is:

$printer = "\\\\localhost\\TM-T88V";

// Open connection to the thermal printer
$fp = fopen($printer, "w");
if (!$fp){
  die('no connection');
}

$data = " PRINT THIS ";

// Cut Paper
$data .= "\x00\x1Bi\x00";

if (!fwrite($fp,$data)){
  die('writing failed');
}

This code works fine as long as the PC is connected to a network. I can get PHP to connect to a shared printer (either on the same pc, or on a pc in the network) by using fopen and "LOCALHOST" or "COMPUTER-NAME" : fopen("\\localhost\TM-T88V",'w');

If I disconnect the pc from the network, PHP can no longer connect to \\localhost or \\COMPUTER-NAME.

I've tried things like: fopen('TM-T88V'), fopen('\\.\TM-T88V'), but I keep getting "[function.fopen]: failed to open stream: No such file or directory...".

How do I connect to a local (shared) printer (preferably by name) without having an active network connection?

Bauke Boorsma
  • 51
  • 1
  • 4
  • 1
    Using a loopback-adapter I've found a way around the problem, this way the computer still 'thinks' it has a network connection so that it can resolve \\localhost. But i think there should be a way to connect directly to the printer. – Bauke Boorsma Apr 23 '12 at 16:35
  • Hi Bauke. Can you please explain the solution to this problem in a little more detail. We are in the same situation as were you an year back. We are not able to send a print to Zebra Thermal Printer. We tried your code as well but it did not work. – Nitin Srivastava Jul 09 '13 at 19:35
  • 1
    Hi Bauke. Thanks a ton for the idea. We have been able to solve this finally at our end as well. The only thing we had to do was to share the printer over network and then access it via network. Loopback adapter did not work for us. – Nitin Srivastava Jul 09 '13 at 20:30
  • @Nitin, sorry for the late reply. Glad to see you solved it. Making the printer a shared printer will enable you to use the php-code to print data. In case you need to access a printer on the same computer, and that computer does not have an active network connection, in that case you will need a loopback adapter (I have not found an way around that). You will also still need to make the printer shared in that situation. – Bauke Boorsma Aug 30 '13 at 12:50

2 Answers2

3

Have you tried fopen("PRN", "w")?

alganet
  • 2,527
  • 13
  • 24
  • Yes, I have. It also gives me an "failed to open stream..." error. I've set the thermal printer to be the default printer, but that does not help either. – Bauke Boorsma Apr 23 '12 at 16:29
0

Here is the code snippet I am using for my print job in PHP :

$handle = printer_open('Printer Name in windows here');

if($handle) { // Make sure the printer is present before sending the job
// print job here
}
pollux1er
  • 5,372
  • 5
  • 37
  • 36