escpos-php icon indicating copy to clipboard operation
escpos-php copied to clipboard

Macros for printer common tasks

Open cesarreyes3 opened this issue 3 years ago • 0 comments
trafficstars

It would be very helpful if we could create macros on the printer class, it could simplify common tasks, for example:

Here, you create 'Item' class for formating a line https://github.com/mike42/escpos-php/blob/f414320fc510afcacd4cbb75902f827399dee429/example/rawbt-receipt.php#L16-L21 https://github.com/mike42/escpos-php/blob/f414320fc510afcacd4cbb75902f827399dee429/example/rawbt-receipt.php#L126-L138 With a macro we can do

Printer::macro('item', function($name = '', $price = '', $dollarSign = false, $width = 48){
        $rightCols = 10;
        $leftCols = $width - $rightCols;
        if ($dollarSign) {
            $leftCols = $leftCols / 2 - $rightCols / 2;
        }
        $left = str_pad($name, $leftCols);

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

//Example
$printer->item('Total', '14.25', true);

@mike42 I could add some test and examples if they are needed

cesarreyes3 avatar Aug 10 '22 18:08 cesarreyes3