编程语言
首页 > 编程语言> > 从PHP打印到POS打印机

从PHP打印到POS打印机

作者:互联网

我们希望打印到运行apache的POS打印机.由于应用程序的设计和部署,应该从服务器进行打印(它应该检测订单并发送到不同的打印机和不同格式的打印…账单,厨房订单等……).出于这个原因和其他(例如来自iPad的访问应用程序),我们丢弃了QZ-Print applet等选项,并且需要直接打印服务器端.

我们搜索了很多,发现有一个名为php-printer的扩展程序,但似乎过时了,只能在WIndows下工作.

我们遵循这个代码:(http://mocopat.wordpress.com/2012/01/18/php-direct-printing-printer-dot-matrix-lx-300/)

$tmpdir = sys_get_temp_dir();   # ambil direktori temporary untuk simpan file.
$file =  tempnam($tmpdir, 'ctk');  # nama file temporary yang akan dicetak
$handle = fopen($file, 'w');
$condensed = Chr(27) . Chr(33) . Chr(4);
$bold1 = Chr(27) . Chr(69);
$bold0 = Chr(27) . Chr(70);
$initialized = chr(27).chr(64);
$condensed1 = chr(15);
$condensed0 = chr(18);
$corte = Chr(27) . Chr(109);
$Data  = $initialized;
$Data .= $condensed1;
$Data .= "==========================\n";
$Data .= "|     ".$bold1."OFIDZ MAJEZTY".$bold0."      |\n";
$Data .= "==========================\n";
$Data .= "Ofidz Majezty is here\n";
$Data .= "We Love PHP Indonesia\n";
$Data .= "We Love PHP Indonesia\n";
$Data .= "We Love PHP Indonesia\n";
$Data .= "We Love PHP Indonesia\n";
$Data .= "We Love PHP Indonesia\n";
$Data .= "--------------------------\n";
$Data .= $corte;
fwrite($handle, $Data);
fclose($handle);
copy($file, "//localhost/KoTickets");  # Lakukan cetak
unlink($file);

它有效,但这会发送纯文本,我们需要发送图像(徽标),并格式化更可爱的账单.我们尝试以相同的方式创建PDF并“发送”到打印机,但只打印空白.

我找到了一个可以使用网络打印机的库(github上的escpos-php),但我们也需要使用USB打印机,以避免客户更换硬件.

一些想法如何实现这一目标?

提前致谢.

解决方法:

这里是escpos-php的作者.

如果您的打印机支持ESC / POS(大多数热敏收据打印机似乎使用它的某些子集),那么我认为该驱动程序将适应您的使用案例:USB或网络打印,徽标,一些格式.其中一些是最近添加的.

USB打印

escpos-php打印到文件指针.在Linux上,您可以使用usblp驱动程序将USB打印机显示为文件,然后只需fopen()它(USB receipt example,blog post about installing a USB printer on Linux).

因此,在USB打印机上打印“Hello world”与打印到联网打印机略有不同:

<?php
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
$connector = new FilePrintConnector("/dev/usb/lp0");
$printer = new Printer($connector);

$printer -> text("Hello World!\n");
$printer -> cut();

$printer -> close();

或者,更像是您当前成功使用的代码,您可以写入临时文件并复制它:

<?php
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;

/* Open file */
$tmpdir = sys_get_temp_dir();
$file =  tempnam($tmpdir, 'ctk');

/* Do some printing */
$connector = new FilePrintConnector($file);
$printer = new Printer($connector);
$printer -> text("Hello World!\n");
$printer -> cut();

$printer -> close();

/* Copy it over to the printer */
copy($file, "//localhost/KoTickets");
unlink($file);

因此,在您的POS系统中,您需要一个根据您的客户配置和首选目标返回文件指针的函数.收据打印机响应速度非常快,但是如果您有几个iPad订单,则应使用文件锁(flock())将操作包装到每台打印机,以避免与并发相关的问题.

另请注意,Windows上的USB支持未经测试.

标志&格式化

一旦弄清楚计划如何与打印机通信,就可以使用全套格式化和图像命令.

可以从PNG文件打印徽标,如下所示:

use Mike42\Escpos\EscposImage;
$logo = EscposImage::load("foo.png");
$printer -> graphics($logo);

对于格式化,README.md和下面的示例应该可以帮助您入门.对于大多数收据,您只需要:

> selectPrintMode()来改变字体大小.
> setEmphasis()切换粗体.
> setJustification()左对齐或居中一些文本或图像.
>每次收据后cut().

我还建议您在当前使用的示例中绘制如下框:

=========
|       |
=========

您可以使用IBM Code page 437中为许多打印机支持的绘图框设计的字符 – 只需在输出中包含字符0xB3到0xDA.它们并不完美,但它看起来不那么“文字”了.

$box = "\xda".str_repeat("\xc4", 10)."\xbf\n";
$box .= "\xb3".str_repeat(" ", 10)."\xb3\n";
$box .= "\xc0".str_repeat("\xc4", 10)."\xd9\n";
$printer -> textRaw($box);

完整的例子

以下示例是also now included与驱动程序.我认为它看起来像一个相当典型的商店收据,格式化,并且可以很容易地适应您的厨房场景.

扫描输出:

用于生成它的PHP源代码:

<?php
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\EscposImage;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;

/* Open the printer; this will change depending on how it is connected */
$connector = new FilePrintConnector("/dev/usb/lp0");
$printer = new Printer($connector);

/* Information for the receipt */
$items = array(
    new item("Example item #1", "4.00"),
    new item("Another thing", "3.50"),
    new item("Something else", "1.00"),
    new item("A final item", "4.45"),
);
$subtotal = new item('Subtotal', '12.95');
$tax = new item('A local tax', '1.30');
$total = new item('Total', '14.25', true);
/* Date is kept the same for testing */
// $date = date('l jS \of F Y h:i:s A');
$date = "Monday 6th of April 2015 02:56:25 PM";

/* Start the printer */
$logo = EscposImage::load("resources/escpos-php.png", false);
$printer = new Printer($connector);

/* Print top logo */
$printer -> setJustification(Printer::JUSTIFY_CENTER);
$printer -> graphics($logo);

/* Name of shop */
$printer -> selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
$printer -> text("ExampleMart Ltd.\n");
$printer -> selectPrintMode();
$printer -> text("Shop No. 42.\n");
$printer -> feed();

/* Title of receipt */
$printer -> setEmphasis(true);
$printer -> text("SALES INVOICE\n");
$printer -> setEmphasis(false);

/* Items */
$printer -> setJustification(Printer::JUSTIFY_LEFT);
$printer -> setEmphasis(true);
$printer -> text(new item('', '$'));
$printer -> setEmphasis(false);
foreach ($items as $item) {
    $printer -> text($item);
}
$printer -> setEmphasis(true);
$printer -> text($subtotal);
$printer -> setEmphasis(false);
$printer -> feed();

/* Tax and total */
$printer -> text($tax);
$printer -> selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
$printer -> text($total);
$printer -> selectPrintMode();

/* Footer */
$printer -> feed(2);
$printer -> setJustification(Printer::JUSTIFY_CENTER);
$printer -> text("Thank you for shopping at ExampleMart\n");
$printer -> text("For trading hours, please visit example.com\n");
$printer -> feed(2);
$printer -> text($date . "\n");

/* Cut the receipt and open the cash drawer */
$printer -> cut();
$printer -> pulse();

$printer -> close();

/* A wrapper to do organise item names & prices into columns */
class item
{
    private $name;
    private $price;
    private $dollarSign;

    public function __construct($name = '', $price = '', $dollarSign = false)
    {
        $this -> name = $name;
        $this -> price = $price;
        $this -> dollarSign = $dollarSign;
    }

    public function __toString()
    {
        $rightCols = 10;
        $leftCols = 38;
        if ($this -> dollarSign) {
            $leftCols = $leftCols / 2 - $rightCols / 2;
        }
        $left = str_pad($this -> name, $leftCols) ;

        $sign = ($this -> dollarSign ? '$' : '');
        $right = str_pad($sign . $this -> price, $rightCols, ' ', STR_PAD_LEFT);
        return "$left$right\n";
    }
}

标签:php,printing,kiosk
来源: https://codeday.me/bug/20190928/1826575.html