Advertisement
Guest User

Front End Script for Setting HP Printer LCD Status

a guest
Nov 15th, 2012
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $msg =  "Your first line     "; // first line (20 chars max)
  4. $msg .= "                    "; // second line, comment this out if your display is one line only
  5.  
  6. if(isset($_GET['firstLine'])) {
  7.     $firstLine = $_GET['firstLine'];
  8.     $len  = strlen($firstLine);
  9.     if($len > 20) {
  10.         $firstLine = substr($firstLine, 0, 19);
  11.      } else {
  12.         for ($i=$len; $i <= 20; $i++) {
  13.             $firstLine .= " ";
  14.         }
  15.     }
  16.     $msg = $firstLine;
  17.  
  18.     if(isset($_GET['secondLine'])) {
  19.         $secondLine = $_GET['secondLine'];
  20.         $len  = strlen($secondLine);
  21.         if($len > 20) {
  22.             $secondLine = substr($secondLine, 0, 19);
  23.         }
  24.         $msg .= $secondLine;
  25.     }
  26.  
  27.     $msg = preg_replace("/[^A-Za-z0-9?![:space:]]/","",$msg); // strip any ASCII commands (eg newlines because they will break the command and print a page) use this if accepting user input.
  28.     $msg = substr($msg,0,40); // (optional) truncate message to 40 characters (2 lines)
  29.  
  30.     $printerIP = '127.0.0.1'; // IPV4 (a.b.c.d) obviously change to your printer IP
  31.  
  32.     $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
  33.     socket_connect($socket,$printerIP,9100) or die("Could not connect to printer.");
  34.     $data = chr(27) . "%-12345X@PJL JOB" . chr(13) . chr(10) .
  35.     "@PJL RDYMSG DISPLAY=\"$msg\"" . chr(13) . chr(10) .
  36.     "@PJL EOJ" . chr(13) . chr(10) .
  37.     chr(27) . "%-12345X";
  38.     socket_write($socket, $data) or die("Could not write to printer. Check connections.");
  39.  
  40.     echo "<strong>Printer status set to:<br>" . $firstLine . "<br>" . $secondLine . "</strong><br>";
  41. }
  42.  
  43. ?>
  44. <!doctype html>
  45. <html lang="en">
  46. <head>
  47.     <meta charset="utf-8">
  48.     <title>Set HP Printer LCD Status</title>
  49. </head>
  50. <body>
  51.     <form method="GET">
  52.         <table>
  53.             <tr>
  54.                 <td><label for="firstLine">First line (20 chars max):</label></td>
  55.                 <td><input type="text" id="firstLie" name="firstLine" size="20"></td>
  56.             </tr>
  57.             <tr>
  58.                 <td><label for="secondLine">Second line (20 chars max):</label></td>
  59.                 <td><input type="text" id="secondLie" name="secondLine" size="20"></td>
  60.             </tr>
  61.             <tr>
  62.                 <td></td>
  63.                 <td><input type="submit" value="Submit"></td>
  64.             </tr>
  65.     </form>
  66. </body>
  67. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement