PHP print to printer (local / network) directly (stuck in print spooler)

21,456

I got the answer, the problem is you have to add this in the beginning

printer_start_doc($ph, "Start Doc");

and this before printer_close

printer_end_doc($ph);

Cannot print without start and end doc.

I am just wondering why I still can print to printer number 1 at other computer, but does not use that start and end doc PHP command.

Share:
21,456
Jef
Author by

Jef

Programmer java and PHP...

Updated on July 09, 2022

Comments

  • Jef
    Jef almost 2 years

    Ok, I am using PHP 5.4 and I want to print directly from web server to printer. I am using PHP module php_printer.dll.

    The function is work well. And I see in printer queue, the data is in, but hang in status "sent to printer"

    Here is the code

            $printerList = printer_list(PRINTER_ENUM_LOCAL);
            var_dump($printerList);
            $printerName = $printerList[0]['NAME'];
            echo $printerName;
            $printer = $printerName;
            if($ph = printer_open($printer)) {
               $content = "hello";
    
               // Set print mode to RAW and send PDF to printer
               printer_set_option($ph, PRINTER_MODE, "RAW");
               printer_write($ph, $content);
               printer_close($ph);
            }
            else "Couldn't connect...";
    

    There is no error in screen or logs file in Apache, so i assume the PHP code run well. And I see the data is in Windows print spooler. The problem is it stuck in there, and it cannot produce the output from printer.

    I assume the problem is because the windows permission problem (I am using windows 8, test in windows 7 or XP cannot work too). I have 4 test printer: 1. HP Laser Jet 401, 2. PDF Printer, 3. Windows Fax, 4. XPS document writer.

    My computer cannot print to all the 4 printers. I test in other computer, it can print to printer number 1, but the other cannot.

    The status is only "Sent to printer". If it is printing, the status is "spooling"

    I attach the screenshot the queue data in spool printer. Sent to printer status

    And this is the screenshot that success spooler (I test with print normal, ctrl+p and print) Spooling

    Any help will be appreciated. Thanks!