php 使用php直接打印到网络打印机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4956143/
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 16:21:40  来源:igfitidea点击:

Print directly to network printer using php

phpwindowsprintingnetwork-printers

提问by Ullas Prabhakar

I am unable to print a page to a network printrer using php.
But this works if it is a local printer. I have installed php_printer.dll and enabled in php.ini The following is the code:

我无法使用 php 将页面打印到网络打印机。
但是如果它是本地打印机,这会起作用。我已经安装了 php_printer.dll 并在 php.ini 中启用了以下代码:

//$handle  =  printer_open("Send To OneNote 2007"); ///This Works

$handle = printer_open('\\192.168.0.8\Canon MF4320-4350'); 
printer_set_option($handle, PRINTER_MODE, "RAW");
printer_write($handle, "TEXT To print");
printer_close($handle);

It shows the error

它显示错误

Warning: printer_write() [function.printer-write]: couldn't allocate the printerjob [5] in E:\Server\xampp\htdocs\Kiosk\Admin\print.php on line 16

警告:printer_write() [function.printer-write]:无法在第 16 行的 E:\Server\xampp\htdocs\Kiosk\Admin\print.php 中分配打印机作业 [5]

回答by Richard Tan

If you used the command line PHP (CLI) the printing to network printers would work. Your $addr is correct by the way.

如果您使用命令行 PHP (CLI),则可以使用网络打印机进行打印。顺便说一下,您的 $addr 是正确的。

The issue lies with PHP when you combine it with Apache. In windows, your php scripts would run under the user SYSTEM. Because of security concerns, all network resources are not visible to SYSTEM.

当您将 PHP 与 Apache 结合使用时,问题就出在 PHP 上。在 Windows 中,您的 php 脚本将在用户 SYSTEM 下运行。出于安全考虑,SYSTEM 无法看到所有网络资源。

To resolve this problem, create a new user with admin privileges (or at least with Network Resource visibility). In Windows, if you run Apache as a service, click on the SERVICE button in the Apache Service Monitor. Under Apache 2.2, right click on properties. Under the LOGIN tab, change the user from SYSTEM to your newly created user account. Restart Apache. You should be able to run your PHP script to print to network printers now.

要解决此问题,请创建一个具有管理员权限(或至少具有网络资源可见性)的新用户。在 Windows 中,如果您将 Apache 作为服务运行,请单击 Apache Service Monitor 中的 SERVICE 按钮。在 Apache 2.2 下,右键单击属性。在 LOGIN 选项卡下,将用户从 SYSTEM 更改为您新创建的用户帐户。重新启动阿帕奇。您现在应该能够运行您的 PHP 脚本来打印到网络打印机。

回答by Blair McMillan

Try using either "s with 4\ or 's with 3. eg:

尝试使用 "s with 4\ 或 's with 3. 例如:

$handle = printer_open("\\192.168.0.8\Canon MF4320-4350");
// or
$handle = printer_open('\2.168.0.8\Canon MF4320-4350');

Also, try using a domain name rather than IP (eg. computer-nameor full.address.example.com).

此外,尝试使用域名而不是 IP(例如computer-namefull.address.example.com)。

回答by Spudley

Just looking at that string, it feels very much as if it's likely to be caused by back-slash escaping going wrong.

光看那个字符串,感觉很可能是反斜杠转义错误造成的。

Debugging tip: Set your network address in a variable rather than directly in printer_open(). Then use print()or similar to display the value.

调试提示:在变量中而不是直接在printer_open(). 然后使用print()或 类似的来显示值。

<?php
   $addr = '\\192.168.0.8\Canon MF4320-4350';
   print $addr;
   printer_open($addr);
   ...
?>

This will allow you to see the value of the string that you're using, which might help you see what's going wrong, and more importantly, help you see how to fix it.

这将允许您查看您正在使用的字符串的值,这可能有助于您了解出了什么问题,更重要的是,可以帮助您了解如何修复它。

Hope that helps.

希望有帮助。