I am trying to print the content(screen) of the browser-client to the client's printer
or even a already created html page at server.
I have found a way with Javascript and VBScript from MSDN.
Is there anyway to achieve something like
that using only PHP ?

    PHP cannot easily control the client's browser, except indirectly by generating Javascript which does.

    The "problem" is that PHP's work is finished once it generates the page to be sent to the browser. PHP is run on the server,

    Javascript, as it is normally encountered, is client-side technology, that is, it runs on the client's browser, not on the web server. (There is server-side javascript, but it only works with Netscape's server, IIRC).

    You could work out a system where the PHP script establishes a TCP connection to the client's computer and sends a file to the client's own printer (e.g. using a combination of popen() and ssh), but

    (a) there are going to be major security issues here,
    and
    (b) you need tight control over both ends of the connection (client and server); you will simply not find a solution that will work on most client's machines.

    May this be informative,

    AC

      a year later

      What exactly are you trying to do?
      If it's an application that needs printing it can be done depending on the setup you have, I have an app that runs on a Linux box and I print to printers shared on WinX.X machines through "smbspool" that comes with Samba, I also print to queues on the same server and you could also print to other Linux boxes, it just depends on what you're really trying to acomplish....

        5 years later

        Hi all,

        My goal is to print barcodes on barcodes printers from my php/mysql application.

        For this reason I'm wondering about this argument but sending characters and escape codes to barcode printers, instead than generating barcode images to print on standard inkjet printers.

        i.e. this is taken from the SATO CX400 barcode printer

        THE SATO PROGRAMMING LANGUAGE
        A programming language for a printer is a familiar concept to most programmers. It is a group
        of commands that are designed to use the internal intelligence of the printer. The commands,
        which are referred to as SATO Command Codes, contain non-printable ASCII characters
        (such as <STX>, <ETX>, <ESC>) and printable characters. These commands must be
        assembled into an organized block of code to be sent as one data stream to the printer, which in
        turn interprets the command codes and generates the desired label output. The programmer is
        free to use any programming language available to send the desired data to the printer.
        The command codes used by the printers are based upon “Escape” (1B hexadecimal)
        sequences. Typically there are four types of command sequences:
        <ESC>{Command}
        These commands generally tell the printer to perform a specific action, like “clear the
        memory.”
        <ESC>{Command} {Data}
        Commands with this format tell the printer to perform a specific action which is dependent
        upon the following data, like “print X labels”, where the value for X is contained in the data.
        <ESC>{Command} {Parameter}
        These commands set the operational parameters of the printer, like “set the print speed to 3.”
        <ESC> {Command} {Parameter} {Data}
        Some commands can contain both Parameter and Data elements, such as “print a Code 39
        symbol containing the data.”

        now, who would like to "translate" it to me? I mean: can it be done with just php and an apache/mysql/php server running on windows xp pro or windows 2003 server?

        I mean, because of server side php concept, it does seem to me that client printers should be shared to the apache/php/mysq/windows server and then used from the server side to next printing via network on the client side where the printer resides (sorry it is tricky but hope some guru may help me with some ideas).

        What do you think?

        Thank you for reading and for any kind of suggestion and or help.

        Robert

          a year later

          Printing from web clients to there directly connected printers might seem tricky from within a php script, while php enables you to directly print documents to a printer in a windows environment, this means that the printer is attached to the server (on which php resides!), while we generally aim at printing at the client side this needs some further environment configuration...

          In a windows client/server environment where the client is a web browser and the server is the php enabled web server and the printer is attached to each client, please do the following to be able to print to the client's directly connected printer:

          1 - install the printer driver on both the server and the client machines; php needs the driver on the server to connect to the printer and the client needs the driver to know there's a printer!.

          2 - share the printer on the client machine with a unique name related to the client computer name, e.g. if the client computer name is "PC01" name it's attached printer share name as "PC01-PRINTER" so there's a portion of the name which is static "-PRINTER" and the other is dynamic "PC01".

          3 - from a php script you can access the printer of each client (if you have many) which utilize the server's php script!!, how this is done??

          Refering to PHP Manual http://www.php.net/manual/en/ref.printer.php

          jason at matteson dot com wrote a comment in that section its brief conclusion a nice function to access the printer by it's shared name

          function getPrinter($SharedPrinterName) {
          global $REMOTE_ADDR;
          $host = getHostByAddr($REMOTE_ADDR);
          return "\\".$host."\".$SharedPrinterName;
          }

          $handle = printer_open(getPrinter("Eltron"));

          how can i know it's name if i have multiple clients??, simply my modified version of jason's function is as follows:

          function getClientPrinter() {
          $host = getHostByAddr($_SERVER['REMOTE_ADDR']);
          return "\\".$host."\".$host.'-PRINTER';
          }

          $handle = printer_open(getClientPrinter());

          which enables you to dynamically select the client and the printer!!!, ...this is a solution i hope to PHP/Windows Client WebApps Printing, where the shared printer network name is used for communication.

            5 years later
            synchronus;10850405 wrote:

            Printing from web clients to there directly connected printers might seem tricky from within a php script, while php enables you to directly print documents to a printer in a windows environment, this means that the printer is attached to the server (on which php resides!), while we generally aim at printing at the client side this needs some further environment configuration...

            In a windows client/server environment where the client is a web browser and the server is the php enabled web server and the printer is attached to each client, please do the following to be able to print to the client's directly connected printer:

            1 - install the printer driver on both the server and the client machines; php needs the driver on the server to connect to the printer and the client needs the driver to know there's a printer!.

            2 - share the printer on the client machine with a unique name related to the client computer name, e.g. if the client computer name is "PC01" name it's attached printer share name as "PC01-PRINTER" so there's a portion of the name which is static "-PRINTER" and the other is dynamic "PC01".

            3 - from a php script you can access the printer of each client (if you have many) which utilize the server's php script!!, how this is done??

            Refering to PHP Manual http://www.php.net/manual/en/ref.printer.php

            jason at matteson dot com wrote a comment in that section its brief conclusion a nice function to access the printer by it's shared name

            function getPrinter($SharedPrinterName) {
            global $REMOTE_ADDR;
            $host = getHostByAddr($REMOTE_ADDR);
            return "\\".$host."\".$SharedPrinterName;
            }

            $handle = printer_open(getPrinter("Eltron"));

            how can i know it's name if i have multiple clients??, simply my modified version of jason's function is as follows:

            function getClientPrinter() {
            $host = getHostByAddr($_SERVER['REMOTE_ADDR']);
            return "\\".$host."\".$host.'-PRINTER';
            }

            $handle = printer_open(getClientPrinter());

            which enables you to dynamically select the client and the printer!!!, ...this is a solution i hope to PHP/Windows Client WebApps Printing, where the shared printer network name is used for communication.

            Such a wonderfull solution, but How I could print outside the LAN? I mean my server could be in anywhere, because its a rent server.

              You're talking about client-side behavior, so you need a client-side solution. PHP is not a client-side language - it's a server-side one.

                bradgrafelman;11015939 wrote:

                You're talking about client-side behavior, so you need a client-side solution. PHP is not a client-side language - it's a server-side one.

                Any suggestions?

                  jsan22;11015951 wrote:

                  Any suggestions?

                  Use JavaScript and make an AJAX call.

                    For the record Anon, you can get Javascript running quite happily server-side. Node.js is the most popular example.

                    As to the OPs question; couldn't you just navigate to the URL of the page you wish to print and add Javascript to that page that calls window.print() when the page loads? Or am I missing something from the question?

                      Ashley Sheridan;11016119 wrote:

                      For the record Anon, you can get Javascript running quite happily server-side. Node.js is the most popular example.

                      As to the OPs question; couldn't you just navigate to the URL of the page you wish to print and add Javascript to that page that calls window.print() when the page loads? Or am I missing something from the question?

                      Print by window.print() doesnt work because I need to print by commands

                        Print by what commands? You seem to have difficulty understanding the difference between client side and server side. What triggers the printing in the first place?

                          Ashley Sheridan;11016139 wrote:

                          Print by what commands? You seem to have difficulty understanding the difference between client side and server side. What triggers the printing in the first place?

                          I need to print by ESC/POS Command

                            You can't print on the visitors machine with PHP. The only printers PHP can talk to are ones that are either connected directly or are on the same network (either local or via a VPN) and even then it can be hit or miss.

                              Write a Reply...