adds support for GenericTape label printers, includes class for 53mm tape printer

This commit is contained in:
Chris Olin
2025-05-02 07:14:47 -04:00
parent f013a4c5ea
commit 2c141579dd
4 changed files with 290 additions and 0 deletions

View File

@@ -0,0 +1,103 @@
<?php
namespace App\Models\Labels\Tapes\Generic;
use App\Helpers\Helper;
use App\Models\Labels\Label;
abstract class GenericTape extends Label
{
// Default tape width in mm
protected const TAPE_WIDTH = 42.0;
// Tape properties
protected float $width;
protected float $height;
protected bool $continuous;
protected float $spacing = 0.0; // Space between labels for non-continuous tapes
// Margins in mm
protected float $marginTop;
protected float $marginBottom;
protected float $marginLeft;
protected float $marginRight;
// Element sizing in mm
protected float $titleSize;
protected float $titleMargin;
protected float $fieldSize;
protected float $fieldMargin;
protected float $labelSize;
protected float $labelMargin;
protected float $barcodeMargin;
protected float $tagSize;
/**
* Constructor for generic tape
*
* @param float $width Width of the tape in mm
* @param float $height Height of the label in mm (for continuous tapes, this is the default height)
* @param bool $continuous Whether the tape is continuous or pre-cut
* @param float $spacing Spacing between labels for non-continuous tapes (in mm)
*/
public function __construct(float $width, float $height, bool $continuous = true, float $spacing = 0.0) {
$this->width = $width;
$this->height = $height;
$this->continuous = $continuous;
$this->spacing = $spacing;
// Calculate base font size (7% of tape width)
$baseFontSize = static::TAPE_WIDTH * 0.07;
// Calculate margin (4% of tape width)
$margin = static::TAPE_WIDTH * 0.04;
// Set margins
$this->marginTop = $margin;
$this->marginBottom = $margin;
$this->marginLeft = $margin;
$this->marginRight = $margin;
// Calculate and set element sizing based on base font size
$this->titleSize = $baseFontSize; // Same as base font size
$this->titleMargin = $baseFontSize * 0.3; // 30% of base font size
$this->fieldSize = $baseFontSize * 1.1; // 110% of base font size
$this->fieldMargin = $baseFontSize * 0.1; // 10% of base font size
$this->labelSize = $baseFontSize * 0.7; // 70% of base font size
$this->labelMargin = $baseFontSize * -0.1; // -10% of base font size
$this->barcodeMargin = $baseFontSize * 0.5; // 50% of base font size
$this->tagSize = $baseFontSize * 0.8; // 80% of base font size
}
// Unit of measurement
public function getUnit() { return 'mm'; }
// Label dimensions
public function getWidth() { return $this->width; }
public function getHeight() { return $this->height; }
// Margins
public function getMarginTop() { return $this->marginTop; }
public function getMarginBottom() { return $this->marginBottom; }
public function getMarginLeft() { return $this->marginLeft; }
public function getMarginRight() { return $this->marginRight; }
/**
* Check if this is a continuous tape
*
* @return bool
*/
public function isContinuous() {
return $this->continuous;
}
/**
* Get spacing between labels (for die-cut tapes)
*
* @return float
*/
public function getSpacing() {
return $this->spacing;
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Models\Labels\Tapes\Generic;
abstract class Tape_53mm extends GenericTape
{
// Override tape width to 53mm
protected const TAPE_WIDTH = 53.0;
private float $tapeHeight;
/**
* Constructor for 53mm tape
*
* @param float $height Height of the label in mm (default 30mm)
* @param bool $continuous Whether the tape is continuous or pre-cut
* @param float $spacing Spacing between labels for non-continuous tapes (in mm)
*/
public function __construct(float $height = 30.0, bool $continuous = true, float $spacing = 0.0) {
parent::__construct(self::TAPE_WIDTH, $height, $continuous, $spacing);
$this->tapeHeight = $height;
}
/**
* Get the barcode size ratio for calculations
*
* @return float
*/
public function getBarcodeRatio() {
return 0.9; // Barcode should use 90% of available width
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace App\Models\Labels\Tapes\Generic;
class Tape_53mm_A extends Tape_53mm
{
public function __construct() {
parent::__construct(40.0, true, 0.0);
}
public function getUnit() { return 'mm'; }
public function getSupportAssetTag() { return false; }
public function getSupport1DBarcode() { return false; }
public function getSupport2DBarcode() { return true; }
public function getSupportFields() { return 5; }
public function getSupportLogo() { return false; }
public function getSupportTitle() { return true; }
public function preparePDF($pdf) {
$pdf->SetAutoPageBreak(false);
}
public function write($pdf, $record) {
$pa = $this->getPrintableArea();
$currentX = $pa->x1;
$currentY = $pa->y1;
$usableWidth = $pa->w;
$usableHeight = $pa->h;
if ($record->has('title')) {
static::writeText(
$pdf, $record->get('title'),
$pa->x1, $pa->y1,
'freesans', '', $this->titleSize, 'C',
$pa->w, $this->titleSize, true, 0
);
$currentY += $this->titleSize + $this->titleMargin;
$usableHeight -= $this->titleSize + $this->titleMargin;
}
// Make the barcode as large as possible while still leaving room for fields
$barcodeSize = min($usableHeight * 0.8, $usableWidth * $this->getBarcodeRatio());
if ($record->has('barcode2d')) {
$barcodeX = $pa->x1 + ($usableWidth - $barcodeSize) / 2;
static::write2DBarcode(
$pdf, $record->get('barcode2d')->content, $record->get('barcode2d')->type,
$barcodeX, $currentY,
$barcodeSize, $barcodeSize
);
$currentY += $barcodeSize + $this->barcodeMargin;
}
if ($record->has('fields')) {
foreach ($record->get('fields') as $field) {
static::writeText(
$pdf, $field['label'],
$currentX, $currentY,
'freesans', '', $this->labelSize, 'L',
$usableWidth, $this->labelSize, true, 0
);
$currentY += $this->labelSize + $this->labelMargin;
static::writeText(
$pdf, $field['value'],
$currentX, $currentY,
'freemono', 'B', $this->fieldSize, 'L',
$usableWidth, $this->fieldSize, true, 0, 0.01
);
$currentY += $this->fieldSize + $this->fieldMargin;
}
}
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace App\Models\Labels\Tapes\Generic;
use Illuminate\Support\Collection;
use TCPDF;
class Tape_53mm_B extends Tape_53mm
{
public function getUnit() { return 'mm'; }
public function getSupportAssetTag() { return false; }
public function getSupport1DBarcode() { return false; }
public function getSupport2DBarcode() { return true; }
public function getSupportFields() { return 5; }
public function getSupportLogo() { return false; }
public function getSupportTitle() { return true; }
public function preparePDF($pdf) {
$pdf->SetAutoPageBreak(false);
}
public function write($pdf, $record) {
$pa = $this->getPrintableArea();
$currentX = $pa->x1;
$currentY = $pa->y1;
$usableWidth = $pa->w;
$usableHeight = $pa->h;
if ($record->has('title')) {
static::writeText(
$pdf, $record->get('title'),
$pa->x1, $pa->y1,
'freesans', '', $this->titleSize, 'C',
$pa->w, $this->titleSize, true, 0
);
$currentY += $this->titleSize + $this->titleMargin;
$usableHeight -= $this->titleSize + $this->titleMargin;
}
// Make the barcode as large as possible while still leaving room for fields
$barcodeSize = min($usableHeight * 0.8, $usableWidth * $this->getBarcodeRatio());
if ($record->has('barcode2d')) {
$barcodeX = $pa->x1;
static::write2DBarcode(
$pdf, $record->get('barcode2d')->content, $record->get('barcode2d')->type,
$barcodeX, $currentY,
$barcodeSize, $barcodeSize
);
$currentX += $barcodeSize + $this->barcodeMargin;
$usableWidth -= $barcodeSize + $this->barcodeMargin;
}
if ($record->has('fields')) {
foreach ($record->get('fields') as $field) {
static::writeText(
$pdf, $field['label'],
$currentX, $currentY,
'freesans', '', $this->labelSize, 'L',
$usableWidth, $this->labelSize, true, 0
);
$currentY += $this->labelSize + $this->labelMargin;
static::writeText(
$pdf, $field['value'],
$currentX, $currentY,
'freemono', 'B', $this->fieldSize, 'L',
$usableWidth, $this->fieldSize, true, 0, 0.01
);
$currentY += $this->fieldSize + $this->fieldMargin;
}
}
}
}