Skip to content

Commit 541db78

Browse files
realchrisolinChris Olin
authored and
Chris Olin
committed
adds support for continuous 53mm and 0.59in printers
1 parent 294c720 commit 541db78

File tree

4 files changed

+436
-10
lines changed

4 files changed

+436
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace App\Models\Labels\Tapes\Generic;
4+
5+
abstract class Continuous_53mm extends GenericTape
6+
{
7+
protected const TAPE_WIDTH = 53.0;
8+
9+
// Minimum height for the label
10+
protected float $minHeight = 30.0;
11+
private float $tapeHeight;
12+
13+
/**
14+
* Constructor for 53mm tape
15+
*
16+
* Assumes tape is continuous, set to false and specify
17+
* $spacing in concrete classfor die-cut labels
18+
*
19+
*
20+
* @param float $height Height of the label in mm (default 60mm)
21+
* @param bool $continuous Whether the tape is continuous or pre-cut
22+
* @param float $spacing Spacing between labels for non-continuous tapes (in mm)
23+
*/
24+
public function __construct($height = 60.0, $continuous = true, $spacing = 0.0) {
25+
parent::__construct(self::TAPE_WIDTH, $height, $continuous, $spacing);
26+
$this->tapeHeight = $height;
27+
}
28+
29+
public function getBarcodeRatio() {
30+
return 0.9; // Barcode should use 90% of available width
31+
}
32+
33+
/**
34+
* Calculate the required height for the content
35+
*
36+
* @param $record The record to calculate height for
37+
* @return float The calculated height in mm
38+
*/
39+
protected function calculateRequiredHeight($record) {
40+
$height = $this->marginTop + $this->marginBottom;
41+
42+
// Add title height if present
43+
if ($record->has('title') && $this->getSupportTitle()) {
44+
$height += $this->titleSize + $this->titleMargin;
45+
}
46+
47+
// Add barcode height if present
48+
if (($record->has('barcode2d') && $this->getSupport2DBarcode()) ||
49+
($record->has('barcode') && $this->getSupport1DBarcode())) {
50+
$pa = $this->getPrintableArea();
51+
$usableWidth = $pa->w;
52+
$barcodeSize = $usableWidth * $this->getBarcodeRatio();
53+
$height += $barcodeSize + $this->barcodeMargin;
54+
}
55+
56+
// Add fields height if present
57+
if ($record->has('fields') && $this->getSupportFields() > 0) {
58+
foreach ($record->get('fields') as $field) {
59+
$height += $this->labelSize + $this->labelMargin;
60+
$height += $this->fieldSize + $this->fieldMargin;
61+
}
62+
}
63+
64+
// Add a small buffer to ensure everything fits
65+
$height += 2.0;
66+
67+
// Ensure minimum height
68+
return max($this->minHeight, $height);
69+
}
70+
71+
/**
72+
* Override the writeAll method to support dynamic page sizes for continuous tapes
73+
*/
74+
public function writeAll($pdf, $data) {
75+
// Use auto-sizing for continuous tapes, fixed height for die-cut tapes
76+
if ($this->continuous) {
77+
$data->each(function ($record, $index) use ($pdf) {
78+
// Calculate the required height for this record
79+
$requiredHeight = $this->calculateRequiredHeight($record);
80+
81+
// Temporarily update the height property
82+
$originalHeight = $this->height;
83+
$this->height = $requiredHeight;
84+
85+
// Add a new page with the calculated dimensions
86+
$pdf->AddPage(
87+
$this->getOrientation(),
88+
[$this->getWidth(), $requiredHeight],
89+
false, // Don't reset page number
90+
false // Don't reset object ID
91+
);
92+
93+
// Write the content
94+
$this->write($pdf, $record);
95+
96+
// Restore the original height
97+
$this->height = $originalHeight;
98+
});
99+
} else {
100+
// Use the default implementation for non-continuous (die-cut) tapes
101+
parent::writeAll($pdf, $data);
102+
}
103+
}
104+
}

app/Models/Labels/Tapes/Generic/Tape_53mm_B.php renamed to app/Models/Labels/Tapes/Generic/Continuous_53mm_A.php

+5-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
namespace App\Models\Labels\Tapes\Generic;
44

5-
use Illuminate\Support\Collection;
6-
use TCPDF;
7-
8-
class Tape_53mm_B extends Tape_53mm
5+
class Continuous_53mm_A extends Continuous_53mm
96
{
10-
11-
7+
128
public function getUnit() { return 'mm'; }
139
public function getSupportAssetTag() { return false; }
14-
public function getSupport1DBarcode() { return false; }
10+
public function getSupport1DBarcode() { return true; }
1511
public function getSupport2DBarcode() { return true; }
1612
public function getSupportFields() { return 5; }
1713
public function getSupportLogo() { return false; }
@@ -44,15 +40,14 @@ public function write($pdf, $record) {
4440
$barcodeSize = min($usableHeight * 0.8, $usableWidth * $this->getBarcodeRatio());
4541

4642
if ($record->has('barcode2d')) {
47-
$barcodeX = $pa->x1;
43+
$barcodeX = $pa->x1 + ($usableWidth - $barcodeSize) / 2;
4844

4945
static::write2DBarcode(
5046
$pdf, $record->get('barcode2d')->content, $record->get('barcode2d')->type,
5147
$barcodeX, $currentY,
5248
$barcodeSize, $barcodeSize
5349
);
54-
$currentX += $barcodeSize + $this->barcodeMargin;
55-
$usableWidth -= $barcodeSize + $this->barcodeMargin;
50+
$currentY += $barcodeSize + $this->barcodeMargin;
5651
}
5752

5853
if ($record->has('fields')) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
namespace App\Models\Labels\Tapes\Generic;
4+
5+
use App\Helpers\Helper;
6+
7+
abstract class Continuous_Landscape_0_59in extends GenericTape
8+
{
9+
// abstract class for printers using 0.59in width paper in landscape orientation
10+
// Using a larger TAPE_WIDTH value to increase font sizes
11+
protected const TAPE_WIDTH = 0.59;
12+
private float $tapeHeight;
13+
protected float $minHeight = 1;
14+
15+
/**
16+
* @param float $length Length of the label in inches (default 2.36in which is 60mm)
17+
* @param bool $continuous Whether the tape is continuous or pre-cut
18+
* @param float $spacing Spacing between labels for non-continuous tapes (in inches)
19+
*/
20+
public function __construct($length = 0.6, $continuous = true, $spacing = 0.0) {
21+
// Swap width and height for landscape orientation
22+
// The height becomes the width, and the length becomes the height
23+
parent::__construct($length, self::TAPE_WIDTH, $continuous, $spacing);
24+
$this->tapeHeight = $length;
25+
26+
$this->marginTop = 0.1;
27+
$this->marginBottom = 0.1;
28+
// Keep small horizontal margins
29+
$this->marginLeft = self::TAPE_WIDTH * 0.2;
30+
// $this->marginRight = self::TAPE_WIDTH * 0.1;
31+
32+
// Override font sizes to make them larger
33+
// Calculate a larger base font size (3x the default)
34+
$baseFontSize = self::TAPE_WIDTH * 0.16; // 3x the default 0.07
35+
36+
// Recalculate all element sizing based on the larger base font size
37+
$this->titleSize = $baseFontSize; // Same as base font size
38+
$this->titleMargin = $baseFontSize * 0.3; // 30% of base font size
39+
$this->fieldSize = $baseFontSize * 1.1; // 110% of base font size
40+
$this->fieldMargin = $baseFontSize * 0.1; // 10% of base font size
41+
$this->labelSize = $baseFontSize * 0.7; // 70% of base font size
42+
$this->labelMargin = $baseFontSize * -0.1; // -10% of base font size
43+
$this->barcodeMargin = $baseFontSize * 0.9; // 20% of base font size
44+
$this->tagSize = $baseFontSize * 0.8; // 80% of base font size
45+
}
46+
47+
public function getBarcodeRatio() {
48+
return 1.0; // Barcode should use 100% of available height
49+
}
50+
51+
/**
52+
* Calculate the required length for the content
53+
*
54+
* @param $record The record to calculate length for
55+
* @return float The calculated length in inches
56+
*/
57+
protected function calculateRequiredLength($record) {
58+
59+
// Calculate length needed for barcode and fields side by side
60+
$requiredLength = 0;
61+
62+
// Add barcode length if present
63+
if (($record->has('barcode2d') && $this->getSupport2DBarcode()) ||
64+
($record->has('barcode') && $this->getSupport1DBarcode())) {
65+
// Use full tape width for barcode size
66+
$barcodeSize = self::TAPE_WIDTH;
67+
$requiredLength += $barcodeSize + $this->barcodeMargin * 0.3; // Minimal margin
68+
}
69+
70+
// Add fields length if present - calculate based on actual content
71+
if ($record->has('fields') && $this->getSupportFields() > 0) {
72+
$fields = array_slice($record->get('fields')->toArray(), 0, $this->getSupportFields());
73+
74+
// Base width for field area
75+
$fieldsWidth = self::TAPE_WIDTH;
76+
77+
// Calculate additional width based on text length
78+
foreach ($fields as $field) {
79+
// Get label and value text
80+
$labelText = $field['label'] ?? '';
81+
$valueText = $field['value'] ?? '';
82+
83+
// Calculate approximate width needed based on text length
84+
// Increase character width to ensure enough space (0.15 inches per character)
85+
$labelWidth = strlen($labelText) * 0.09;
86+
$valueWidth = strlen($valueText) * 0.09;
87+
88+
// Use the longer of the two
89+
$textWidth = max($labelWidth, $valueWidth);
90+
91+
// Ensure minimum width and add to total
92+
$fieldsWidth = max($fieldsWidth, $textWidth);
93+
}
94+
95+
// Add the calculated width for fields
96+
$requiredLength += $fieldsWidth;
97+
98+
// Add minimal extra space for field padding
99+
// Reduce padding to eliminate extraneous space on right edge
100+
// $requiredLength += self::TAPE_WIDTH * 0.1;
101+
}
102+
103+
// Ensure minimum length
104+
return max($this->minHeight, $requiredLength);
105+
}
106+
107+
/**
108+
* Calculate text width accurately using the PDF object
109+
*
110+
* @param $pdf The PDF object
111+
* @param string $text The text to measure
112+
* @param string $font The font to use
113+
* @param string $style The font style
114+
* @param float $size The font size
115+
* @return float The calculated width
116+
*/
117+
protected function calculateTextWidth($pdf, $text, $font, $style, $size) {
118+
$originalFont = $pdf->getFontFamily();
119+
$originalStyle = $pdf->getFontStyle();
120+
$originalSize = $pdf->getFontSizePt();
121+
122+
$pdf->SetFont($font, $style, Helper::convertUnit($size, $this->getUnit(), 'pt', true));
123+
$width = $pdf->GetStringWidth($text);
124+
125+
// Restore original font settings
126+
$pdf->SetFont($originalFont, $originalStyle, $originalSize);
127+
128+
return $width;
129+
}
130+
131+
/**
132+
* Override the writeAll method to support dynamic page sizes for continuous tapes
133+
*/
134+
public function writeAll($pdf, $data) {
135+
// Use auto-sizing for continuous tapes, fixed height for die-cut tapes
136+
if ($this->continuous) {
137+
$data->each(function ($record, $index) use ($pdf) {
138+
// Calculate the required length by calling write with calculateOnly=true
139+
$requiredLength = $this->write($pdf, $record);
140+
141+
// If write didn't return a length (old implementation), fall back to calculateRequiredLength
142+
if ($requiredLength === null) {
143+
$requiredLength = $this->calculateRequiredLength($record);
144+
}
145+
146+
// Temporarily update the height property
147+
$originalHeight = $this->height;
148+
$this->height = self::TAPE_WIDTH; // Keep height fixed at tape width
149+
150+
// Add a new page with the calculated dimensions
151+
// Keep height fixed at TAPE_WIDTH, use calculated length for width
152+
$pdf->AddPage(
153+
$this->getOrientation(),
154+
[$requiredLength, self::TAPE_WIDTH],
155+
false, // Don't reset page number
156+
false // Don't reset object ID
157+
);
158+
159+
// Write the content
160+
$this->write($pdf, $record);
161+
162+
// Restore the original height
163+
$this->height = $originalHeight;
164+
});
165+
} else {
166+
// Use the default implementation for non-continuous (die-cut) tapes
167+
parent::writeAll($pdf, $data);
168+
}
169+
}
170+
}

0 commit comments

Comments
 (0)