PHP printing to local Thermal Printer, does not wo

2019-03-29 15:43发布

问题:

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?

回答1:

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



回答2:

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
}