Printing data to printer using PHP

删除回忆录丶 提交于 2019-12-18 12:02:55

问题


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?


回答1:


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";
}



回答2:


You have several options to print with PHP on Windows:

1. Extension "php_printer"

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/

2. Execute the windows CLI command "print" via PHP

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

3. Use Sockets

$fp=pfsockopen("192.168.1.201",9100);
fputs($fp,$print_data);
fclose($fp);

4. Output to HTML, open in browser and trigger window.print()



来源:https://stackoverflow.com/questions/22313439/printing-data-to-printer-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!