使用 PHP 将数据打印到打印机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22313439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:49:39  来源:igfitidea点击:

Printing data to printer using PHP

phpwindowsprinting

提问by user3150191

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_outputvariable, and I know my data is good because when I send it via email, it shows everything that is supposed to be shown.

我有一个问题困扰了我至少 3 周。我需要使用 php 将一些数据打印到打印机。我将数据保存到一个$print_output变量中,我知道我的数据很好,因为当我通过电子邮件发送它时,它显示了应该显示的所有内容。

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.

好吧,结果我没有安装 php_printer.dll 扩展,我被告知不要重新编译 php 来添加它。

What I'd like to do is simply print out the data that is stored in $print_outputto 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.

我想要做的只是将存储在$print_output同一网络上的打印机中的数据打印出来。我不想使用 javascript 功能,window.print()因为我无法弹出打印对话屏幕。

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_openfunction?

有没有人有任何信息可以为我指明正确的方向?或者另一种不使用php的printer_open功能而直接将少量数据直接打印到打印机的方法?

回答by user3150191

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.

对于遇到同样问题的任何人,我发现我可以简单地使用套接字编程推送数据,如下所示。下面的ip地址是我打印机的ip地址。如果您愿意,您可以 telnet 到您的打印机,以确保连接能正常工作。

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

回答by Jens A. Koch

You have several options to print with PHP on Windows:

在 Windows 上使用 PHP 打印有多种选择:

1. Extension "php_printer"

1.扩展“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:

由于 PHP 5.2.0 已过时,因此很难找到已编译的扩展。您可能会尝试删除 5.2.8 的 php_printer 扩展:

http://downloads.php.net/pierre/php_printer-cvs-20081215-5.2.8-nts-Win32.zip

http://downloads.php.net/pierre/php_printer-cvs-20081215-5.2.8-nts-Win32.zip

Add to your php.ini:

添加到您的 php.ini:

extension=php_printer.dll

Recent version might be found at Pierre's download site: http://downloads.php.net/pierre/

最新版本可以在 Pierre 的下载站点找到:http: //downloads.php.net/pierre/

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

2.通过PHP执行windows CLI命令“print”

An alternative solution is to use the windows command "print".

另一种解决方案是使用 windows 命令“打印”。

See http://technet.microsoft.com/en-us/library/cc772773%28v=ws.10%29.aspx

请参阅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

3. 使用套接字

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

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

4.输出为HTML,在浏览器中打开并触发window.print()