added single pdf logic in ocr
This commit is contained in:
@@ -35,23 +35,23 @@ class OcrValidationResource extends Resource
|
||||
public $serialNumbers = [];
|
||||
|
||||
|
||||
protected $listeners = [
|
||||
'set-serial-numbers' => 'fillSerialNumbers'
|
||||
];
|
||||
// protected $listeners = [
|
||||
// 'set-serial-numbers' => 'fillSerialNumbers'
|
||||
// ];
|
||||
|
||||
use InteractsWithForms;
|
||||
|
||||
public function fillSerialNumbers($event)
|
||||
{
|
||||
$serialNumbers = $event['serialNumbers'] ?? [];
|
||||
// public function fillSerialNumbers($event)
|
||||
// {
|
||||
// $serialNumbers = $event['serialNumbers'] ?? [];
|
||||
|
||||
$this->form->fill([
|
||||
'serial_1' => $serialNumbers[0] ?? null,
|
||||
'serial_2' => $serialNumbers[1] ?? null,
|
||||
'serial_3' => $serialNumbers[2] ?? null,
|
||||
'serial_4' => $serialNumbers[3] ?? null,
|
||||
]);
|
||||
}
|
||||
// $this->form->fill([
|
||||
// 'serial_1' => $serialNumbers[0] ?? null,
|
||||
// 'serial_2' => $serialNumbers[1] ?? null,
|
||||
// 'serial_3' => $serialNumbers[2] ?? null,
|
||||
// 'serial_4' => $serialNumbers[3] ?? null,
|
||||
// ]);
|
||||
// }
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
@@ -256,27 +256,9 @@ class OcrValidationResource extends Resource
|
||||
->label('Upload OCR')
|
||||
->action(function ($get, callable $set) {
|
||||
|
||||
// $serialNumbers = [
|
||||
// $get('serial_1'),
|
||||
// $get('serial_2'),
|
||||
// $get('serial_3'),
|
||||
// $get('serial_4'),
|
||||
// ];
|
||||
|
||||
// $hasSerial = collect($serialNumbers)->some(fn($s) => !empty($s));
|
||||
// $serialNumbersJson = $get('serial_numbers');
|
||||
// $serialNumbers = json_decode($serialNumbersJson, true) ?? [];
|
||||
// $serials = session('serial_numbers', []);
|
||||
$serials = session('serial_numbers');
|
||||
|
||||
// if (empty($serials)) {
|
||||
// Filament::notify('danger', 'No serial numbers found!');
|
||||
// return;
|
||||
// }
|
||||
|
||||
// ✅ Store into local Filament state (optional)
|
||||
$set('serial_numbers', $serials);
|
||||
//dd($serials);
|
||||
|
||||
if (empty($serials)) {
|
||||
Notification::make()
|
||||
@@ -286,9 +268,69 @@ class OcrValidationResource extends Resource
|
||||
return;
|
||||
}
|
||||
|
||||
// Now you can proceed with your PDF logic
|
||||
//$serialNumbers = array_slice($serialNumbers, 0, 4);
|
||||
|
||||
if (count($serials) == 1)
|
||||
{
|
||||
$serialNumbers = $serials;
|
||||
|
||||
$templatePath = storage_path('app/private/uploads/StickerTemplateOcr/Single.pdf');
|
||||
$outputPath = storage_path('app/private/uploads/StickerTemplateOcr/single_filled.pdf');
|
||||
|
||||
$pdf = new Fpdi('P', 'mm', [90, 90]);
|
||||
|
||||
$templateId = $pdf->setSourceFile($templatePath);
|
||||
$templatePage = $pdf->importPage(1);
|
||||
|
||||
$pdf->AddPage();
|
||||
$pdf->useTemplate($templatePage, 0, 0, 90, 90);
|
||||
|
||||
$pdf->SetFont('Helvetica', '', 10);
|
||||
$pdf->SetTextColor(0, 0, 0);
|
||||
|
||||
$slots = [
|
||||
['x' => 5.7, 'y' => 41.9, 'w' => 46.5, 'h' => 3.5], // 1st serial
|
||||
];
|
||||
|
||||
$qrSlots = [
|
||||
['x' => 17.3, 'y' => 29.2, 'size' => 11.4],
|
||||
];
|
||||
|
||||
foreach ($serialNumbers as $i => $serial) {
|
||||
if (!isset($slots[$i]) || !isset($qrSlots[$i])) continue;
|
||||
|
||||
// Erase old QR completely (slightly larger)
|
||||
$pdf->SetFillColor(255, 255, 255);
|
||||
$pdf->Rect($qrSlots[$i]['x']-1, $qrSlots[$i]['y']-1, $qrSlots[$i]['size']+2, $qrSlots[$i]['size']+2, 'F');
|
||||
|
||||
// Generate new QR code
|
||||
$qrPath = storage_path("app/private/uploads/QR/qr_$serial.png");
|
||||
$qrDir = storage_path('app/private/uploads/QR');
|
||||
if (!file_exists($qrDir)) mkdir($qrDir, 0777, true);
|
||||
//QrCode::format('svg')->size(100)->generate($serial, $qrPath);
|
||||
QrCode::format('png')->size(300)->errorCorrection('H')->generate($serial, $qrPath);
|
||||
|
||||
// Place QR code
|
||||
$pdf->Image($qrPath, $qrSlots[$i]['x'], $qrSlots[$i]['y'], $qrSlots[$i]['size'], $qrSlots[$i]['size']);
|
||||
|
||||
// Erase old serial
|
||||
$pdf->SetFillColor(255, 255, 255);
|
||||
$pdf->Rect($slots[$i]['x'], $slots[$i]['y'], $slots[$i]['w'], $slots[$i]['h'], 'F');
|
||||
|
||||
// Write new serial
|
||||
$pdf->SetXY($slots[$i]['x'], $slots[$i]['y']);
|
||||
$pdf->Cell($slots[$i]['w'], $slots[$i]['h'], $serial, 0, 0, 'L');
|
||||
}
|
||||
|
||||
// Save the final PDF
|
||||
$pdf->Output('F', $outputPath);
|
||||
|
||||
// Download
|
||||
return response()->download($outputPath);
|
||||
}
|
||||
if(count($serials) == 4)
|
||||
{
|
||||
|
||||
$serialNumbers = array_slice($serials, 0, 4);
|
||||
|
||||
//dd($serialNumbers);
|
||||
@@ -367,7 +409,16 @@ class OcrValidationResource extends Resource
|
||||
|
||||
// Download
|
||||
return response()->download($outputPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
Notification::make()
|
||||
->title('Please capture either 1 or 4 serial numbers.')
|
||||
->danger()
|
||||
->send();
|
||||
return;
|
||||
|
||||
}
|
||||
}),
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user