Hey all,

I am trying to write a script that will fire a bunch of forms to a printer when a user clicks submit on a form. Right now I am having problems getting a simple test page working. Here is my script:

<?php
$printer = "\\SERVERNAME\PRINTERNAME"; //This info has been changed for privacy reasons
$handle = printer_open($printer);
printer_write($handle, "This is a test print");
printer_close($handle);
echo $printer;
?>

I get the following error:

"Warning: printer_write(): supplied argument is not a valid Printer Handle resource in c:\inetpub\wwwroot\dev\print.php on line 4

Warning: printer_close(): supplied argument is not a valid Printer Handle resource in c:\inetpub\wwwroot\dev\print.php on line 5"

Any ideas? I tried to search the forum and I could not find my answer.

    As stated on the index page for all the printer functions, these are supported under Win32 only

    Are you using a windows machine?

    Also, use some basic debugging:

    <?php
    $printer = '\\\SERVERNAME\PRINTERNAME';
    $handle = printer_open($printer);
    if(!$handle || $handle == NULL)
    {
      die('Error connecting to Printer');
    }
    else
    {
      // Now write to the printer and print
    }
    ?>

    you're not connecting to the printer for whatever reason, and a resource isn't returned...

    ~Brett

      My server is a windows machine, and so is my workstation. I still get an error now it is just "Error connecting to Printer." I am able to connect to the printer through windows and print with no problems. Is there something I need to do in php.ini to enable network printing? I installed the PECL for the printing and added it to the PHP.ini file.

      Aybabtu.

        Just to check...

        Windows users must enable php_printer.dll inside of php.ini in order to use these functions.

        The printer in question is one that is connected to the server, not the client.

        Just some things to check up on....

        ~Brett

          The php_printer has been installed and enabled in php.ini. Here is the kinda tricky part that I possible do not understand. The printer is mapped to my server, is that the same as connected to the server? I was able to get the to work by mapping a network printer as a local printer but, to add to the confusion, the printer I want to send to is a OPTIO virtual printer. I want to be able to create a form on our intranet and have it printed, emailed, and faxed to certain devices. Optio will handle the faxing, but it needs to have data spooled to the virtual print queue to create the fax.

            Mapping i believe is not the same as connected. Not sure, you've expended all of my knowledge on this subject.... sorry. Weedpacket may know about this...

            ~Brett

              Thanks for the help though, I do appreciate it when someone at least makes an effort. Should I PM Weedpacket? Also, I got to thinking that I could just install a local virtual printer on the server to re-direct the print stream to the other printer. I will let you know how that works out.

              Aubabtu.

                I have an answer to my problem if anyone care to hear it. This may not be the most efficent way but what I did was install RedMon port monitor on my server and was able to send the jobs to this local virtual printer and then it relays the job to the virtual printer on the network.


                Now another question:
                What happens if you do not use the printer_close() command?

                  8 months later

                  Hi Aybabtu,

                  I had some troubles about php_printer. When a try to execute my code, i have the bellow error:

                  Warning: printer_write(): supplied argument is not a valid Printer Handle resource in c:\Inetpub\wwwroot\formularios\adm\teste.php on line 18

                  Warning: printer_close(): supplied argument is not a valid Printer Handle resource in c:\Inetpub\wwwroot\formularios\adm\teste.php on line 21.

                  My code is:

                                  $handle = printer_open("\\\\xxx\\HP"); // "xxx" is the name of machine where the printer is connected and "HP" is the name of the printer
                  
                      printer_write($handle, "isto e um teste");
                  
                                  printer_close($handle);

                  I think that the code connect to the printer because if I put a invalid name to the print, I receive the below message:

                  Warning: printer_open() [function.printer-open]: couldn't connect to the printer [\xxx\HP55] in c:\Inetpub\wwwroot\formularios\adm\teste.php on line 11

                  Warning: printer_write(): supplied argument is not a valid Printer Handle resource in c:\Inetpub\wwwroot\formularios\adm\teste.php on line 18

                  Warning: printer_close(): supplied argument is not a valid Printer Handle resource in c:\Inetpub\wwwroot\formularios\adm\teste.php on line 21.

                  Could you help me?

                    2 months later
                    Aybabtu wrote:

                    I have an answer to my problem if anyone care to hear it. This may not be the most efficent way but what I did was install RedMon port monitor on my server and was able to send the jobs to this local virtual printer and then it relays the job to the virtual printer on the network.

                    ...

                    Hi Aybabtu, thank for your tip... I tried it, but I can't get rid of making it work...

                    I have my php-mysql applcation on a windows XP apache-php-mysql server where Apache is 2.0.55 and php is 5.1.2.

                    Then I have one pc (client) where I want to have a SATO barcode printer (connected with serial cable) "receiving" prints from the server.
                    The SATO printer is shared on the client as CX400_1

                    What happen:

                    If I connect the printer directly to the Server, barcode prints are working fine.
                    If I try your solution ( to have the printer connected to client. This is a must, in need the barcode printer in the inventory, not in the server room two floors above... ;_-) ), prints are queued in the virtual printer ( I called it CX400_V ) but not released to the CX400_1.

                    How do you have configured the "virtual printer" ? I suppose, same drivers as the client networked one. Some special or advanced settings? SATO printer is more or less as Zebra, you send codes and escapes codes.
                    And next?
                    Which setting for the RedMon RPT1: port? There are 5 settings
                    - Program handles output
                    - Prompt for file name
                    - Copy stdout to printer
                    - Copy temporary file to printer
                    - Copy pipe to printer

                    I tried all the three Copy settings... but result is always the same. Prints doesn't move from CX400_V to CX400_1

                    Thank you for any answer and also to any welcome help.

                    Robert

                    .

                      9 days later

                      Couple things to check

                      1.Are you sure the printer is being shared?
                      2. If so, is INET_USR added to the users of the printer?
                      3. Make sure the printer can print from everyone including anonymous users

                        twilliamsen wrote:

                        Couple things to check

                        1.Are you sure the printer is being shared?
                        2. If so, is INET_USR added to the users of the printer?
                        3. Make sure the printer can print from everyone including anonymous users

                        I have windows xp home on the client pc.

                        So I have no Protections to be set on the printer... maybe is this the issue?

                        Thank you twilliamsen for your interest.

                        I have this additional info:
                        - I wrote a test script. It is PRINT4.PHP
                        - I've copied it into c:\tmp folder
                        - I've opened a cdm (terminal) and I've run this command c:\programs\php\php.exe c:\tmp\print4.php
                        - well... the printer prints correctly

                        In the print4.php script, the printer handle points to the shared printer

                        Now I knew (reading aroud) about the php issue/bug that prints on shared printers don't work from a web version of a script.

                        One MAN ( Richard Theobald ) kindly tip me this

                        Richard wrote:

                        You will see a directory called "CLI" under your PHP installation directory. CLI stands for "Command Line Interface". In this directory is another PHP.EXE program. You can use this program to run a php script from the command prompt (eg: "php testscript.php"). Using this version of PHP I got printing to work to a shared printer on another computer. I don't know why this one worked and the web version didn't.

                        and in fact that works fine...

                        Now... why it doesn't work with shell_exec !??!!?

                        I tried with shell_exec I tried with redmon too... nothing, you always get this:

                        PHP Warning: printer_write(): couldn't allocate the printerjob [5] in c:\tmp\stampa4.php on line 24
                        PHP Warning: printer_write(): couldn't allocate the printerjob [5] in c:\tmp\stampa4.php on line 37
                        PHP Warning: printer_write(): couldn't allocate the printerjob [5] in c:\tmp\stampa4.php on line 38
                        PHP Warning:.............

                        This makes me crazy... 😕 😕 :mad: :mad:

                        I'll try with XP Pro, 'cause I suspected something about users permissions... In fact running the above command is as user Robert (me... cheers :-)) ) and when run by shell_exec maybe gives a different result... RedMon runs it as SYSTEM user.

                        Thank you for any tip.

                        Robert

                          Write a Reply...