php调用云打印,打印小票

本文介绍了如何在PHP开发中实现云打印功能,以满足类似美团接单时的小票打印需求。通过蕊烨云开发后台注册设备,获取SN码,并在PHP后台添加PrintXy.php文件配置用户ID、密钥和打印机SN,最终在需要打印的PHP页面调用相关函数完成打印任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发的过程中,遇到商家需要类似美团接单功能,当收到订单时,要用小票打印机,提示收到新的订单,并且打印,现在以蕊烨xp-c58h(4G)打印机为测试机,使用php后台调用

1、访问蕊烨云开发后台,注册登录后,添加设备,把打印机的sn码添加上去开放平台管理后台珠海移科智能科技有限公司是专业研发、生产、销售芯烨自动接单云打印机、微信订餐打印机、专注智能云打印的公司icon-default.png?t=O83Ahttps://admin.xpyun.net/2、在后台admin/common/library中,添加PrintXy.php文件,添加上自己在蕊烨云创建的用户ID,密钥,以及打印机的SN

<?php

namespace app\common\library;

use think\Config;

/**
 * 邮箱验证码类
 */
class PrintXy
{
    /**
     * 58mm 系列打印机模板
     */
    public const ROW_MAX_CHAR_LEN = 32;
    public const MAX_HEAD_NAME_CHAR_LEN58 = 14;
    private const MAX_NAME_CHAR_LEN = 20;
    private const LAST_ROW_MAX_NAME_CHAR_LEN = 16;
    private const MAX_QUANTITY_CHAR_LEN = 6;
    private const MAX_PRICE_CHAR_LEN = 6;

    /**
     * 初始化
     * @access public
     * @param array $options 参数
     * @return Email
     */
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($options);
        }

        return self::$instance;
    }

    public function test()
    {
       p(111);die;
    }
    public function print($order,$detArr){
        header("Content-type:text/html;charset=utf-8");//必须 官方要求
        header("Access-Control-Allow-Origin: *");//必须 否则可能报跨域错误
        $user = 'xxx';//芯烨云平台注册用户名(开发者 ID)
        $timestamp = time();//当前UNIX时间戳,10位,精确到秒
        $user_key = 'xxx';//用户开发者密钥
        $sign = sha1($user.$user_key.$timestamp);//对参数 user + user_key + timestamp 拼接后(+号表示连接符)进行SHA1加密得到签名,值为40位小写字符串
        $sn = 'xxx';//打印机编号
//        $printContent = '123';//打印内容
        $printContent = '';//打印内容
        $printContent = $printContent . "<C>" . "<B>天龙云小票</B>" . "<BR></C>";
        $printContent = $printContent . "<BR>";

        $printContent = $printContent . "菜名" . str_repeat(" ", self::MAX_HEAD_NAME_CHAR_LEN58) . "数量" . str_repeat(" ", 4) . "单价" . str_repeat(" ", 2)
            . "<BR>";
        $printContent = $printContent . str_repeat("-", self::ROW_MAX_CHAR_LEN) . "<BR>";
        foreach($detArr as $k=>$vo){
            $printContent = $printContent . $this->formatPrintOrderItem($vo['name'], $vo['num'], $vo['price']);
        }
//        $printContent = $printContent . $this->formatPrintOrderItem("可乐鸡翅", 2, 9.99);
//        $printContent = $printContent . $this->formatPrintOrderItem("水煮鱼特辣水煮鱼特辣水煮鱼特辣水煮鱼特辣水煮鱼特辣", 1, 108.0);
//        $printContent = $printContent . $this->formatPrintOrderItem("豪华版超级无敌龙虾炒饭豪华版超级无敌龙虾炒饭豪华版超级无敌龙虾炒饭", 1, 99.9);
//        $printContent = $printContent . $this->formatPrintOrderItem("炭烤鳕鱼炭烤鳕鱼炭烤鳕鱼", 5, 19.99);
//        $printContent = $printContent . $this->formatPrintOrderItem("Grilled cod with charcoal", 5, 19.99);
//        $printContent = $printContent . $this->formatPrintOrderItem("炭烤鳕鱼(Grilled cod with charcoal)", 5, 19.99);
        $printContent = $printContent . str_repeat("-", self::ROW_MAX_CHAR_LEN) . "<BR>";
        $printContent = $printContent . "<R>" . "合计:" . $order['total'] . "元" . "<BR></R>";

        $printContent = $printContent . "<BR>";
        $printContent = $printContent . "<L>"
            . "客户地址:" . $order['address'] . "<BR>"
            . "客户电话:" . $order['phoneNum'] . "<BR>"
            . "下单时间:" . date('Y-m-d H:i:s') . "<BR>"
            . "备注:" .  $order['infotext'] . "<BR>";

        $printContent = $printContent . "<C>"
            . "<QR>https://wxshop.shop</QR>"
            . "</C>";
        $data = json_encode(array("user"=>$user,"timestamp"=>$timestamp,"sign"=>$sign,"sn"=>$sn,"content"=>$printContent));
        $url = "https://platform.xpyun.net/api/openapi/xprinter/print";
        $result = $this->curl_post($url,$data);
//        var_dump($result);
    }

    //php 模拟post请求接口
    function curl_post($url,$data){
        $header = array('Content-Type: application/json; charset=utf-8');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    /**
     * 格式化菜品列表(用于58mm打印机)
     * 注意:默认字体排版,若是字体宽度倍大后不适用
     * 58mm打印机一行可打印32个字符 汉子按照2个字符算
     * 分3列: 名称20字符一般用16字符4空格填充  数量6字符  单价6字符,不足用英文空格填充 名称过长换行
     *
     * @param foodName 菜品名称
     * @param quantity 数量
     * @param price 价格
     * @throws Exception
     */

    public function formatPrintOrderItem($foodName, $quantity, $price)
    {
        $orderNameEmpty = str_repeat(" ", self::MAX_NAME_CHAR_LEN);
        $foodNameLen = $this->CalcGbkLenForPrint($foodName);
//         print("foodNameLen=".$foodNameLen."\n");

        $quantityStr = '' . $quantity;
        $quantityLen = $this->CalcAsciiLenForPrint($quantityStr);
        // print("quantityLen=".$quantityLen."\n");

        $priceStr = '' . round($price, 2);
        $priceLen = $this->CalcAsciiLenForPrint($priceStr);
        // print("priceLen=".$priceLen);

        $result = $foodName;
        $mod = $foodNameLen % self::ROW_MAX_CHAR_LEN;
        // print("mod=".$mod."\n");

        if ($mod <= self::LAST_ROW_MAX_NAME_CHAR_LEN) {
            // 保证各个列的宽度固定,不足部分,利用空格填充
            //make sure all the column length fixed, fill with space if not enough
            $result = $result . str_repeat(" ", self::MAX_NAME_CHAR_LEN - $mod);

        } else {
            // 另起新行
            // new line
            $result = $result . "<BR>";
            $result = $result . $orderNameEmpty;
        }

        $result = $result . $quantityStr . str_repeat(" ", self::MAX_QUANTITY_CHAR_LEN - $quantityLen);
        $result = $result . $priceStr . str_repeat(" ", self::MAX_PRICE_CHAR_LEN - $priceLen);

        $result = $result . "<BR>";

        return $result;
    }
    public function CalcGbkLenForPrint($data)
    {
        return (strlen($data) + mb_strlen($data, "UTF8")) / 2;
    }

    public function CalcAsciiLenForPrint($data)
    {
        return strlen($data);
    }



}

3、在需要调用打印的php页面,引入

use app\common\library\PrintXy;

在代码中调用,把需要调用的参数传进去即可

$pring = new PrintXy();
$pring->print($order,$detArr);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值