Configuring an XPrinter Label Printer on Linux in VMware Workstation

An example for setting up in CentOS without a graphical shell; by analogy, you can set it up on any Linux OS.

I solve a specific problem, from php I need to print labels with arbitrary text according to a template. Since the event cannot rely on a stable Internet, and most of the automation tasks overlap with the website, we decided to work with a virtual machine on VMware.

XPrinter is also suitable for marking tasks, it is much easier to install under windows. I settled on the XP-460B model with a label width of up to 108 mm.

Configuring an XPrinter Label Printer on Linux in VMware Workstation

Since I rarely set up Linux and connect devices to it, I was looking for ready-made setup manuals, I realized that the easiest way to connect a printer is through cups. I couldn’t connect the printer via USB, no manipulations on the advice in the manuals helped, I just broke the virtual machine several times.

  • We download the drivers from the manufacturer's website xprintertech.com, they come in one archive for Windows, Mac and Linux

    Drivers are posted on the site for a series of devices, in my case 4 inch Label Printer Drivers. As it turned out, the XP-460B has already been discontinued, I figured out which series it belongs to based on breadcrumbs of a similar XP-470B model.

  • Install the printer in windows, enable sharing

    Configuring an XPrinter Label Printer on Linux in VMware Workstation

  • For Linux, the archive contains 1 file 4BARCODE. This is a 2 in 1 file, a bash script with a tar archive that unpacks itself and copies the drivers into cups. In my case, bzip2 is needed for unpacking (for the 80 mm series, a different archiver is used)
    yum install cups
    yum install bzip2
    chmod 744 ./4BARCODE
    sh ./4BARCODE
    service cups start
    
  • Next you need to open localhost:631 in the browser, for convenience I make the setting to open from the browser in windows. Edit /etc/cups/cupsd.conf:
    Listen localhost:631 мСняСм на Listen *:631
    <Location />
      Order allow,deny
      Allow localhost
      Allow 192.168.1.*  
    </Location>
    <Location /admin>
      Order allow,deny
      Allow localhost
      Allow 192.168.1.*
    </Location>
    

    Add port 631 to firewall (or iptables):

    firewall-cmd --zone=public --add-port=631/tcp --permanent
    firewall-cmd --reload
    
  • We open the link in the browser by the IP of the virtual machine, in my case 192.168.1.5:631/admin

    Add a printer (you need to enter root and password)

    Configuring an XPrinter Label Printer on Linux in VMware Workstation

  • There are 2 options that I managed to configure, via the LPD protocol and via samba.
    1. To connect via the LPD protocol, you need to enable the service in windows (Turn Windows features on or off), restart the computer.

      Configuring an XPrinter Label Printer on Linux in VMware Workstation
      In the cups settings, enter lpd://192.168.1.52/Xprinter_XP-460B, where 192.168.1.52 is the IP of the computer on which the printer is installed, Xprinter_XP-460B is the name of the printer in the windows sharing settings

      Configuring an XPrinter Label Printer on Linux in VMware Workstation
      Select driver 4BARCODE => 4B-3064TA

      Configuring an XPrinter Label Printer on Linux in VMware Workstation
      We do not select anything in the parameters and do not save! I tried adjusting the label size, but then the printer doesn't work for some reason. The label size can be set in the print job.

      Configuring an XPrinter Label Printer on Linux in VMware Workstation
      Trying to print a test page - done!

    2. Second option. You need to install samba, start, restart cups, then a new connection point will appear in cups, in the settings enter a line like smb://user:[email protected]/Xprinter_XP-460B. Where, user is a user in windows, the user must have a password, authorization does not pass with an empty one.

When everything worked out and the printer printed a test page, jobs can be sent through the console:

lpr -P Xprinter_XP-460B -o media=Custom.100x102mm test.txt

In this example, the label has dimensions of 100x100 mm, 2 mm are selected experimentally. The distance between the labels is 3 mm, but if you set the height to 103 mm, the tape shifts, it is inconvenient to tear off the label. The disadvantage of the LPD protocol is that jobs are sent as to a regular printer, the ESC / P0S format is not sent for printing, the sensor does not calibrate labels.

Then you can work with the printer through php. There are libraries for working with cups, it's easier for me to send a command to the console via exec();

Since ESC/P0S doesn't work, I decided to make templates in pdf via tFPDF library

require_once($_SERVER["DOCUMENT_ROOT"] . "/tfpdf/tfpdf.php");
$w = 100;
$h = 100;
$number = 59;
$pdf = new tFPDF('P', 'mm', [$w, $h]);
$pdf->SetTitle('Information');
$pdf->AddFont('Font', 'B', $_SERVER["DOCUMENT_ROOT"] . '/fonts/opensans-bold.ttf', true);
$pdf->SetTextColor(0,0,0);
$pdf->SetDrawColor(0,0,0);

$pdf->AddPage('P');
$pdf->SetDisplayMode('real','default');
$pdf->Image($_SERVER["DOCUMENT_ROOT"]. '/images/logo_site.png',$w - 4 - 28,$h - 13,28.1,9.6,'');

$pdf->SetFontSize(140);
$pdf->SetXY(0,24);
$pdf->Cell($w,$h - 45, $number,0,0,'C',0);

$pdf->SetFontSize(1);
$pdf->SetTextColor(255,255,255);
$pdf->Write(0, $number);

$pdf->Output('example.pdf','I');

exec('php label.php | lpr -P Xprinter_XP-460B -o media=Custom.100x102mm');

Configuring an XPrinter Label Printer on Linux in VMware Workstation
Ready. I killed 2 days off to set up, I hope it will be useful to someone.

Source: habr.com

Add a comment