• Products
    •  Barcode Professional
      Encoder / Writer / Generator

      •  Compare Barcode Professional Products...
      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)
      • SDK for .NET Windows
      • for Windows Forms
      • for WPF
      • for Blazor
      • for ASP.NET (Web Forms & MVC)
      • for Reporting Services (SSRS)

       ThermalLabel
      Design & Print Barcode Thermal Labels

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)
      • Visual Editor for Windows Add-on
      • Web Editor for ASP.NET Add-on

      Web Printing Solutions

      •  Compare JSPrintManager vs WebClientPrint...

      JS Print Manager
      Printing Solution for Any Web Platform

      • for Any Web Platform
      • for Blazor

       WebClientPrint
      RAW & File Printing for ASP.NET & PHP

      • for ASP.NET (Core, MVC & WebForms)
      • for PHP

       Printer Emulator SDKs

      ZPL Printer Emulator SDK
      Convert, Preview & Render ZPL to Image & PDF

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)

      EPL Printer Emulator SDK
      Convert, Preview & Render EPL to Image & PDF

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)

       Virtual Printer Drivers

      Virtual ZPL Printer Driver
      Convert, Print, Preview & Render ZPL

      • Virtual ZPLPrinter Driver for Windows & macOS

      Virtual EPL Printer Driver
      Convert, Print, Preview & Render EPL

      • Virtual EPLPrinter Driver for Windows & macOS

       On-Premise Web APIs for Docker

      • BarcodePro Web API for Docker
        2D & Linear Barcode Encoder / Writer / Generator
      • ThermalLabel Web API for Docker
        Design, Preview, Export & Print Barcode Thermal Labels
      • ZPLPrinter Web API for Docker
        Convert, Preview & Render ZPL to Image & PDF
      • EPLPrinter Web API for Docker
        Convert, Preview & Render EPL to Image & PDF

       Download

      • Take advantage of our Trial versions!

      • Download Now!

       Product's Documentation

      • Review product help docs online

      • Help Doc Center

       Tech Questions?

      • Get assistance right from members of our Dev Team!

      • Contact Support

  • Downloads
    •  Barcode Professional
      Encoder / Writer / Generator

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)
      • SDK for .NET Windows
      • for Windows Forms
      • for WPF
      • for Blazor
      • for ASP.NET (Web Forms & MVC)
      • for Reporting Services (SSRS)

       ThermalLabel
      Design & Print Barcode Thermal Labels

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)
      • VISUAL LABEL EDITORS Add-on
      • Visual Editor for Windows Add-on
      • Web Editor for ASP.NET Add-on
      • ASP.NET CLIENT SIDE PRINT
      • TLClientPrint (ASP.NET Client Print)

      JS Print Manager
      Printing Solution for Any Web Platform

      • WEB DEV SIDE
      • Javascript Object
      • Blazor Component
      • CLIENT SIDE
      • JSPM for Windows, Linux, RPi, Android & Mac

       WebClientPrint
      RAW & File Printing for ASP.NET & PHP

      • SERVER SIDE
      • for ASP.NET (Core, MVC & WebForms)
      • for PHP
      • CLIENT SIDE
      • WCPP for Windows, Linux, RPi & Mac

       Download Printer Emulator SDKs

      ZPL Printer Emulator SDK
      Convert, Preview & Render ZPL to Image & PDF

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)

      EPL Printer Emulator SDK
      Convert, Preview & Render EPL to Image & PDF

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)

       Download Virtual Printer Drivers

      Virtual ZPL Printer Driver
      Convert, Print, Preview & Render ZPL

      • Virtual ZPLPrinter Driver for Windows & macOS

      Virtual EPL Printer Driver
      Convert, Print, Preview & Render EPL

      • Virtual EPLPrinter Driver for Windows & macOS

       Download On-Premise Web APIs for Docker

      • BarcodePro Web API for Docker
        2D & Linear Barcode Encoder / Writer / Generator
      • ThermalLabel Web API for Docker
        Design, Preview, Export & Print Barcode Thermal Labels
      • ZPLPrinter Web API for Docker
        Convert, Preview & Render ZPL to Image & PDF
      • EPLPrinter Web API for Docker
        Convert, Preview & Render EPL to Image & PDF
  • Get Help
    •  Contact Support


      •  Get assistance right from members of our Dev Team!
      •  24hs Max Turn-around
      •  Bilingual - English / Español

      • Contact Support Now!

       Product's Documentation


      •  Review product help docs online

      • Go to Documentation Center

       Barcode Center


      •  Learn more about barcode symbologies
      •  Linear (1D), Postal, Component Composite, MICR & 2D

      • Go to Barcode Center

  • Articles
    •  Barcode Professional
      Encoder / Writer / Generator

      • SDK for .NET Windows
      • for Windows Forms
      • for WPF
      • for ASP.NET (Web Forms & MVC)
      • for Reporting Services (SSRS)

       ThermalLabel
      Design & Print Barcode Thermal Labels

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)

      ZPL Printer Emulator SDK
      Convert, Preview & Render ZPL to Image & PDF

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)

      JS Print Manager
      Printing Solution for Any Web Platform

      • for Any Web Platform
      • for Blazor

       WebClientPrint
      RAW & File Printing for ASP.NET & PHP

      • for ASP.NET (Core, MVC & WebForms)
      • for PHP
  • Purchase
    •  Barcode Professional
      Encoder / Writer / Generator

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)
      • SDK for .NET Windows
      • for Windows Forms
      • for WPF
      • for Blazor
      • for ASP.NET (Web Forms & MVC)
      • for Reporting Services (SSRS)

       ThermalLabel
      Design & Print Barcode Thermal Labels

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)
      • Visual Editor for Windows Add-on
      • Web Editor for ASP.NET Add-on

      JS Print Manager
      Printing Solution for Any Web Platform

      • for Any Web Platform
      • for Blazor

       WebClientPrint
      RAW & File Printing for ASP.NET & PHP

      • for ASP.NET (Core, MVC & WebForms)
      • for PHP

       Buy Printer Emulator SDKs

      ZPL Printer Emulator SDK
      Convert, Preview & Render ZPL to Image & PDF

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)

      EPL Printer Emulator SDK
      Convert, Preview & Render EPL to Image & PDF

      • SDK for .NET Standard (.NET, CORE, Xamarin, Mono & UWP)

       Buy Virtual Printer Drivers

      Virtual ZPL Printer Driver
      Convert, Print, Preview & Render ZPL

      • Virtual ZPLPrinter Driver for Windows & macOS

      Virtual EPL Printer Driver
      Convert, Print, Preview & Render EPL

      • Virtual EPLPrinter Driver for Windows & macOS

       Buy On-Premise Web APIs for Docker

      • BarcodePro Web API for Docker
        2D & Linear Barcode Encoder / Writer / Generator
      • ThermalLabel Web API for Docker
        Design, Preview, Export & Print Barcode Thermal Labels
      • ZPLPrinter Web API for Docker
        Convert, Preview & Render ZPL to Image & PDF
      • EPLPrinter Web API for Docker
        Convert, Preview & Render EPL to Image & PDF

      • FAQ
      • Licensing & EULA
      • Request a Quote
      • Renewal & Upgrade
      • Export Regulations
      • Distributors & Resellers
  • Company
    •  About Us

      Founded in 2004, Neodynamic designs and develops Barcode, Imaging, Labeling & Printing Tools for .NET & Web developers. We are experts in those fields with strong know-how on .NET, ASP.NET, SSRS, PHP & HTML/JS projects. More...

       Contact Us

       Customers

      Our products are used by many of the world's most important companies, government agencies, banks, and universities. See our most notable customers...

       Testimonials

       Awards

      • Neodynamic is recognized by its high-quality products and first-class support services that have been acknowledged by its customers around the world.

       Blog

  • Products
  • Printing
  • WebClientPrint
  • for PHP
  • Articles

How to print raw ESC/POS commands from PHP directly to the client printer


Product WebClientPrint for PHP Published 12/27/2013 Updated 10/28/2020 Author Neodynamic

Overview

Many kind of printers like dot-matrix, impact, kiosk, thermal, inkjet, etc; do internally handle ESC/POS (a.k.a. ESC/P) commands which are processed to produce the output printing. ESC/POS was designed by EPSON and is widely used by many other printer brands mainly on POS (Point Of Sales or Point of Services) scenarios like retail, banking, hospitality, and healthcare.

ESC/POS commands are very simple and the main character you'll find in each command is ESC i.e. ASCII hex 1B (or dec 27). Almost any ESC/POS command will start with the ESC (hex 1B) character although it's not the only one as you'll see in the next sample later. The main advantage of using raw ESC/POS commands for printing instead of using the built-in browser javascript printing (window.print();) is that the printing performance will be way faster; a factor that is key in the aforementioned scenarios. In PHP websites, you'll be able to use raw printing feature thanks to our WebClientPrint for PHP solution that was specially designed for this kind of printing needs.

In this walkthrough, you'll learn how to print raw ESC/POS commands from a PHP website directly to the client printer without displaying a print dialog at all. You'll be able to print ESC/POS commands to the Default client printer as well as to any other installed printer at the client machine. This solution works with any popular browser like Chrome, Firefox, Opera & Safari on Windows, Linux, Raspberry Pi or Mac systems!

The ESC/POS commands that we'll use in this article will print out a simple retail receipt that will look like this:

A Sample Receipt printed from PHP and created by using ESC/POS commands
A Sample Receipt printed from PHP and created by using ESC/POS commands

How to specify ESC/POS commands in PHP

ESC/POS commands are composed of a set of simple bytes (from 00 up to FF in hex notation) and most of them always starts with ESC which is byte 1B.
Some commands require additional parameters which have to be expressed in bytes too. For example, the following shows how the ESC/POS command for "emphasized text " is found in the reference manual and how it should be specified in bytes.

  • ESC/POS command for: "Emphasized mode selected."
  • From the ESC/POS Reference Manual:
    ESC ! 8
  • It must be specified in bytes (HEX notation) as:
    0x1B!0x08
    Notice that ESC needs to be specified as 0x1B and the integer parameter value is NOT digit 8 char (hex 38) BUT byte hex 08!!! Be aware of the way you must specify commands & params values as a misunderstanding on this subject might carry unexpected printing results or no printing output at all.

Later on this article, you'll find the ESC/POS commands and their explanation that you can use in PHP to print the sample receipt shown above.

Try Online Demo!

Requirements

Development/Server-side

  WebClientPrint 6.0 for PHP (or greater)
  PHP 5.3 (or greater)
  jQuery 1.4.1 (or greater)

Client-side

  WebClientPrint Processor 5.0 for Windows, Linux, Raspberry Pi & Mac

Follow up these steps

  • Download & install WebClientPrint for PHP
  • Create a new PHP Website naming it PrintESCPOSSample
  • Now follow up the instructions for each PHP Classic or PHP MVC/Laravel:

    • PHP Classic
    • PHP MVC/Laravel

    • Create a new folder called wcpcache at your project's root folder

    • Copy the WebClientPrint.php to your project's root folder

    Creating Controllers

    • Create a new PHP file at root folder and name it WebClientPrintController.php and then copy/paste the following code:

    <?php
    //*********************************
    // IMPORTANT NOTE 
    // 1. In this sample we store users related stuff (like
    // the list of printers and whether they have the WCPP 
    // client utility installed) in the wcpcache folder BUT 
    // you can change it to another different storage (like a DB)!
    // which will be required in Load Balacing scenarios
    // 
    // 2. If your website requires user authentication, then
    // THIS FILE MUST be set to ALLOW ANONYMOUS access!!!
    // 
    //*********************************
     
    include 'WebClientPrint.php';
    use Neodynamic\SDK\Web\WebClientPrint;
     
    //IMPORTANT SETTINGS:
    //===================
    //Set wcpcache folder RELATIVE to WebClientPrint.php file
    //FILE WRITE permission on this folder is required!!!
    WebClientPrint::$wcpCacheFolder = getcwd().'/wcpcache/';
     
    if (file_exists(WebClientPrint::$wcpCacheFolder) == false) {
        //create wcpcache folder
        $old_umask = umask(0);
        mkdir(WebClientPrint::$wcpCacheFolder, 0777);
        umask($old_umask);
    }
     
    //===================
     
    // Clean built-in Cache
    // NOTE: Remove it if you implement your own cache system
    WebClientPrint::cacheClean(30); //in minutes
     
     
    // Process WebClientPrint Request
     
    $urlParts = parse_url($_SERVER['REQUEST_URI']);
    if (isset($urlParts['query'])){
        $query = $urlParts['query'];
        parse_str($query, $qs);
         
        //get session id from querystring if any
        $sid = NULL;
        if (isset($qs[WebClientPrint::SID])){
            $sid = $qs[WebClientPrint::SID];
        }
         
        try{
            //get request type
            $reqType = WebClientPrint::GetProcessRequestType($query);
             
            if($reqType == WebClientPrint::GenPrintScript ||
               $reqType == WebClientPrint::GenWcppDetectScript){
                //Let WebClientPrint to generate the requested script
                 
                //Get Absolute URL of this file
                $currentAbsoluteURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
                $currentAbsoluteURL .= $_SERVER["SERVER_NAME"];
                if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443")
                {
                    $currentAbsoluteURL .= ":".$_SERVER["SERVER_PORT"];
                } 
                $currentAbsoluteURL .= $_SERVER["REQUEST_URI"];
                $currentAbsoluteURL = substr($currentAbsoluteURL, 0, strrpos($currentAbsoluteURL, '?'));
                 
                ob_start();
                ob_clean();
                header('Content-type: text/javascript');
                echo WebClientPrint::generateScript($currentAbsoluteURL, $query);
                return;
            } 
            else if ($reqType == WebClientPrint::ClientSetWcppVersion)
            {
                //This request is a ping from the WCPP utility
                //so store the session ID indicating this user has the WCPP installed
                //also store the WCPP Version if available
                if(isset($qs[WebClientPrint::WCPP_SET_VERSION]) && strlen($qs[WebClientPrint::WCPP_SET_VERSION]) > 0){
                    WebClientPrint::cacheAdd($sid, WebClientPrint::WCP_CACHE_WCPP_VER, $qs[WebClientPrint::WCPP_SET_VERSION]);
                }
                return;
            }
            else if ($reqType == WebClientPrint::ClientSetInstalledPrinters)
            {
                //WCPP Utility is sending the installed printers at client side
                //so store this info with the specified session ID
                WebClientPrint::cacheAdd($sid, WebClientPrint::WCP_CACHE_PRINTERS, strlen($qs[WebClientPrint::WCPP_SET_PRINTERS]) > 0 ? $qs[WebClientPrint::WCPP_SET_PRINTERS] : '');
                return;
            }
            else if ($reqType == WebClientPrint::ClientSetInstalledPrintersInfo)
            {
                //WCPP Utility is sending the installed printers at client side with detailed info
                //so store this info with the specified session ID
                //Printers Info is in JSON format
                $printersInfo = $_POST['printersInfoContent'];
                 
                WebClientPrint::cacheAdd($sid, WebClientPrint::WCP_CACHE_PRINTERSINFO, $printersInfo);
                return;
            }
            else if ($reqType == WebClientPrint::ClientGetWcppVersion)
            {
                //return the WCPP version for the specified Session ID (sid) if any
                ob_start();
                ob_clean();
                header('Content-type: text/plain');
                echo WebClientPrint::cacheGet($sid, WebClientPrint::WCP_CACHE_WCPP_VER);
                return;    
            }
            else if ($reqType == WebClientPrint::ClientGetInstalledPrinters)
            {
                //return the installed printers for the specified Session ID (sid) if any
                ob_start();
                ob_clean();
                header('Content-type: text/plain');
                echo base64_decode(WebClientPrint::cacheGet($sid, WebClientPrint::WCP_CACHE_PRINTERS));
                return;
            }    
            else if ($reqType == WebClientPrint::ClientGetInstalledPrintersInfo)
            {
                //return the installed printers with detailed info for the specified Session ID (sid) if any
                ob_start();
                ob_clean();
                header('Content-type: text/plain');
                echo base64_decode(WebClientPrint::cacheGet($sid, WebClientPrint::WCP_CACHE_PRINTERSINFO));
                return;
            }    
        }
        catch (Exception $ex)
        {
            throw $ex;
        }
         
    } 
    

    • Create a new PHP file and name it PrintESCPOSController.php and then copy/paste the following code:

    <?php
    
    include 'WebClientPrint.php';
    
    use Neodynamic\SDK\Web\WebClientPrint;
    use Neodynamic\SDK\Web\DefaultPrinter;
    use Neodynamic\SDK\Web\InstalledPrinter;
    use Neodynamic\SDK\Web\ClientPrintJob;
    
    // Process request
    // Generate ClientPrintJob? only if clientPrint param is in the query string
    $urlParts = parse_url($_SERVER['REQUEST_URI']);
    
    if (isset($urlParts['query'])) {
        $rawQuery = $urlParts['query'];
        parse_str($rawQuery, $qs);
        if (isset($qs[WebClientPrint::CLIENT_PRINT_JOB])) {
    
            $useDefaultPrinter = ($qs['useDefaultPrinter'] === 'checked');
            $printerName = urldecode($qs['printerName']);
    
            //Create ESC/POS commands for sample receipt
            $esc = '0x1B'; //ESC byte in hex notation
            $newLine = '0x0A'; //LF byte in hex notation
            
            $cmds = '';
            $cmds = $esc . "@"; //Initializes the printer (ESC @)
            $cmds .= $esc . '!' . '0x38'; //Emphasized + Double-height + Double-width mode selected (ESC ! (8 + 16 + 32)) 56 dec => 38 hex
            $cmds .= 'BEST DEAL STORES'; //text to print
            $cmds .= $newLine . $newLine;
            $cmds .= $esc . '!' . '0x00'; //Character font A selected (ESC ! 0)
            $cmds .= 'COOKIES                   5.00'; 
            $cmds .= $newLine;
            $cmds .= 'MILK 65 Fl oz             3.78';
            $cmds .= $newLine . $newLine;
            $cmds .= 'SUBTOTAL                  8.78';
            $cmds .= $newLine;
            $cmds .= 'TAX 5%                    0.44';
            $cmds .= $newLine;
            $cmds .= 'TOTAL                     9.22';
            $cmds .= $newLine;
            $cmds .= 'CASH TEND                10.00';
            $cmds .= $newLine;
            $cmds .= 'CASH DUE                  0.78';
            $cmds .= $newLine . $newLine;
            $cmds .= $esc . '!' . '0x18'; //Emphasized + Double-height mode selected (ESC ! (16 + 8)) 24 dec => 18 hex
            $cmds .= '# ITEMS SOLD 2';
            $cmds .= $esc . '!' . '0x00'; //Character font A selected (ESC ! 0)
            $cmds .= $newLine . $newLine;
            $cmds .= '11/03/13  19:53:17';
    
    		//Create a ClientPrintJob obj that will be processed at the client side by the WCPP
    		$cpj = new ClientPrintJob();
    		//set ESCPOS commands to print...
    		$cpj->printerCommands = $cmds;
            $cpj->formatHexValues = true;
    		
    		if ($useDefaultPrinter || $printerName === 'null') {
    			$cpj->clientPrinter = new DefaultPrinter();
    		} else {
    			$cpj->clientPrinter = new InstalledPrinter($printerName);
    		}
    
    		//Send ClientPrintJob back to the client
    		ob_start();
    		ob_clean();
    		header('Content-type: application/octet-stream');
    		echo $cpj->sendToClient();
    		ob_end_flush();
    		exit();
            
        }
    }
    

    Creating Web Pages

    • The default page is for detecting whether the client machine has the WebClientPrint Processor (WCPP) Utility installed. Create a new PHP page and name it index.php. Then copy/paste the following code:

    <?php
      session_start();
      
      include 'WebClientPrint.php';
      use Neodynamic\SDK\Web\WebClientPrint;
      
    ?>
    <!DOCTYPE html>
    <html>
        <head>
            <title>Detecting WebClientPrint Processor...</title>
            
            <style>
            body{font: 13px 'Segoe UI', Tahoma, Arial, Helvetica, sans-serif;}        
            </style>
        
        </head>
        <body>
            <div id="msgInProgress">
                <div id="mySpinner" style="width:32px;height:32px"></div>
                <br />
                Detecting WCPP utility at client side... 
                <br />
                Please wait a few seconds...
                <br />
            </div>
            <div id="msgInstallWCPP" style="display:none;">    
                <h3>#1 Install WebClientPrint Processor (WCPP)!</h3>
                <p>
                    <strong>WCPP is a native app (without any dependencies!)</strong> that handles all print jobs 
                    generated by the <strong>WebClientPrint PHP component</strong> at the server side. The WCPP 
                    is in charge of the whole printing process and can be 
                    installed on <strong>Windows, Linux, Raspberry Pi & Mac!</strong>
                </p>
                <p>
                    <a href="http://www.neodynamic.com/downloads/wcpp/" target="_blank">Download and Install WCPP from Neodynamic website</a><br />                
                </p>         
                <h3>#2 After installing WCPP...</h3>
                <p>
                    <a href="PrintESCPOS.php">You can go and test the printing page</a>
                </p>
    
            </div>
            
            <!-- Add Reference to jQuery at Google CDN -->
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    
            
            
    
            <script type="text/javascript">
    
                var wcppPingTimeout_ms = 60000; //60 sec
                var wcppPingTimeoutStep_ms = 500; //0.5 sec
    
                function wcppDetectOnSuccess(){
                    // WCPP utility is installed at the client side
                    // redirect to WebClientPrint sample page
    
                    // get WCPP version
                    var wcppVer = arguments[0];
                    if(wcppVer.substring(0, 1) == "6")
                        window.location.href = "PrintESCPOS.php";
                    else //force to install WCPP v6.0
                        wcppDetectOnFailure();
                }
    
                function wcppDetectOnFailure() {
                    // It seems WCPP is not installed at the client side
                    // ask the user to install it
                    $('#msgInProgress').hide();
                    $('#msgInstallWCPP').show();                
                }
    
                
    
            </script>
            
            <?php
            
            //Get Absolute URL of this page
            $currentAbsoluteURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
            $currentAbsoluteURL .= $_SERVER["SERVER_NAME"];
            if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443")
            {
                $currentAbsoluteURL .= ":".$_SERVER["SERVER_PORT"];
            } 
            $currentAbsoluteURL .= $_SERVER["REQUEST_URI"];
        
            //WebClientPrinController.php is at the same page level as WebClientPrint.php
            $webClientPrintControllerAbsoluteURL = substr($currentAbsoluteURL, 0, strrpos($currentAbsoluteURL, '/')).'/WebClientPrintController.php';
    
            // Create WCPP detection script
            echo WebClientPrint::createWcppDetectionScript($webClientPrintControllerAbsoluteURL, session_id());
    
            ?>
        </body>
    </html>
    

    • Add a new PHP page and name it PrintESCPOS.php. Copy/paste the following code:

    <?php 
        session_start();
    
        include 'WebClientPrint.php';
        use Neodynamic\SDK\Web\WebClientPrint;
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>How to directly Print ESCPOS Commands without Preview or Printer Dialog</title>
    </head>
    <body>
        <!-- Store User's SessionId -->
        <input type="hidden" id="sid" name="sid" value="<?php echo session_id(); ?>" />
        
        <h1>How to directly Print ESCPOS Commands without Preview or Printer Dialog</h1>
        <label class="checkbox">
            <input type="checkbox" id="useDefaultPrinter" /> <strong>Use default printer</strong> or...
        </label>
        <div id="loadPrinters">
        <br />
        WebClientPrint can detect the installed printers in your machine.
        <br />
        <input type="button" onclick="javascript:jsWebClientPrint.getPrinters();" value="Load installed printers..." />
                        
        <br /><br />
        </div>
        <div id="installedPrinters" style="visibility:hidden">
        <br />
        <label for="installedPrinterName">Select an installed Printer:</label>
        <select name="installedPrinterName" id="installedPrinterName"></select>
        </div>
                
        <br /><br />
        <input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') + '&printerName=' + $('#installedPrinterName').val());" value="Print Label..." />
            
        <script type="text/javascript">
            var wcppGetPrintersTimeout_ms = 60000; //60 sec
            var wcppGetPrintersTimeoutStep_ms = 500; //0.5 sec
    
            function wcpGetPrintersOnSuccess(){
                // Display client installed printers
                if(arguments[0].length > 0){
                    var p=arguments[0].split("|");
                    var options = '';
                    for (var i = 0; i < p.length; i++) {
                        options += '<option>' + p[i] + '</option>';
                    }
                    $('#installedPrinters').css('visibility','visible');
                    $('#installedPrinterName').html(options);
                    $('#installedPrinterName').focus();
                    $('#loadPrinters').hide();                                                        
                }else{
                    alert("No printers are installed in your system.");
                }
            }
    
            function wcpGetPrintersOnFailure() {
                // Do something if printers cannot be got from the client
                alert("No printers are installed in your system.");
            }
        </script>
        
        <!-- Add Reference to jQuery at Google CDN -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
    
        <?php
        //Get Absolute URL of this page
        $currentAbsoluteURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
        $currentAbsoluteURL .= $_SERVER["SERVER_NAME"];
        if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443")
        {
            $currentAbsoluteURL .= ":".$_SERVER["SERVER_PORT"];
        } 
        $currentAbsoluteURL .= $_SERVER["REQUEST_URI"];
        
        //WebClientPrinController.php is at the same page level as WebClientPrint.php
        $webClientPrintControllerAbsoluteURL = substr($currentAbsoluteURL, 0, strrpos($currentAbsoluteURL, '/')).'/WebClientPrintController.php';
        
        //PrintESCPOSController.php is at the same page level as WebClientPrint.php
        $printESCPOSControllerAbsoluteURL = substr($currentAbsoluteURL, 0, strrpos($currentAbsoluteURL, '/')).'/PrintESCPOSController.php';
        
        //Specify the ABSOLUTE URL to the WebClientPrintController.php and to the file that will create the ClientPrintJob object
        echo WebClientPrint::createScript($webClientPrintControllerAbsoluteURL, $printESCPOSControllerAbsoluteURL, session_id());
        ?>
           
    
    </body>
    </html>
    

    WebClientPrint Setup for Laravel 8+

    • Create a new folder called WebClientPrint under app folder of your project

    • Copy the WebClientPrint.php to that WebClientPrint folder

    • Create a new folder called wcpcache under the WebClientPrint folder

    Creating Controllers

    • Add a new Controller to your project and name it WebClientPrintController.php. Then copy/paste the following code:

    <?php
     
    namespace App\Http\Controllers;
     
    use Illuminate\Http\Request;
     
    //*********************************
    // IMPORTANT NOTE 
    // 1. In this sample we store users related stuff (like
    // the list of printers and whether they have the WCPP 
    // client utility installed) in the wcpcache folder BUT 
    // you can change it to another different storage (like a DB)!
    // which will be required in Load Balacing scenarios
    // 
    // 2. If your website requires user authentication, then
    // THIS FILE MUST be set to ALLOW ANONYMOUS access!!!
    //
    // 3. EXCLUDE this Controller from CSRF Protection
    // Ref https://laravel.com/docs/5.5/csrf#csrf-excluding-uris
    //*********************************
     
    //Includes WebClientPrint classes
    include_once(app_path() . '\WebClientPrint\WebClientPrint.php');
    use Neodynamic\SDK\Web\WebClientPrint;
     
     
    class WebClientPrintController extends Controller
    {
     
        public function processRequest(Request $request){
     
             
            //IMPORTANT SETTINGS:
            //===================
            //Set wcpcache folder RELATIVE to WebClientPrint.php file
            //FILE WRITE permission on this folder is required!!!
            WebClientPrint::$wcpCacheFolder = app_path() . '/WebClientPrint/wcpcache/';
            //===================
     
            // Clean built-in Cache
            // NOTE: Remove it if you implement your own cache system
            WebClientPrint::cacheClean(30); //in minutes
     
            //get session id from querystring if any
            $sid = NULL;
            if ($request->has(WebClientPrint::SID)){
                $sid = $request->input(WebClientPrint::SID);
            }
             
            try{
                //get query string from url
                $query = substr($request->fullUrl(), strpos($request->fullUrl(), '?')+1);
                //get request type
                $reqType = WebClientPrint::GetProcessRequestType($query);
                 
                if($reqType == WebClientPrint::GenPrintScript ||
                $reqType == WebClientPrint::GenWcppDetectScript){
                    //Let WebClientPrint to generate the requested script
                     
                    //Get Absolute URL of this file
                    $currentAbsoluteURL = substr($request->fullUrl(), 0, strrpos($request->fullUrl(), '?'));
                     
                    // Return WebClientPrint's javascript code 
                    return response(WebClientPrint::generateScript($currentAbsoluteURL, $query))
                            ->header('Content-Type', 'text/javascript');
                     
                } 
                else if ($reqType == WebClientPrint::ClientSetWcppVersion)
                {
                    //This request is a ping from the WCPP utility
                    //so store the session ID indicating this user has the WCPP installed
                    //also store the WCPP Version if available
                    if($request->has(WebClientPrint::WCPP_SET_VERSION) && strlen($request->input(WebClientPrint::WCPP_SET_VERSION)) > 0){
                        WebClientPrint::cacheAdd($sid, WebClientPrint::WCP_CACHE_WCPP_VER, $request->input(WebClientPrint::WCPP_SET_VERSION));
                    }
                    return;
                }
                else if ($reqType == WebClientPrint::ClientSetInstalledPrinters)
                {
                    //WCPP Utility is sending the installed printers at client side
                    //so store this info with the specified session ID
                    WebClientPrint::cacheAdd($sid, WebClientPrint::WCP_CACHE_PRINTERS, strlen($request->input(WebClientPrint::WCPP_SET_PRINTERS)) > 0 ? $request->input(WebClientPrint::WCPP_SET_PRINTERS) : '');
                    return;
                }
                else if ($reqType == WebClientPrint::ClientSetInstalledPrintersInfo)
                {
                    //WCPP Utility is sending the installed printers at client side with detailed info
                    //so store this info with the specified session ID
                    //Printers Info is in JSON format
                    $printersInfo = $request->input('printersInfoContent');
                     
                    WebClientPrint::cacheAdd($sid, WebClientPrint::WCP_CACHE_PRINTERSINFO, $printersInfo);
                    return;
                }
                else if ($reqType == WebClientPrint::ClientGetWcppVersion)
                {
                    //return the WCPP version for the specified Session ID (sid) if any
                    return response(WebClientPrint::cacheGet($sid, WebClientPrint::WCP_CACHE_WCPP_VER))
                            ->header('Content-Type', 'text/plain');                 
                }
                else if ($reqType == WebClientPrint::ClientGetInstalledPrinters)
                {
                    //return the installed printers for the specified Session ID (sid) if any
                    return response(base64_decode(WebClientPrint::cacheGet($sid, WebClientPrint::WCP_CACHE_PRINTERS)))
                            ->header('Content-Type', 'text/plain'); 
                }    
                else if ($reqType == WebClientPrint::ClientGetInstalledPrintersInfo)
                {
                    //return the installed printers with detailed info for the specified Session ID (sid) if any
                    return response(base64_decode(WebClientPrint::cacheGet($sid, WebClientPrint::WCP_CACHE_PRINTERSINFO)))
                            ->header('Content-Type', 'text/plain'); 
                }    
            }
            catch (Exception $ex)
            {
                throw $ex;
            }
     
        }
         
    }
    

    • Exclude WebClientPrintAPIController from CSRF Protection by editing app\Http\Middleware\VerifyCsrfToken.php so it looks like the following code:

    <?php
    
    namespace App\Http\Middleware;
    
    use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
    
    class VerifyCsrfToken extends BaseVerifier
    {
        /**
         * The URIs that should be excluded from CSRF verification.
         *
         * @var array
         */
        protected $except = [
            'WebClientPrintController'
        ];
    }
    
    

    • Edit the HomeController so it looks like the following code:

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    use Session;
    
    //Includes WebClientPrint classes
    include_once(app_path() . '\WebClientPrint\WebClientPrint.php');
    use Neodynamic\SDK\Web\WebClientPrint;
    
    
    class HomeController extends Controller
    {
        public function index(){
    
            $wcppScript = WebClientPrint::createWcppDetectionScript(action('WebClientPrintController@processRequest'), Session::getId());    
    
            return view('home.index', ['wcppScript' => $wcppScript]);
        }
    
        public function printESCPOS(){
            $wcpScript = WebClientPrint::createScript(action('WebClientPrintController@processRequest'), action('PrintESCPOSController@printCommands'), Session::getId());    
    
            return view('home.printESCPOS', ['wcpScript' => $wcpScript]);
        }
        
    }
    

    • Add a new Controller and name it PrintESCPOSController and paste the following code:

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    
    //*********************************
    // IMPORTANT NOTE 
    // ==============
    // If your website requires user authentication, then
    // THIS FILE MUST be set to ALLOW ANONYMOUS access!!!
    //
    //*********************************
    
    //Includes WebClientPrint classes
    include_once(app_path() . '\WebClientPrint\WebClientPrint.php');
    use Neodynamic\SDK\Web\WebClientPrint;
    use Neodynamic\SDK\Web\Utils;
    use Neodynamic\SDK\Web\DefaultPrinter;
    use Neodynamic\SDK\Web\InstalledPrinter;
    use Neodynamic\SDK\Web\PrintFile;
    use Neodynamic\SDK\Web\ClientPrintJob;
    
    use Session;
    
    class PrintESCPOSController extends Controller
    {
        public function printCommands(Request $request){
            
           if ($request->exists(WebClientPrint::CLIENT_PRINT_JOB)) {
    
                $useDefaultPrinter = ($request->input('useDefaultPrinter') === 'checked');
                $printerName = urldecode($request->input('printerName'));
                
    			//Create ESC/POS commands for sample receipt
                $esc = '0x1B'; //ESC byte in hex notation
                $newLine = '0x0A'; //LF byte in hex notation
                
                $cmds = '';
                $cmds = $esc . "@"; //Initializes the printer (ESC @)
                $cmds .= $esc . '!' . '0x38'; //Emphasized + Double-height + Double-width mode selected (ESC ! (8 + 16 + 32)) 56 dec => 38 hex
                $cmds .= 'BEST DEAL STORES'; //text to print
                $cmds .= $newLine . $newLine;
                $cmds .= $esc . '!' . '0x00'; //Character font A selected (ESC ! 0)
                $cmds .= 'COOKIES                   5.00'; 
                $cmds .= $newLine;
                $cmds .= 'MILK 65 Fl oz             3.78';
                $cmds .= $newLine . $newLine;
                $cmds .= 'SUBTOTAL                  8.78';
                $cmds .= $newLine;
                $cmds .= 'TAX 5%                    0.44';
                $cmds .= $newLine;
                $cmds .= 'TOTAL                     9.22';
                $cmds .= $newLine;
                $cmds .= 'CASH TEND                10.00';
                $cmds .= $newLine;
                $cmds .= 'CASH DUE                  0.78';
                $cmds .= $newLine . $newLine;
                $cmds .= $esc . '!' . '0x18'; //Emphasized + Double-height mode selected (ESC ! (16 + 8)) 24 dec => 18 hex
                $cmds .= '# ITEMS SOLD 2';
                $cmds .= $esc . '!' . '0x00'; //Character font A selected (ESC ! 0)
                $cmds .= $newLine . $newLine;
                $cmds .= '11/03/13  19:53:17';
    
    			//Create a ClientPrintJob obj that will be processed at the client side by the WCPP
    			$cpj = new ClientPrintJob();
    			//set ESCPOS commands to print...
    			$cpj->printerCommands = $cmds;
    			$cpj->formatHexValues = true;
    			
    			if ($useDefaultPrinter || $printerName === 'null') {
    				$cpj->clientPrinter = new DefaultPrinter();
    			} else {
    				$cpj->clientPrinter = new InstalledPrinter($printerName);
    			}
    		
    			//Send ClientPrintJob back to the client
    			return response($cpj->sendToClient())
    						->header('Content-Type', 'application/octet-stream');
                    
                
            }
        }    
        
    }
    

    Creating/Editing Views

    • Edit the resources / views / layouts / app.blade.php file so it looks like follows:

    
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>WebClientPrint for PHP</title>    
    </head>
    <body>
        
        <div>
            @yield('body')
    	</div>
    
    	<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
        
        @yield('scripts')
    
    </body>
    </html>
    

    • The default View is for detecting whether the client machine has the WebClientPrint Processor (WCPP) Utility installed. Edit the resources / views / home / index.blade.php file and add the folowing section to the BODY:

    
    @extends('layouts.app')
    
    @section('body')
    <div id="msgInProgress">
        
        <h3>Detecting WCPP utility at client side...</h3>
        <h3>Please wait a few seconds...</h3>
        <br />
    </div>
    <div id="msgInstallWCPP" style="display:none;">
        <h3>#1 Install WebClientPrint Processor (WCPP)!</h3>
        <p>
            <strong>WCPP is a native app (without any dependencies!)</strong> that handles all print jobs
            generated by the <strong>WebClientPrint for PHP component</strong> at the server side. The WCPP
            is in charge of the whole printing process and can be
            installed on <strong>Windows, Linux, Mac & Raspberry Pi!</strong>
        </p>
        <p>
            <a href="//www.neodynamic.com/downloads/wcpp/" target="_blank" >Download and Install WCPP from Neodynamic website</a><br />
        </p>
        <h3>#2 After installing WCPP...</h3>
        <p>
            <a href="{{action('HomeController@printESCPOS')}}" >You can go and test the printing page...</a>
        </p>
    
    </div>
    @endsection
    
    @section('scripts')
    <script type="text/javascript">
    
                var wcppPingTimeout_ms = 60000; //60 sec
                var wcppPingTimeoutStep_ms = 500; //0.5 sec
                         
                function wcppDetectOnSuccess(){
                    // WCPP utility is installed at the client side
                    // redirect to WebClientPrint sample page
                    
                    // get WCPP version
                    var wcppVer = arguments[0];
                    if(wcppVer.substring(0, 1) == "6")
                        window.location.href = '{{action('HomeController@printESCPOS')}}';
                    else //force to install WCPP v6.0
                        wcppDetectOnFailure();
                }
    
                function wcppDetectOnFailure() {
                    // It seems WCPP is not installed at the client side
                    // ask the user to install it
                    $('#msgInProgress').hide();
                    $('#msgInstallWCPP').show();
                }
    
    </script>
    
    
    {!! 
    
    // Register the WCPP Detection script code
    // The $wcpScript was generated by HomeController@index
    
    $wcppScript;
    
    !!}
    
    @endsection
    

    • Add a new View with the following name and under such folders: resources / views / home / printESCPOS.blade.php Then, copy/paste the folowing code:

    
    @extends('layouts.app')
    
    @section('body')
    
    <h1>How to directly Print ESCPOS Commands without Preview or Printer Dialog</h1>
    <label class="checkbox">
    	<input type="checkbox" id="useDefaultPrinter" /> <strong>Use default printer</strong> or...
    </label>
    <div id="loadPrinters">
    <br />
    WebClientPrint can detect the installed printers in your machine.
    <br />
    <input type="button" onclick="javascript:jsWebClientPrint.getPrinters();" value="Load installed printers..." />
    				
    <br /><br />
    </div>
    <div id="installedPrinters" style="visibility:hidden">
    <br />
    <label for="installedPrinterName">Select an installed Printer:</label>
    <select name="installedPrinterName" id="installedPrinterName"></select>
    </div>
    		
    <br /><br />
    <input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') + '&printerName=' + $('#installedPrinterName').val());" value="Print Label..." />
    	  
    @endsection
    
    @section('scripts')
    
    <script type="text/javascript">
    	var wcppGetPrintersTimeout_ms = 60000; //60 sec
        var wcppGetPrintersTimeoutStep_ms = 500; //0.5 sec
    
    	function wcpGetPrintersOnSuccess(){
    		// Display client installed printers
    		if(arguments[0].length > 0){
    			var p=arguments[0].split("|");
    			var options = '';
    			for (var i = 0; i < p.length; i++) {
    				options += '<option>' + p[i] + '</option>';
    			}
    			$('#installedPrinters').css('visibility','visible');
    			$('#installedPrinterName').html(options);
    			$('#installedPrinterName').focus();
    			$('#loadPrinters').hide();                                                        
    		}else{
    			alert("No printers are installed in your system.");
    		}
    	}
    
    	function wcpGetPrintersOnFailure() {
    		// Do something if printers cannot be got from the client
    		alert("No printers are installed in your system.");
    	}
    </script>
    
    
    {!! 
    
    // Register the WebClientPrint script code
    // The $wcpScript was generated by PrintESCPOSController@index
    
    $wcpScript;
    
    !!}
    
    @endsection
    

    • Finally, add the following routes to routes / web.php

    
    Route::get('/', 'HomeController@index');
    Route::get('home', 'HomeController@index');
    Route::get('home/index', 'HomeController@index');
    Route::get('home/printESCPOS', 'HomeController@printESCPOS');
    Route::get('PrintESCPOSController', 'PrintESCPOSController@printCommands');
    Route::any('WebClientPrintController', 'WebClientPrintController@processRequest');
    

    • Edit the app\Providers\RouteServiceProvider.php and uncommet the line protected $namespace = 'App\\Http\\Controllers';

  • That's it! Run your website and test it. Click on Print Label to print the ESC/POS Commands without print dialog. You can print it to the Default client printer or you can get a list of the installed printers available at the client machine.

    NOTE You can also print directly to any LPT Parallel Port, RS232 Serial Port or IP/Ethernet Printer although these scenarios have not been considered in this article for simplicity. For further details on those scenarios, please contact our tech support.

Useful? Share it!

Tweet

Need help? Contact Support

We provide best-in-class customer service and support directly from members of our dev team! If we are available when you contact us, you will get a response in few minutes; otherwise the maximum turnaround is 24hs in most cases.

Products

Barcode Professional
Encoder/Writer/Generator

ThermalLabel
Design &Print Barcode Thermal Labels

ZPLPrinter Emulator SDK
Convert, Preview & Render ZPL

JSPrintManager
Printing Solution for Any Web Platform

WebClientPrint
Raw Data Printing for Websites

Downloads

Download fully-functional trials

Get Help

Contact Support

Articles, Tutorials & Guides
Get Help Docs
Barcode Center
GitHub Projects

Purchase

Buy Licenses
Renewal & Upgrade
Request a Quote

Keep in touch

Contact Us
Follow @neodynamic1

Company

Founded in 2004, Neodynamic designs and develops Barcode, Imaging, Labeling & Printing Tools for .NET, PHP, Docker & Web developers. We are experts in those fields with strong know-how on .NET, ASP.NET, SSRS, PHP & HTML/JS projects.

Copyright © 2004 - 2025 Neodynamic - All rights reserved.  |  Privacy Policy  |  Contact Us  |  Sitemap