PHP code example of trupermax / print-agent
1. Go to this page and download the library: Download trupermax/print-agent library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
trupermax / print-agent example snippets
use Trupermax\PrintAgent\PrintAgent;
use Trupermax\PrintAgent\EscPos;
$agent = new PrintAgent();
$bytes = (new EscPos())
->init()
->align('center')
->bold(true)
->textLine('Hola Mundo')
->cut()
->build();
$agent->printEscPos($bytes, useDefaultPrinter: true);
$agent = new PrintAgent(string $baseUrl = 'http://127.0.0.1:9876');
$res = $agent->status();
// ['status' => 'running', 'message' => 'Print Agent funcionando correctamente']
$printers = $agent->printers();
// [['name' => 'EPSON TM-T20', 'driver' => '...', 'port' => '...', 'is_default' => true]]
$agent->printPDF(
pdfBase64: base64_encode(file_get_contents('documento.pdf')),
printerName: 'HP LaserJet',
copies: 2,
);
$agent->printZPL(
raw: '^XA^FO50,50^ADN,36,20^FDHola^FS^XZ',
printerName: 'ZDesigner GK420d',
copies: 1,
);
$bytes = (new EscPos())->init()->textLine('Hola')->cut()->build();
$agent->printEscPos(
data: $bytes,
printerName: 'EPSON TM-T20',
copies: 1,
);
$builder = $agent->escpos();
$bytes = $builder->init()->textLine('Hola')->cut()->build();
(new EscPos())->init()
->align('left') // izquierda (defecto)
->align('center') // centro
->align('right') // derecha
->bold(true)
->bold(false)
->underline(true)
->underline(false)
->fontSize(1) // normal
->fontSize(2) // doble
->fontSize(3) // triple
->fontSize(4) // cuádruple
->text('Subtotal: ')->bold(true)->text('Q.150.00')->bold(false)
->textLine('Hola Mundo')
->line() // 42 guiones
->line('=', 42) // 42 signos igual
->line('*', 20) // 20 asteriscos
->feed() // avanza 1 línea
->feed(3) // avanza 3 líneas
->row('Subtotal', 'Q.150.00')
->row('IVA 12%', 'Q.18.00', 42)
->row('TOTAL', 'Q.168.00')
->columns(
cells: ['Cant.', 'Descripción', 'Precio', 'Total'],
defs: [
['width' => 6, 'align' => 'left'],
['width' => 16, 'align' => 'left'],
['width' => 10, 'align' => 'right'],
['width' => 10, 'align' => 'right'],
]
)
->qr('https://miempresa.com')
->qr('https://miempresa.com', size: 6, errorLevel: 'H')
->barcode('1234567890128')
->barcode('1234567890128', type: 'EAN13', height: 80, textPosition: 'below')
->cut() // corte parcial (defecto)
->cut(false) // corte completo
->raw([0x1b, 0x21, 0x00])
$bytes = (new EscPos())->init()->textLine('Hola')->cut()->build();
use Trupermax\PrintAgent\PrintAgent;
use Trupermax\PrintAgent\EscPos;
$agent = new PrintAgent();
$items = [
['code' => 'A00310', 'desc' => 'TOR HEX GR5 NC 3/8 X 1', 'qty' => 2, 'price' => 2.00],
['code' => 'A03672', 'desc' => 'FAJA A-40 LISA SUPERBELT', 'qty' => 1, 'price' => 37.00],
];
$colDefs = [
['width' => 6, 'align' => 'left'],
['width' => 10, 'align' => 'right'],
['width' => 10, 'align' => 'right'],
['width' => 16, 'align' => 'right'],
];
$escpos = (new EscPos())
->init()
->align('center')
->bold(true)
->textLine('MI EMPRESA S.A.')
->bold(false)
->textLine('NIT: 123456-7')
->feed(1)
->align('left')
->line('-', 42)
->columns(['Cant.', 'Precio', 'Desc.%', 'Total'], $colDefs)
->line('-', 42);
$total = 0;
foreach ($items as $item) {
$itemTotal = $item['qty'] * $item['price'];
$total += $itemTotal;
$escpos
->textLine("{$item['code']} - {$item['desc']}")
->columns(
[(string) $item['qty'], 'Q.' . number_format($item['price'], 2), '', 'Q.' . number_format($itemTotal, 2)],
$colDefs
)
->feed(1);
}
$escpos
->line('-', 42)
->align('right')
->bold(true)
->textLine('TOTAL: Q.' . number_format($total, 2))
->bold(false)
->feed(1)
->align('center')
->qr('https://miempresa.com', size: 4)
->feed(3)
->cut();
$bytes = $escpos->build();
$printers = $agent->printers();
$printer = collect($printers)->firstWhere('is_default', true)['name'] ?? '';
$agent->printEscPos($bytes, printerName: $printer);
bash
# Instalar dependencias
composer install
# Ejecutar la demo manual (requiere Print Agent corriendo en localhost:9876)
php test/manual.php