Skip to content

Commit

Permalink
Add barcode support
Browse files Browse the repository at this point in the history
  • Loading branch information
rpungello committed Jul 14, 2022
1 parent bf32031 commit 44b6360
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 32 deletions.
34 changes: 34 additions & 0 deletions database/migrations/create_label_barcodes_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Rpungello\LaravelLabels\Models\Label;
use Rpungello\LaravelLabels\Models\LabelBarcode;

return new class () extends Migration {
public function up()
{
Schema::create('label_barcodes', function (Blueprint $table) {
$table->id();

$table->foreignIdFor(Label::class);
$table->unsignedDecimal('x_pos', 4, 1);
$table->unsignedDecimal('y_pos', 4, 1);

$table->unsignedDecimal('width', 4, 1)->nullable();
$table->unsignedDecimal('height', 4, 1)->nullable();

$table->unsignedTinyInteger('type')->default(LabelBarcode::TYPE_1D);
$table->string('symbology');
$table->string('content');

$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('label_barcodes');
}
};
3 changes: 2 additions & 1 deletion src/LabelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function configurePackage(Package $package): void
->name('laravel-label-printer')
->hasViews()
->hasMigration('create_labels_table')
->hasMigration('create_label_fields_table');
->hasMigration('create_label_fields_table')
->hasMigration('create_label_barcodes_table');

$this->app->singleton('laravel-label-printer', function () {
return new LabelPrinter();
Expand Down
5 changes: 5 additions & 0 deletions src/Models/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function fields(): Relation
return $this->hasMany(LabelField::class);
}

public function barcodes(): Relation
{
return $this->hasMany(LabelBarcode::class);
}

public function getEffectivePageWidth(): float
{
return $this->page_width - $this->horizontal_margin * 2;
Expand Down
34 changes: 34 additions & 0 deletions src/Models/LabelBarcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Rpungello\LaravelLabels\Models;

use Illuminate\Database\Eloquent\Relations\Relation;

class LabelBarcode extends \Illuminate\Database\Eloquent\Model
{
public const TYPE_1D = 0;
public const TYPE_2D = 1;

protected $fillable = [
'x_pos',
'y_pos',
'width',
'height',
'type',
'symbology',
'content',
];

protected $casts = [
'x_pos' => 'float',
'y_pos' => 'float',
'width' => 'float',
'height' => 'float',
'type' => 'int',
];

public function label(): Relation
{
return $this->belongsTo(Label::class);
}
}
99 changes: 68 additions & 31 deletions src/PdfDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Support\Arrayable;
use Rpungello\LaravelLabels\Models\Label;
use Rpungello\LaravelLabels\Models\LabelBarcode;
use Rpungello\LaravelLabels\Models\LabelField;
use Rpungello\LaravelStringTemplate\Facades\LaravelStringTemplate;
use Spatie\Color\Color;
Expand Down Expand Up @@ -42,6 +43,11 @@ public function addLabel(PrintsOnLabels $data): void
foreach ($this->template->fields as $field) {
$this->addLabelField($field, $data, $xPos, $yPos);
}

foreach ($this->template->barcodes as $barcode) {
$this->addLabelBarcode($barcode, $data, $xPos, $yPos);
}

$this->currentColumn++;
$this->checkPage();
}
Expand All @@ -54,6 +60,27 @@ private function getCurrentCoordinates(): array
];
}

private function addDebugRectangles(float $xPos, float $yPos)
{
$this->debuggingRectangle(
$xPos - $this->template->padding,
$yPos - $this->template->padding,
$this->template->label_width,
$this->template->label_height,
Hsl::fromString('hsl(0, 40%, 70%)')
);

$this->debuggingRectangle(
$xPos,
$yPos,
$this->template->label_width - $this->template->padding * 2,
$this->template->label_height - $this->template->padding * 2,
Hsl::fromString('hsl(120, 40%, 70%)')
);

$this->setDrawColor(0, 0, 0);
}

private function debuggingRectangle(float $xPos, float $yPos, float $width, float $height, Color $color)
{
if (config('app.debug')) {
Expand Down Expand Up @@ -82,6 +109,47 @@ private function addLabelField(LabelField $field, PrintsOnLabels $data, float $x
);
}

public function MultiCell($w, $h, $txt, $border = 0, $align = 'J', $fill = false, $ln = 1, $x = null, $y = null, $reseth = true, $stretch = 0, $ishtml = false, $autopadding = true, $maxh = 0, $valign = 'T', $fitcell = false): int|string
{
return parent::MultiCell($w, $h, $txt, $this->shouldDisplayBorder($border), $align, $fill, $ln, $x, $y, $reseth, $stretch, $ishtml, $autopadding, $maxh, $valign, $fitcell); // TODO: Change the autogenerated stub
}

private function shouldDisplayBorder(mixed $border): int
{
return $border || config('app.debug');
}

private function addLabelBarcode(LabelBarcode $barcode, PrintsOnLabels $data, float $xPos, float $yPos)
{
$barcodeXPos = $xPos + $barcode->x_pos;
$barcodeYPos = $yPos + $barcode->y_pos;

$maxWidth = $this->template->label_width - $this->template->padding * 2 - $barcode->x_pos;
$maxHeight = $this->template->label_height - $this->template->padding * 2 - $barcode->y_pos;

$formattedContent = LaravelStringTemplate::format($barcode->content, $data->getLabelData());

if ($barcode->type === LabelBarcode::TYPE_1D) {
$this->write1DBarcode(
$formattedContent,
$barcode->symbology,
$barcodeXPos,
$barcodeYPos,
is_null($barcode->width) ? $maxWidth : min($barcode->width, $maxWidth),
is_null($barcode->height) ? $maxHeight : min($barcode->height, $maxHeight),
);
} else {
$this->write2DBarcode(
$formattedContent,
$barcode->symbology,
$barcodeXPos,
$barcodeYPos,
is_null($barcode->width) ? $maxWidth : min($barcode->width, $maxWidth),
is_null($barcode->height) ? $maxHeight : min($barcode->height, $maxHeight),
);
}
}

private function checkPage(): void
{
if ($this->currentColumn === $this->numberOfColumns) {
Expand All @@ -107,35 +175,4 @@ public function Cell($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $
{
parent::Cell($w, $h, $txt, $this->shouldDisplayBorder($border), $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
}

private function shouldDisplayBorder(mixed $border): int
{
return $border || config('app.debug');
}

public function MultiCell($w, $h, $txt, $border = 0, $align = 'J', $fill = false, $ln = 1, $x = null, $y = null, $reseth = true, $stretch = 0, $ishtml = false, $autopadding = true, $maxh = 0, $valign = 'T', $fitcell = false): int|string
{
return parent::MultiCell($w, $h, $txt, $this->shouldDisplayBorder($border), $align, $fill, $ln, $x, $y, $reseth, $stretch, $ishtml, $autopadding, $maxh, $valign, $fitcell); // TODO: Change the autogenerated stub
}

private function addDebugRectangles(float $xPos, float $yPos)
{
$this->debuggingRectangle(
$xPos - $this->template->padding,
$yPos - $this->template->padding,
$this->template->label_width,
$this->template->label_height,
Hsl::fromString('hsl(0, 40%, 70%)')
);

$this->debuggingRectangle(
$xPos,
$yPos,
$this->template->label_width - $this->template->padding * 2,
$this->template->label_height - $this->template->padding * 2,
Hsl::fromString('hsl(120, 40%, 70%)')
);

$this->setDrawColor(0, 0, 0);
}
}

0 comments on commit 44b6360

Please sign in to comment.