PHP连接打印机

<?php
header("Content-type: text/html; charset=utf-8");

class Netprint{
    public $host = ‘192.168.2.200‘;  //打印机IP
    public $port = 9100;  //默认端口
    public $socket;

    function initcon($host, $port = ‘9100‘){//连接打印机

        if(!empty($host) && !empty($port)){
            $this->host = $host;
            $this->port = $port;
            $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
            socket_set_nonblock($socket);
            $con = @socket_connect($socket,$this->host,$this->port);
            $flag = socket_select($r=array($socket), $w = array($socket), $f = array($socket), 2);
            if($flag == ‘1‘){
                socket_set_block($socket);
            }else{
                $flag = socket_select($r=array($socket), $w = array($socket), $f = array($socket), 2);
                if($flag == ‘1‘){
                    socket_set_block($socket);
                }else{
                    return false;
                }
            }
            $this->socket = $socket;
            return true;
        }else{
            return false;
        }
    }
    function initprint(){//初始化打印
        if($this->socket){
            $sendstr1 = pack("C*", ‘27‘, ‘64‘);
            socket_sendto($this->socket,$sendstr1,strlen($sendstr1),0,$this->host,$this->port);

            $sendstr2 = pack("C*", ‘27‘, ‘83‘);
            socket_sendto($this->socket,$sendstr2,strlen($sendstr2),0,$this->host,$this->port);

            $sendstr3 = pack("C*", ‘27‘, ‘33‘, ‘0‘);
            socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);

            return true;
        }else{
            return false;
        }
    }
    function sendprint($content){//打印内容

        if($this->socket){

            //$content = mb_convert_encoding($content,"GB2312","UTF-8");
            $content = iconv("UTF-8","GB18030",$content);
            $sendstr4 = pack(‘A*‘, $content);
            socket_sendto($this->socket,$sendstr4,strlen($sendstr4),0,$this->host,$this->port);

            return true;
        }else{
            return false;
        }
    }
    function cutter(){//切纸
        if($this->socket){

            $content = mb_convert_encoding("\n\n\n\n\n\n\n","GB2312","UTF-8");
            $sendstr4 = pack(‘A*‘, $content);
            socket_sendto($this->socket,$sendstr4,strlen($sendstr4),0,$this->host,$this->port);

            $sendstr5 = pack("C*", ‘29‘, ‘86‘, ‘48‘);
            socket_sendto($this->socket,$sendstr5,strlen($sendstr5),0,$this->host,$this->port);

            return true;
        }else{
            return false;
        }
    }
    function closecon(){//关闭连接
        if($this->socket){

            socket_close($this->socket);

            return true;
        }else{
            return false;
        }
    }
    function normal(){
        if($this->socket){

            $sendstr3 = pack("C*", ‘29‘, ‘33‘, ‘0‘);
            socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);

            return true;
        }else{
            return false;
        }
    }
    function middle(){//居中
        if($this->socket){

            $sendstr3 = pack("C*", ‘27‘, ‘97‘, ‘49‘);
            socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);

            return true;
        }else{
            return false;
        }
    }
    function width_2x(){//宽*2
        if($this->socket){

            $sendstr3 = pack("C*", ‘29‘, ‘33‘, ‘16‘);
            socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);

            return true;
        }else{
            return false;
        }
    }
    function height_2x(){//高*2
        if($this->socket){

            $sendstr3 = pack("C*", ‘29‘, ‘33‘, ‘1‘);
            socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);

            return true;
        }else{
            return false;
        }
    }
    function bold(){//加粗
        if($this->socket){

            $sendstr3 = pack("C*", ‘27‘, ‘33‘, ‘8‘);
            socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);

            return true;
        }else{
            return false;
        }
    }
    function underline(){//下划线
        if($this->socket){

            $sendstr3 = pack("C*", ‘27‘, ‘33‘, ‘128‘);
            socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);

            return true;
        }else{
            return false;
        }
    }
    function align_left(){//左对齐
        if($this->socket){

            $sendstr3 = pack("C*", ‘27‘, ‘97‘, ‘48‘);
            socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);

            return true;
        }else{
            return false;
        }
    }
    function align_right(){//右对齐
        if($this->socket){

            $sendstr3 = pack("C*", ‘27‘, ‘97‘, ‘50‘);
            socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);

            return true;
        }else{
            return false;
        }
    }
    function font_1x(){//字体大小1x
        if($this->socket){

            $sendstr3 = pack("C*", ‘27‘, ‘33‘, ‘0‘);
            socket_sendto($this->socket,$sendstr3,strlen($sendstr3),0,$this->host,$this->port);

            return true;
        }else{
            return false;
        }
    }
}

$net = new Netprint();
$msgtext = ‘‘;
$ret = $net->initcon(‘192.168.00.00‘); //打印机IP
if($ret){
    $net->initprint();

    $net->width_2x();
    $content = "宽成二\n";
    $net->sendprint($content);

    $net->normal();
    $net->normal();
    $net->height_2x();
    $content = "高成二\n";
    $net->sendprint($content);

    $net->normal();
    $net->normal();
    $net->width_2x();
    $net->height_2x();
    //$net->bold();
    //$net->middle();
    $content = "宽高成二\n";
    $net->sendprint($content);

    $net->normal();
    $net->normal();
    $net->font_1x();
    $content = "普通字体\n";
    $net->sendprint($content);

    $net->cutter();
    //$net->closecon();
    $msgtext = ‘打印成功‘;
}else{
    $msgtext .= "打印机连接不上 ";
}
echo $msgtext;
?>
时间: 2024-10-13 22:25:32

PHP连接打印机的相关文章

C#连接打印机初研究

最近有朋友让我帮忙做个控制打印机复印的程序. 上网查吧. http://www.cnblogs.com/janes/archive/2011/03/15/1985143.html   先找个简单的例子压压惊. 简单. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; usin

在不连接打印机的情况下,为打印机安装驱动程序

大家不知道有没有如下经历:打印机安装在一台XP系统的电脑上,将这台打印机共享出去,其他装有XP系统的局域网用户可以直接从共享找到打印机,通过网络安装驱动程序(其实是COPY那台安装打印机的XP系统电脑上的驱动),就可以使用共享打印机.而如果局域网中有一台电脑安装的是win7或者更高版本windows系统,直接从共享打开打印机时,由于主机上打印机驱动为XP平台的,大多数时候与WIN7(尤其64位)或不兼容,而出现不能安装驱动的情况.这种情况一般是64位的win7或win8系统在连接32位系统的计算

Android开发之蓝牙连接打印机

代码很简单,直接一个布局文件和一个activity.需要的朋友可以直接将这两部分粘贴复制到项目中即可. Activity部分: package com.anhua.bluetooth; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.u

基于 Koa平台Node.js开发的KoaHub.js连接打印机的代码

最近好多小伙伴都在做微信商城的项目,那就给大家分享一个基于 Koa.js 平台的 Node.js web 开发的框架连接微信易联云打印机接口的代码,供大家学习.koahub-yilianyun 微信易联云打印机接口 koahub-yilianyun易联云打印机node接口 Installation $ npm install koahub-yilianyunUse with co-request var printer = require('koahub-yilianyun');var resu

连接打印机服务器

1.进入打印机后台配置服务器IP地址,设置成跟自己的网络同一个网段. 2.PING打印机服务器IP地址,如果通了,则正常. 3.如果是DHCP服务器首先设好IP地址保留段,免得过了IP地址租期,导致无法打印. 4.检查交换机端口绑定.网口插槽.网线是否正常. 5.客户端添加打印机,设置端口IP地址和驱动.

电脑连接打印机

第一步:安装对应打印机的驱动 第二步:在“控制面板---硬件和声音---设备和打印机”中找到对应的打印机,鼠标右击---打印机属性---端口---配置端口,修改“打印机名或IP地址”为对应的打印机就可以了. 原文地址:https://www.cnblogs.com/hh8888-log/p/11249153.html

windows10查看网络连接打印机IP地址

1 寻找打印机 打开控制面板 win+x 图1 控制面板 寻找打印机 图2 打印机 2 寻找打印机属性 找到打印机属性. 图3 打印机属性 3 查看打印机属性 查看打印机属性. 图4 查看打印机属性 4 查看打印机IP 打开打印机URL. 图5 查看打印机IP 原文地址:https://www.cnblogs.com/xdq101/p/12096801.html

java连接打印机

package com.gdpe.ws.bpmn.web;import java.io.File;import java.io.FileInputStream; import javax.print.Doc;import javax.print.DocFlavor;import javax.print.DocPrintJob;import javax.print.PrintService;import javax.print.PrintServiceLookup;import javax.pri

C#调用斑马打印机打印条码标签(支持COM、LPT、USB、TCP连接方式和ZPL、EPL、CPCL指令)

在批量打印商品标签时一般都要加上条码或图片,而这类应用大多是使用斑马打印机,所以我也遇到了怎么打印的问题. 一种办法是用标签设计软件做好模板,在标签设计软件中打印,这种办法不用写代码,但对我来说觉得不能接受,所以尝试代码解决问题. 网上搜索一番,找不到什么资料,基本都是说发送ZPL.EPL指令到打印机,而且还是COM/LPT口连接打印机.后来研究.net的打印类库,发现是用绘图方式打印至打印机的,也叫GDI打印,于是思路有了点突破,那我可以用报表工具画好标签,运行报表时,把结果输出位图,再发送至