Is the printer connected to your client or your server?
Jul 31, 2024
7
Level 1
Issue with USB Printing Using Mike42 and X-Printer XP-365B
Hi Laracasts community,
I’m working on integrating USB printing into a Laravel application using the X-Printer XP-365B. I’ve been facing challenges with getting the printer to work correctly. Here's a summary of my setup and the issues I'm encountering:
<?php
namespace App\Services\Printer;
use App\Services\PdfGeneratorService;
use Exception;
use Illuminate\Support\Facades\Log;
use Mike42\Escpos\EscposImage;
use Mike42\Escpos\ImagickEscposImage;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
use Mike42\Escpos\Printer;
class PrinterService
{
protected ?WindowsPrintConnector $connector = null;
protected ?Printer $printer = null;
protected string $pdfPath;
protected bool $failedToInitialize = false;
protected bool $failedToPrint = false;
/**
* @throws Exception
*/
public function __construct(public PdfGeneratorService $pdfGeneratorService)
{
try {
$this->connector = new WindowsPrintConnector("Xprinter_XP-365B");
$this->printer = new Printer($this->connector);
} catch (Exception $e) {
Log::channel('db')->error('Failed to initialize the printer : '.$e->getMessage());
$this->failedToInitialize = true;
}
}
protected function processToPrint(): void
{
$this->printer->initialize();
$this->printer->text("Hello world\n");
$this->printer->cut();
$this->printer->pulse();
$this->printer->close();
dd('no printing');
/*try {
$pages = ImagickEscposImage::loadPdf($this->pdfPath, 260);
foreach ($pages as $page) {
$this->printer->graphics($page, Printer::IMG_DOUBLE_HEIGHT | Printer::IMG_DOUBLE_WIDTH);
}
} catch (Exception $e) {
Log::channel('db')->error('Failed to print : '.$e->getMessage());
$this->failedToPrint = true;
} finally {
$this->printer?->cut();
$this->printer?->close();
$this->pdfGeneratorService->deleteGeneratedFile();
}*/
}
}
Issue: Error: I’m not seeing any print output from the printer. The code is not throwing any errors, but nothing gets printed. Testing: I’ve tested the printer using the Windows test page, and it prints fine.
I can't see where is the problem, the code is not throwing any errors.
Thanks in advance
Please or to participate in this conversation.