1. 准备工作 php.ini文件中打开 extension=php_sockets.dll
    2. 确保socket正常运行
    3. <?php
    4. header("Content-type: text/html; charset=utf-8");
    5. class Netprint{
    6. public $host = '192.168.2.200'; //打印机IP
    7. public $port = 9100; //默认端口
    8. public $socket;
    9. function initcon($host, $port = '9100'){//连接打印机
    10. if(!empty($host) && !empty($port)){
    11. $this->host = $host;
    12. $this->port = $port;
    13. $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP) or die("Could not create socket.\n");
    14. socket_set_nonblock($socket);
    15. $con = @socket_connect($socket,$this->host,$this->port) or die("Could not connect server.\n");;
    16. $flag = socket_select($r=array($socket), $w = array($socket), $f = array($socket), 2);
    17. if($flag == '1'){
    18. socket_set_block($socket);
    19. }else{
    20. $flag = socket_select($r=array($socket), $w = array($socket), $f = array($socket), 2);
    21. if($flag == '1'){
    22. socket_set_block($socket);
    23. }else{
    24. return false;
    25. }
    26. }
    27. $this->socket = $socket;
    28. return true;
    29. }else{
    30. return false;
    31. }
    32. }
    33. function initprint(){//初始化打印
    34. if($this->socket){
    35. $sendstr1 = pack("C*", '27', '64');
    36. socket_sendto($this->socket,$sendstr1,strlen($sendstr1),0,$this->host,$this->port);
    37. $sendstr2 = pack("C*", '27', '83');
    38. socket_sendto($this->socket,$sendstr2,strlen($sendstr2),0,$this->host,$this->port);
    39. $sendstr3 = pack("C*", '27', '33', '0');
    40. socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);
    41. return true;
    42. }else{
    43. return false;
    44. }
    45. }
    46. function sendprint($content){//打印内容
    47. if($this->socket){
    48. //$content = mb_convert_encoding($content,"GB2312","UTF-8");
    49. $content = iconv("UTF-8","GB18030",$content);
    50. $sendstr4 = pack('A*', $content);
    51. socket_sendto($this->socket,$sendstr4,strlen($sendstr4),0,$this->host,$this->port);
    52. return true;
    53. }else{
    54. return false;
    55. }
    56. }
    57. function cutter(){//切纸
    58. if($this->socket){
    59. $content = mb_convert_encoding("\n\n\n\n\n\n\n","GB2312","UTF-8");
    60. $sendstr4 = pack('A*', $content);
    61. socket_sendto($this->socket,$sendstr4,strlen($sendstr4),0,$this->host,$this->port);
    62. $sendstr5 = pack("C*", '29', '86', '48');
    63. socket_sendto($this->socket,$sendstr5,strlen($sendstr5),0,$this->host,$this->port);
    64. return true;
    65. }else{
    66. return false;
    67. }
    68. }
    69. function closecon(){//关闭连接
    70. if($this->socket){
    71. socket_close($this->socket);
    72. return true;
    73. }else{
    74. return false;
    75. }
    76. }
    77. function normal(){
    78. if($this->socket){
    79. $sendstr3 = pack("C*", '29', '33', '0');
    80. socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);
    81. return true;
    82. }else{
    83. return false;
    84. }
    85. }
    86. function middle(){//居中
    87. if($this->socket){
    88. $sendstr3 = pack("C*", '27', '97', '49');
    89. socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);
    90. return true;
    91. }else{
    92. return false;
    93. }
    94. }
    95. function width_2x(){//宽*2
    96. if($this->socket){
    97. $sendstr3 = pack("C*", '29', '33', '16');
    98. socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);
    99. return true;
    100. }else{
    101. return false;
    102. }
    103. }
    104. function height_2x(){//高*2
    105. if($this->socket){
    106. $sendstr3 = pack("C*", '29', '33', '1');
    107. socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);
    108. return true;
    109. }else{
    110. return false;
    111. }
    112. }
    113. function bold(){//加粗
    114. if($this->socket){
    115. $sendstr3 = pack("C*", '27', '33', '8');
    116. socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);
    117. return true;
    118. }else{
    119. return false;
    120. }
    121. }
    122. function underline(){//下划线
    123. if($this->socket){
    124. $sendstr3 = pack("C*", '27', '33', '128');
    125. socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);
    126. return true;
    127. }else{
    128. return false;
    129. }
    130. }
    131. function align_left(){//左对齐
    132. if($this->socket){
    133. $sendstr3 = pack("C*", '27', '97', '48');
    134. socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);
    135. return true;
    136. }else{
    137. return false;
    138. }
    139. }
    140. function align_right(){//右对齐
    141. if($this->socket){
    142. $sendstr3 = pack("C*", '27', '97', '50');
    143. socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);
    144. return true;
    145. }else{
    146. return false;
    147. }
    148. }
    149. function font_1x(){//字体大小1x
    150. if($this->socket){
    151. $sendstr3 = pack("C*", '27', '33', '0');
    152. socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);
    153. return true;
    154. }else{
    155. return false;
    156. }
    157. }
    158. }
    159. $net = new Netprint();
    160. $msgtext = '';
    161. $ret = $net->initcon('192.168.00.00'); //打印机IP
    162. if($ret){
    163. $net->initprint();
    164. $net->width_2x();
    165. $content = "宽成二\n";
    166. $net->sendprint($content);
    167. $net->normal();
    168. $net->normal();
    169. $net->height_2x();
    170. $content = "高成二\n";
    171. $net->sendprint($content);
    172. $net->normal();
    173. $net->normal();
    174. $net->width_2x();
    175. $net->height_2x();
    176. //$net->bold();
    177. //$net->middle();
    178. $content = "宽高成二\n";
    179. $net->sendprint($content);
    180. $net->normal();
    181. $net->normal();
    182. $net->font_1x();
    183. $content = "普通字体\n";
    184. $net->sendprint($content);
    185. $net->cutter();
    186. //$net->closecon();
    187. $msgtext = '打印成功';
    188. }else{
    189. $msgtext .= "打印机连接不上 ";
    190. }
    191. echo $msgtext;
    192. ?>