如何从php直接打印(我的情况是现有pdf),将其打印到局域网打印机?目前我尝试了一些方法,但都没有成功,不知道应该继续探索哪个方向。我正在运行Windows SBS 2008上的Apache和PHP 5.3.9。
目前我所知的方法:
- shell_exec()
- phpprintipp - 如果能让它工作,这似乎是最好的方法
- php_printer.dll - 目前没有存在的dll
看起来这应该是一个简单的任务,已经有广泛接受的方法,但到目前为止我还没有找到。谢谢!
这是一个棘手的问题。我曾经在使用Ruby从Windows打印时遇到过一些麻烦,但我找到了几个可以使用的潜在解决方案,它们通过调用外部命令来实现,在PHP中,这个命令是system()
或exec()
(不要忘记escapeshellcmd()
/escapeshellarg()
——它们往往会使这些事情更加容易,特别是在Windows上)。所有这些解决方案都假定Windows知道打印机并且可以通过名称进行引用。
You can literally just redirect the file to the networked printer, e.g.:
copy /b \path\to\filename.pdf > \\Printer_Machine\Printer_Queue
The /b
switch specifies a binary file, but I'm 80% sure it's not strictly now,
in 2012.
You can try the print
command:
print /d:\\Printer_Machine\Printer_Queue \path\to\filename.pdf
\d
stands for "device." I haven't actually tried this one and I'm not sure if it
works with PDF or only, owing to its DOS origins, text files.
Install Adobe Reader and use its command line facilities:
AcroRd32.exe /t \path\to\filename.pdf "Printer Name" "Driver Name" "Port Name"
I'm not sure if your server environment can accommodate Reader but this is the
solution I've been most successful with. You can find
documentation here
(PDF, pg. 24). Printer Name
and Driver Name
should match exactly what you see in the
printer's properties in Control Panel. Port_Name
can usually be omitted, I think.
Print using Ghostscript. I've never tried this on Windows but the documentation is here and there's more info here. The command goes something like this:
gswin32.exe -sDEVICE=mswinpr2 -sOutputFile="%printer%Printer Name" \path\to\filename.pdf
mswinpr2
refers to Windows' own print drivers (see the second link above),
"%printer%
" is literal and required and "Printer Name
" should, again, match the
printer's name from Control Panel exactly. Ghostscript has many, many options and
you'll likely have to spend some time configuring them.
net use
命令注册网络打印机,并为其设备命名,例如:C:\> net use LPT2 \\Printer_Machine\Printer_Queue /persistent:yes
LPT2
或LPT2:
代替\\Printer_...
。希望这对你有所帮助!exec()
运行AcroRd32.exe时,输出是什么? - Jordan Runningcontrol+z
(EOF)到打印机了吗? - Bradexec()
打开阅读器,看看是否能够解决问题。 - Jordan Running不确定是否适用于所有打印机,但这可以通过发送ZPL文件到Zebra标签打印机来完成工作:
<?php
if(($conn = fsockopen('192.168.10.112',9100,$errno,$errstr))===false){
echo 'Connection Failed' . $errno . $errstr;
}
$data = <<<HERE
^XA
^FT50,200
^A0N,200,200^FDTEST^FS
^FT50,500
^A0N,200,200^FDZebra Printer^FS
^XZ
HERE;
#send request
$fput = fputs($conn, $data, strlen($data));
#close the connection
fclose($conn);
?>
php_printer.dll
。它有什么问题吗?为什么它对你不起作用?不要通过直接与IP打印机通信来重新发明轮子。 - Brad