PHP Direct Print In Client System

3.5k Views Asked by At

I want to print receipts directly (of course silently) to respective dot matrix printers attached to the client systems in a local wamp hosted application. Although I was successful in this by using jsprintsetup in mozilla. But the printer speed became a challenge always.

After some days I was able to send print jobs directly from php application without any delay and the printer responds superb from the localhost server and from the client systems as well. But I tested this application in a windows domain network and in the real case there wont be any domain network and the application requires this to be done in any LAN with or without domain. I am not sure but no domain network might be the problem.

And the real screwed thing is;

The localhost server prints the receipts. No Issues! From the client We can send the print job to the printer attached to the localhost server. Superfine!

But not getting printer connection to the printer attached to the client system neither from client nor server.

The Php Warning: ("Warning: fopen(\192.168.1.13\TVS MSP 250 Star): failed to open stream: Permission denied in D:\wamp")

Note: I ran all the way to get this done including installing php_printer.dll, shared the wamp and www folder with full permissions, used "\\systemname\printername" and a lot other stuff to check whether that will do.. But no help..

Please help me..

Here is my code;

$bold1 = Chr(27) . Chr(69); // semi em. bold
$bold0 = Chr(27) . Chr(70); // "" 
$initialized = chr(27).chr(79);
$initialized = chr(27).chr(64); // initialize printer
$initialized .= chr(27).chr(51).chr(40); //Select 6 lines per inch Line Spacing
$initialized .= chr(10).chr(12).chr(15).chr(50).chr(20); // Draft, Font, Pitch
$initialized .= chr(27).chr(79); // Cancel Skip Perforation
$initialized .= chr(27).chr(106).chr(108); // Reverse Line Feed
$condensed0 = chr(18);

$initialized .= chr(27).chr(97).chr(0); //justify left
$initialized .= chr(27).chr(37).chr(0); // Selects normal character set

$initialized .= chr(27).chr(67).chr(15); // Page Length
$initialized .= chr(27).chr(67).chr(5);
$initialized .= chr(13);
$data  = $initialized;
$data .= chr(27).chr(79); // Cancel Skip Perforation
$data .= chr(27).chr(120).chr(48); // Select Draft Mode
$data .= chr(27).chr(97).chr(0); // Justify Center
//$data .= chr(14); // Double Width For One Line
//$condensed1 = chr(15);                                    

$data .= $bold1."           HEADER      ".$bold0."\n";
$data .= chr(10);
$data .= "           "SUB HEADER"   "."\n";
//$data .= chr(10);
$data .= "             "."BASE LINE".""."\n";
//$data .= chr(10);
$data .= "   "."\n";
$data .= "Date: ".$time."       "."\n";

$data .= "|----------------------------------------------------------|\n";

$data .= "Thank You! Print Testing Successfull"."\n";

$data .= "|----------------------------------------------------------|\n";
$data .= chr(27).chr(102).chr(49);
$data .= " \n";
$data .= " \n";
$data .= " \n";
$data .= " \n";
$data .= " \n";
$data .= " \n";
$data .= " \n";
$data .= " \n";
$data .= " \n";
$data .= " \n";
$data .= " \n";
$data .= " \n";

$fop = fopen($printer, "RAW");

if (!$fop){
    echo 'Selected Printer: '.$printer;
    print "<br/>";
    print "<img src=\"$image2\" width=\"16px\" height=\"15px\"\ align=\"absmiddle\"/>";
    print " ";
    echo  'Sorry! Could Not Connect To The Selected Printer';
    print "<br/>";
}
else
{
    print "<br/>";
    print "<img src=\"$image\" width=\"16px\" height=\"15px\"\ align=\"absmiddle\"/>";
    print " ";
    echo  'Printer Connected Successfully'; 
}
$data .= "\x00\x1Bi\x00";

if (!fwrite($fop,$data)){
    print "<img src=\"$image2\" width=\"16px\" height=\"15px\"\ align=\"absmiddle\"/>";
    print " ";
    echo 'Printing Failed!';
    print "<br/>";
}  
else
{
    print "<br/>";
    print "<img src=\"$image\" width=\"16px\" height=\"15px\"\ align=\"absmiddle\"/>";
    print " ";
    echo 'Printing Succeeded!';
    print "<br/>";
}
2

There are 2 best solutions below

0
On

you can use mike42 escpos github plugin.

after download you need to make a connector for your printer. use this

my sample for a shared printer and for a php file that is running by schedule task is:

require 'vendor/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\CapabilityProfile;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;


$connector = new WindowsPrintConnector("smb://computername/printername");
$printer = new Printer($connector);

$printer -> text("hello world");
$printer -> text("\n");
$printer -> text("\n");
$printer -> text("hello again");
$printer -> cut();
$printer -> close();

this is fast enough.

0
On

Maybe you already solve the problem, but this may help other with similar problem.

If you want the client to print in the client printer, then why don't you install webserver and php at the client computer, and let the client computer run the php code that print locally.

You pass the data from the server website to the local with POST method or similar.