Install Library
composer require mike42/escpos-php
Buat Route dan Controller
- Buat route di
routes/web.php
Route::get('/print-struk', [\App\Http\Controllers\PrintController::class, 'printStruk']);
- Buat Controller
php artisan make:controller PrintController
Contoh PrintController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector; // Untuk Windows
// use Mike42\Escpos\PrintConnectors\FilePrintConnector; // Untuk Linux
class PrintController extends Controller
{
public function printStruk()
{
try {
// Ganti nama printer sesuai di komputer kamu (Contoh: "POS-58")
$connector = new WindowsPrintConnector("POS-58");
$printer = new Printer($connector);
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->text("TOKO MAJU JAYA\n");
$printer->text("Jl. Raya Contoh No.123\n");
$printer->feed();
$printer->setJustification(Printer::JUSTIFY_LEFT);
$printer->text("Barang xQty Harga\n");
$printer->text("Buku Tulis x2 10.000\n");
$printer->text("Pulpen x1 5.000\n");
$printer->feed();
$printer->setJustification(Printer::JUSTIFY_RIGHT);
$printer->text("Total: 25.000\n");
$printer->feed(2);
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->text("Terima kasih!\n");
$printer->cut();
$printer->close();
return "Struk berhasil dicetak!";
} catch (\Exception $e) {
return "Gagal mencetak: " . $e->getMessage();
}
}
}
Setting Printer (Windows)
- Pastikan printer thermal kamu sudah terinstall dan dibagikan (shared).
- Cek nama printer di: Control Panel > Devices and Printers
- Klik kanan > Printer Properties > Share this printer.
- Gunakan nama printer sesuai di
WindowsPrintConnector("NamaPrinter")