Print Directly to Network Printer Using PHP

print directly from iis using php aplication to printer through server not browser to a shared printer

after 4 weeks I find a solution.
this is a way to use a shared printer.
use mike42 escpos github plugin.

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();

printing over network from PHP app

You could use the LPR Printer class from here:

http://www.phpclasses.org/package/2540-PHP-Abstraction-for-printing-documents.html

Example:

<?php 
include("PrintSend.php");
include("PrintSendLPR.php");

$lpr = new PrintSendLPR();
$lpr->setHost("10.0.0.17"); //Put your printer IP here
$lpr->setData("C:\\wampp2\\htdocs\\print\\test.txt"); //Path to file, OR string to print.

$lpr->printJob("someQueue"); //If your printer has a built-in printserver, it might just accept anything as a queue name.
?>

Printing data to printer using PHP

For anyone who is having the same trouble, I figured out I can simply push the data using socket programming as follows. The ip address below is my printer's ip address. You can telnet into your printer to make sure the connection works beforehand if you'd like.

if(isset($_POST['order'])){
$print_output= $_POST['order'];
}
try
{
$fp=pfsockopen("192.168.1.33", 9100);
fputs($fp, $print_output);
fclose($fp);

echo 'Successfully Printed';
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}


Related Topics



Leave a reply



Submit