What is the best approach for printing (an existing pdf, in my case) to a LAN printer directly from php? So far I have been unsuccessful in getting anything to work, but I'm not sure what direction to further pursue. I am running Apache on Windows SBS 2008, PHP 5.3.9.
Approaches I know of so far:
It seems like this should be a simple task that has a widely accepted approach, but so far I'm not finding it. Thanks!
This is a tough nut to crack. I've had my own adventures in Windows printing from Ruby and came up with a few potential solutions that work by invoking an external command, which in PHP-land is system()
or exec()
(don't forget escapeshellcmd()
/escapeshellarg()
—they tend to make this stuff easier, especially on Windows). All of them assume Windows knows about the printer and it can be referenced by name.
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.
Finally, a general tip: You can register a network printer with a device name with the net use
command e.g.:
C:\> net use LPT2 \\Printer_Machine\Printer_Queue /persistent:yes
This should let you use LPT2
or LPT2:
in place of \\Printer_...
with most commands.
I hope that's helpful!
Not sure if this works for all printers but this gets the job done sending ZPL files to a Zebra label printer:
<?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);
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With