title: How to access IP Printer Using PHP Or any Other Programming Language
body: <p>I'm kind of stuck while implementing the solution that would help in accessing an IP Printer and for your knowledge, the tech stack that I'm using is mentioned below.</p>
  
  <pre><code>PHP version 7.2,
  Apache 2.4
  mysql 5
  
  Method 1.
  </code></pre>
  
  <p>I've tried implementing it with php_printer.dll extension of PECL but it is not working out, There are following steps I performed,
  1.Downloaded the php_printer.dll file,
  2.Placed it in ext directory of php
  3.enabled the extension by placing extension=php_printer.dll in php.ini file,
  4.restarted the server</p>
  
  <pre><code>Method 2.
  </code></pre>
  
  <p>Using the socket functions. I'd passed the IP address and port to the socket function and created the instance and then performed the write operation.
  This doesn't throw any error but there comes no response from the printer.</p>
  
  <pre><code>Method 1:
  
  $printer_name = "Canon G4010 series"; 
  $handle = printer_open($printer_name);
  printer_start_doc($handle, "My Document");
  printer_start_page($handle);
  $font = printer_create_font("Arial", 100, 100, 400, false, false, false, 0);
  printer_select_font($handle, $font);
  printer_draw_text($handle, 'This sentence should be printed.', 100, 400);
  printer_delete_font($font);
  printer_end_page($handle);
  printer_end_doc($handle);
  printer_close($handle);
  
  
  
  
  
  Method 2: Using Sockets 
  
  &lt;?php
  
  echo "&lt;h2&gt;TCP/IP Connection&lt;/h2&gt;\n";
  
  $port = 9100;
  $ip = "192.168.1.5";
  
  
  
  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  
  if ($socket &lt; 0) {
      echo "socket_create() failed. reason: " . socket_strerror($socket) . "\n";
  }else {
      echo "OK.\n";
  }
  
  echo "Try to connect '$ip' Port '$port'...\n";
  $result = socket_connect($socket, $ip, $port);
  if ($result &lt; 0) {
      echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
  }else {
      echo "Connect OK\n";
  }
  
  $in = "This is Robin, I'm testing whether the printer is working fine or not!!\r\n";
  $out = '';
  
  if(!socket_write($socket, $in, strlen($in))) {
      echo "socket_write() failed. reason: " . socket_strerror($socket) . "\n";
  }else {
      echo "Send Message to Server Successfully!\n";
      echo "Send Information:&lt;font color='red'&gt;$in&lt;/font&gt; &lt;br&gt;";
  }
  
  while($out = socket_read($socket, 8192)) {
      echo "Receive Server Return Message Successfully!\n";
      echo "Received Message:",$out;
  }
  
  
  echo "Turn Off Socket...\n";
  socket_close($socket);
  echo "Turn Off OK\n";
  ?&gt;
  </code></pre>
  
  <p>Expectation: To get the document printed after running the file</p>
  
  <p>Error For <code>Method1:</code></p>
  
  <pre><code>PHP Warning:  PHP Startup: Unable to load dynamic library 'php_printer.dll' (tried: D:/Webhost/Php/72/ext/php_printer.dll (%1 is not a valid Win32 application.), D:/Webhost/Php/72/ext/php_php_printer.dll.dll (The specified module could not be found.)) in Unknown on line 0
  </code></pre>
  
  <p>Method2: Doesn't Throw any error but doesn't perform the expected task.</p>