Reputation: 415
I have a question that has been troubling me for at least 3 weeks now. I need to print some data to a printer using php. I have data saved into a $print_output
variable, and I know my data is good because when I send it via email, it shows everything that is supposed to be shown.
Well, I tried writing this code, where I thought I could test it but wasn't sure if it would have even worked.
$handle = printer_open("\\\\192.168.1.33_4\\Printer_Office");
printer_set_option($handle, PRINTER_MODE, "raw");
printer_write($handle,$print_output);
printer_close($handle);
Well, turns out I don't have php_printer.dll extension installed and I was told not to re-compile php to add it.
What I'd like to do is simply print out the data that is stored in $print_output
to a printer on my same network. I don't want to use the javascript function window.print()
because I can't have a print dialogue screen pop up.
Does anyone have any information that can point me in the right direction? Or another way to simply print a small amount of data directly to the printer without using php's printer_open
function?
Upvotes: 12
Views: 59062
Reputation: 164
I had same problem. I found that I can 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();
Upvotes: 1
Reputation: 41756
You have several options to print with PHP on Windows:
Because PHP 5.2.0 is outdated, it's quite hard to find compiled extension. You might try to drop in the php_printer extension for 5.2.8:
http://downloads.php.net/pierre/php_printer-cvs-20081215-5.2.8-nts-Win32.zip
Add to your php.ini:
extension=php_printer.dll
Recent version might be found at Pierre's download site: http://downloads.php.net/pierre/
An alternative solution is to use the windows command "print".
See http://technet.microsoft.com/en-us/library/cc772773%28v=ws.10%29.aspx
exec("print /d:\\192.168.1.33_4\\Printer_Office c:\accounting\report.txt);
$fp=pfsockopen("192.168.1.201",9100);
fputs($fp,$print_data);
fclose($fp);
Upvotes: 6
Reputation: 415
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";
}
Upvotes: 11