Print to network printer from PHP

10,974

Solution 1

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.

  1. 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.

  2. 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.

  3. 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.

  4. 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!

Solution 2

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);
?>
Share:
10,974
Tim
Author by

Tim

Updated on July 20, 2022

Comments

  • Tim
    Tim almost 2 years

    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:

    • shell_exec()
    • phpprintipp - this seems like the best approach to me if I could get it to work
    • php_printer.dll - no current dll exists

    It seems like this should be a simple task that has a widely accepted approach, but so far I'm not finding it. Thanks!

  • Tim
    Tim about 12 years
    Thanks for the answer. Option 1 (copy) did cause the printer to print, but it was only a sheet that says "1 file(s) copied." and did not print my pdf. With option 2 (print) I always get an "Unable to initialize device 'printer'". With option 3 (Adobe Reader) I was able to successfully print from the command prompt on the server, but get nothing when running from PHP. I echoed the line I am passing to exec, and it matches exactly what I input manually, but nothing happens. Any thoughts on any of that?
  • Jordan Running
    Jordan Running about 12 years
    I'm guessing it's a path issue, either to the PDF file or to the executable; what was the output from AcroRd32.exe when you ran it with exec()?
  • Brad
    Brad about 12 years
    Tim, did you send a control+z (EOF) to your printer after copy?
  • Tim
    Tim about 12 years
    @Jordan: I don't get any result. If I supply an output variable, it returns an empty array ( array(0) { } ).
  • Jordan Running
    Jordan Running about 12 years
    Hmm. Is there any output when you run it from the command line? You could try some of the other options in the Reader docs to see if you can e.g. open Reader with exec().
  • Tim
    Tim about 12 years
    @Jordan: This is what I'm passing to exec() (as echoed when the script is run): "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe" /t "\\CCASERVER\Data\CCA Proposals\test.pdf" "bizhubC252" "KONICA MINOLTAC252/C252P VXL" When I use that exact same thing in the command prompt, it works. Should it look differently from php? I don't get any output from the command prompt, but nor can I just open Reader through php.