GTS-MEG's avatar

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

0 likes
7 replies
Snapey's avatar

Is the printer connected to your client or your server?

GTS-MEG's avatar

I'm now working locally on my computer to test

Snapey's avatar

@GTS-MEG Whats the point, you need to print on the client, unless you are installing the printer in your datacenter? So, the final code is unlikely to be php.

GTS-MEG's avatar

@Snapey When a user scans their QR code using a scanner, a Python script intercepts the QR code. The script then sends an HTTP request to my Laravel application to initiate some processes. Following this, the application proceeds to print a ticket for the user. This is happening in one server. the printer is connected to the server via usb. I'm testing this locally with my computer, I have the printer and the qr code scanner with me

Tray2's avatar

@GTS-MEG You should set up a proper printer server with a printer queue and send the printout to the queue instead, that way you don't have to worry about the communication to the printer from your web app.

Vadim_K's avatar

Do you call your function processToPrint? By the way it is protected.

Please or to participate in this conversation.