php - 如何使用 PHP 打印到 windows 网络打印机?
问题描述
我有一台带有 Windows 打印机驱动程序的标签打印机。我直接打印到端口 9100 上的打印机,但某些标签无法打印。制造商表示首选方法是打印到 Windows 驱动程序。如何使用 PHP 打印到 Windows 打印机?
运行此代码
$handle = printer_open('\\\\localhost\\Brother HL-L2370DW series');
printer_set_option($handle, PRINTER_MODE, "RAW");
printer_write($handle, "TEXT To print");
printer_close($handle);
得到错误:
Uncaught Error: Call to undefined function printer_open() in C:\xampp\htdocs\srichter\tester\print-network-printer.php:4
我在 Windows 桌面 PC 上的 XAMPP 上安装了最新的 PHP。PHP 不再支持printer_open 了吗?
如何打印到网络连接的打印机?
一种答案。使用 shell_exec 并运行 Out-Printer powershell cmdlet:
shell_exec( 'powershell "Get-Content -Path /users/srich/steve.txt | Out-Printer -Name \"Brother HL-L2370DW series\""')
解决方案
使用shell_exec
对我有用。
shell_exec 运行DOS
COPY
命令将文件复制到打印机:
$errmsg = shell_exec( 'COPY "C:\\Users\\srich\\Dropbox\\notes\\Walker\\' .
'Daily notes\\2021\\2021-07-12 GTIN label.zpl" ' .
'"\\\\HPLAPTOP\\ZDesigner ZD620-203dpi ZPL" 2>&1' ) ;
echo '<hr>' ;
echo $errmsg ;
shell_exec 运行powershell
Out-Printer
cmdlet:
$errmsg = shell_exec( 'powershell "Get-Content -Path /c/users/srich/steve.txt ' .
' | Out-Printer -Name \"Brother HL-L2370DW series\"" 2>&1 ');
echo '<hr>' ;
echo $errmsg ;