当前位置:首页 > PHP

php实现小票打印

2026-02-14 00:53:29PHP

PHP 实现小票打印的方法

使用 ESC/POS 指令直接打印

ESC/POS 是一种常见的打印机指令集,适用于热敏打印机和点阵打印机。PHP 可以通过发送原始指令控制打印机。

// 连接打印机(假设通过 USB 或网络)
$printer = fopen("php://temp", "r+");

// 发送初始化指令
fwrite($printer, "\x1B\x40");

// 设置居中打印
fwrite($printer, "\x1B\x61\x31");

// 打印标题
fwrite($printer, "=== 销售小票 ===\n");

// 设置左对齐
fwrite($printer, "\x1B\x61\x30");

// 打印商品信息
fwrite($printer, "商品1     10.00\n");
fwrite($printer, "商品2     20.00\n");

// 打印总计
fwrite($printer, "----------------\n");
fwrite($printer, "总计:     30.00\n");

// 切纸
fwrite($printer, "\x1D\x56\x41\x03");

fclose($printer);

使用现成的 PHP 库

mike42/escpos-php 是一个流行的 PHP 库,封装了 ESC/POS 指令。

php实现小票打印

require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;

try {
    $connector = new NetworkPrintConnector("192.168.1.100", 9100);
    $printer = new Printer($connector);

    $printer->setJustification(Printer::JUSTIFY_CENTER);
    $printer->text("=== 销售小票 ===\n");

    $printer->setJustification(Printer::JUSTIFY_LEFT);
    $printer->text("商品1     10.00\n");
    $printer->text("商品2     20.00\n");

    $printer->text("----------------\n");
    $printer->text("总计:     30.00\n");

    $printer->cut();
    $printer->close();
} catch (Exception $e) {
    echo "打印失败: " . $e->getMessage();
}

通过浏览器打印

如果需要从网页打印小票,可以使用 JavaScript 的 window.print() 方法。

php实现小票打印

// PHP 生成 HTML 小票
echo '<div id="receipt" style="width:80mm; font-family: monospace;">
    <h2 style="text-align:center;">=== 销售小票 ===</h2>
    <p>商品1     10.00</p>
    <p>商品2     20.00</p>
    <hr>
    <p>总计:     30.00</p>
</div>';

// JavaScript 打印
echo '<script>window.onload = function() { window.print(); }</script>';

使用 CUPS 打印服务

在 Linux 系统上,可以通过 CUPS 服务打印。

// 生成 PDF 或文本文件
file_put_contents('/tmp/receipt.txt', "=== 销售小票 ===\n商品1     10.00\n商品2     20.00\n总计:     30.00");

// 使用 lpr 命令打印
exec('lpr -P receipt_printer /tmp/receipt.txt');

云打印解决方案

对于云打印需求,可以使用 Google Cloud Print 或其他云打印 API。

$receiptContent = "=== 销售小票 ===\n商品1     10.00\n商品2     20.00\n总计:     30.00";
$printJob = array(
    'title' => '销售小票',
    'content' => base64_encode($receiptContent),
    'contentType' => 'text/plain'
);

// 发送到云打印服务
$ch = curl_init('https://www.google.com/cloudprint/submit');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $printJob);
curl_exec($ch);
curl_close($ch);

注意事项

  • 确保打印机支持 ESC/POS 指令集
  • 网络打印机需要正确配置 IP 和端口
  • 浏览器打印需要用户确认
  • 云打印服务可能需要 OAuth 认证

标签: 小票php
分享给朋友:

相关文章

php实现登录

php实现登录

PHP 登录功能实现 数据库连接与用户表创建 创建一个数据库连接文件 db.php,用于管理数据库连接。MySQL 示例代码如下: <?php $host = 'localhost'; $db…

php实现静态

php实现静态

静态化实现方法 PHP实现静态化主要通过生成HTML文件或缓存机制来减少服务器动态解析的开销。以下是几种常见方法: 生成静态HTML文件 通过file_put_contents()将动态内容保存为H…

php 实现跳转

php 实现跳转

PHP 实现页面跳转的方法 在PHP中,实现页面跳转可以通过多种方式完成,以下是几种常用的方法: 使用header函数 header函数是PHP中最常用的跳转方法,通过发送HTTP头部信息实现跳转。…

php变量的实现

php变量的实现

PHP变量的实现机制 PHP变量的实现基于Zend引擎的底层数据结构。核心结构体zval用于存储变量类型和值,其设计允许动态类型转换和高效内存管理。 zval结构体 PHP变量通过zval结构体实现…

php 路由实现的

php 路由实现的

PHP 路由实现方法 基于原生 PHP 实现路由 创建一个简单的路由解析器,通过解析 URL 路径来调用对应的控制器或函数。 // 定义路由表 $routes = [ '/' => '…

php 实现md5

php 实现md5

使用 PHP 内置函数实现 MD5 PHP 提供了 md5() 函数,可以直接计算字符串的 MD5 哈希值。该函数接受一个字符串参数,返回 32 字符的十六进制哈希值。 $string = "H…