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

2021-12-28 00:00:00 windows printing php

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

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

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

Well, turns out I don't have php_printer.dll extension installed and I was told not to re-compile php to add it.

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

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.

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

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?

推荐答案

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

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(), "
";
}

相关文章