diff --git a/app/Http/Controllers/PdfController.php b/app/Http/Controllers/PdfController.php index a3bd0b7..03ac47e 100644 --- a/app/Http/Controllers/PdfController.php +++ b/app/Http/Controllers/PdfController.php @@ -913,6 +913,8 @@ class PdfController extends Controller $itemCharacteristicId = $request->query('item_characteristic_id'); $stickerId = trim($stickerId); + //dd($plantId,$itemCharacteristicId,$stickerId); + // Normalize "empty" or "" to null $plantId = ($plantId && $plantId != 'empty') ? $plantId : null; $itemCharacteristicId = ($itemCharacteristicId && $itemCharacteristicId != 'empty') ? $itemCharacteristicId : null; @@ -956,4 +958,91 @@ class PdfController extends Controller return $pdfService->generate($stickerId, $elements->toArray()); } } + + + // public function generatePdf(Request $request, $stickerId) + // { + // $plantId = $request->query('plant_id'); + // $itemCharacteristicId = $request->query('item_characteristic_id'); + // $stickerId = trim($stickerId); + + // //dd($plantId,$itemCharacteristicId,$stickerId); + + // // Normalize "empty" or "" to null + // $plantId = ($plantId && $plantId != 'empty') ? $plantId : null; + // $itemCharacteristicId = ($itemCharacteristicId && $itemCharacteristicId != 'empty') ? $itemCharacteristicId : null; + + + // if ($plantId && $itemCharacteristicId) + // { + // $plantId = $request->query('plant_id'); + // $itemCharacteristicId = $request->query('item_characteristic_id'); + + + // $sticId = StickerStructureDetail::where('id', $stickerId)->first(); + + // $sId = $sticId->id; + + // $stickerId = $sticId->sticker_id; + + // $dynamicElements = StickerDetail::where('sticker_structure_detail_id', $sId) + // ->where('element_type', 'Dynamic') + // ->get(); + + // $itemCharacteristic = null; + + // if ($plantId && $itemCharacteristicId) { + // $itemCharacteristic = ItemCharacteristic::where('plant_id', $plantId) + // ->where('id', $itemCharacteristicId) + // ->first(); + // } + + // $pdfService = new StickerPdfService(); + + // return $pdfService->generatePdf1( + // $stickerId, + // $dynamicElements, + // $itemCharacteristic + // ); + // } + // else + // { + // $elements = StickerStructureDetail::where('sticker_id', $stickerId) + // ->first(); + + // $pdfService = new StickerPdfService(); + // return $pdfService->generate($stickerId, $elements->toArray()); + // } + // } + + + public function generatePdf(Request $request, $stickerId) + { + $plantId = $request->query('plant_id'); + $itemCharacteristicId = $request->query('item_characteristic_id'); + $serialNumber = $request->query('serial_number'); + + if ($plantId && $itemCharacteristicId) { + $structure = StickerStructureDetail::findOrFail($stickerId); + + $dynamicElements = StickerDetail::where( + 'sticker_structure_detail_id', + $structure->id + )->where('element_type', 'Dynamic')->get(); + + $itemCharacteristic = ItemCharacteristic::where('plant_id', $plantId) + ->where('id', $itemCharacteristicId) + ->first(); + + return (new StickerPdfService())->generatePdf1( + $structure->sticker_id, + $dynamicElements, + $itemCharacteristic, + $serialNumber + ); + } + + return abort(404); + } + } diff --git a/app/Services/StickerPdfService.php b/app/Services/StickerPdfService.php index 825efae..94d82bc 100644 --- a/app/Services/StickerPdfService.php +++ b/app/Services/StickerPdfService.php @@ -788,6 +788,243 @@ class StickerPdfService // ->header('Content-Disposition', 'inline; filename="sticker.pdf"'); } + public function generatePdf1(string $stickerId, Collection $dynamicElements, ?ItemCharacteristic $itemCharacteristic, ?string $serialNumber) + { + + $dynamicValueMap = []; + + foreach ($dynamicElements as $element) { + + $column = $element->characteristics_type; + + $value = ''; + + if ( + $itemCharacteristic && + $column && + Schema::hasColumn('item_characteristics', $column) + ) { + $value = $itemCharacteristic->{$column}; + } + + $dynamicValueMap[$element->id] = [ + 'design_type' => $element->design_element_type, // Text / Image + 'value' => $value, + ]; + } + + $structure = StickerStructureDetail::where('sticker_id', $stickerId) + ->first(); + + $structureId = $structure->id; + $elements = StickerDetail::where( + 'sticker_structure_detail_id', + $structureId + )->get(); + + $width = (float) $structure->sticker_width; + $height = (float) $structure->sticker_height; + + $pdf = new TCPDF('P', 'mm', [$width, $height], true, 'UTF-8', false); + + // $pdf->SetMargins( + // (float) $structure->sticker_lmargin, + // (float) $structure->sticker_tmargin, + // (float) $structure->sticker_rmargin, + // ); + + // //$pdf->SetAutoPageBreak(false, (float) $structure->sticker_bmargin); + // $pdf->SetAutoPageBreak(false, (float) $structure->sticker_bmargin); + + $pdf->setPrintHeader(false); + $pdf->setPrintFooter(false); + // $pdf->setCellPaddings(0, 0, 0, 0); + // $pdf->setCellMargins(5, 5, 5, 5); + + // Set margins + // $pdf->SetMargins(5, 5, 5); // left, top, right + + $pdf->SetMargins( + (float) $structure->sticker_lmargin, + (float) $structure->sticker_tmargin, + (float) $structure->sticker_rmargin, + ); + $pdf->SetAutoPageBreak(false, 0); + + $pdf->AddPage(); + + if (!empty($serialNumber)) { + $pdf->SetFont('helvetica', 'B', 10); + $pdf->SetTextColor(0, 0, 0); + + // HARD-CODED POSITION (mm) + $x = 40; // change as needed + $y = 60; // change as needed + + $pdf->Text($x, $y, (string) $serialNumber); + } + + $pdf->SetFont('helvetica', 'B', 10); + + foreach ($elements as $row) { + + switch ($row->design_element_type) { + + case 'Text': + + $pdf->SetFont( + $row->string_font ?? 'helvetica', + '', + (int) ($row->string_size ?? 10) + ); + + $pdf->SetTextColor( + ...$this->hexToRgb($row->element_colour ?? '#000000') + ); + + $textValue = $row->string_value ?? ''; + + if ( + $row->element_type == 'Dynamic' && + isset($dynamicValueMap[$row->id]) + ) { + $textValue = $dynamicValueMap[$row->id]['value']; + } + + $pdf->Text( + (float) ($row->string_x_value ?? 0), + (float) ($row->string_y_value ?? 0), + (string) $textValue + ); + + break; + + case 'QR': + $pdf->write2DBarcode( + $row->qr_value ?? '', + 'QRCODE,H', + (float) ($row->qr_x_value ?? 0), + (float) ($row->qr_y_value ?? 0), + (float) ($row->qr_size ?? 10), + (float) ($row->qr_size ?? 10) + ); + break; + + case 'Image': + + $imagePath = null; + + if ( + $row->element_type == 'Dynamic' && + isset($dynamicValueMap[$row->id]) && + ! empty($dynamicValueMap[$row->id]['value']) + ) { + $imageName = strtolower($dynamicValueMap[$row->id]['value']).'.png'; + + $imagePath = public_path('images/'.ltrim($imageName, '/')); + } + + $pdf->Image( + $imagePath, + (float) ($row->image_x ?? 0), + (float) ($row->image_y ?? 0), + (float) ($row->image_width ?? 0), + (float) ($row->image_height ?? 0) + ); + break; + case 'Shape': + if ($row->shape_name == 'Line') { + $pdf->SetLineWidth((float) ($row->shape_pen_size ?? 0.3)); + + if (isset($row->element_colour)) { + $rgb = $this->hexToRgb($row->element_colour); + $pdf->SetDrawColor($rgb[0], $rgb[1], $rgb[2]); + } else { + $pdf->SetDrawColor(0, 0, 0); + } + + $pdf->Line( + (float) $row->shape_x1_value, + (float) $row->shape_y1_value, + (float) $row->shape_x2_value, + (float) $row->shape_y2_value + ); + } elseif ($row->shape_name == 'Rectangle') { + $pdf->SetLineWidth((float) ($row->shape_pen_size ?? 0.3)); + + if (isset($row->element_colour)) { + $rgb = $this->hexToRgb($row->element_colour); + $pdf->SetDrawColor($rgb[0], $rgb[1], $rgb[2]); + } else { + $pdf->SetDrawColor(0, 0, 0); + } + + $x1 = (float) $row->shape_x1_value; + $y1 = (float) $row->shape_y1_value; + $x2 = (float) $row->shape_x2_value; + $y2 = (float) $row->shape_y2_value; + + $x = min($x1, $x2); + $y = min($y1, $y2); + $width = abs($x2 - $x1); + $height = abs($y2 - $y1); + + $pdf->Rect($x, $y, $width, $height, 'D'); + } elseif ($row->shape_name == 'CurvedRectangle') { + $pdf->SetLineWidth((float) ($row->shape_pen_size ?? 0.3)); + + if (isset($row->element_colour)) { + $rgb = $this->hexToRgb($row->element_colour); + $pdf->SetDrawColor($rgb[0], $rgb[1], $rgb[2]); + } else { + $pdf->SetDrawColor(0, 0, 0); + } + + $x1 = (float) $row->shape_x1_value; + $y1 = (float) $row->shape_y1_value; + $x2 = (float) $row->shape_x2_value; + $y2 = (float) $row->shape_y2_value; + + $x = min($x1, $x2); + $y = min($y1, $y2); + $width = abs($x2 - $x1); + $height = abs($y2 - $y1); + + // radius in mm + // $radius = 3; + $radius = (float) $row->curve_radius; + + $pdf->RoundedRect( + $x, + $y, + $width, + $height, + $radius, + '1111', // ← round all 4 corners (default) + 'D' + ); + } + + break; + } + } + + // return $pdf->Output('sticker.pdf', 'S'); + $pdfContent = $pdf->Output('sticker1.pdf', 'S'); + $filename = "sticker_{$stickerId}_" . time() . ".pdf"; + + // return (new Response($pdfContent, 200)) + // ->header('Content-Type', 'application/pdf'); + // ->header('Content-Disposition', 'inline; filename="sticker.pdf"'); + return response($pdfContent, 200) + ->header('Content-Type', 'application/pdf') + ->header('Content-Disposition', 'inline; filename="'.$filename.'"') + ->header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') + ->header('Pragma', 'no-cache') + ->header('Expires', '0'); + + } + // private function hexToRgb($hex) // { // $hex = ltrim($hex, '#'); diff --git a/routes/web.php b/routes/web.php index 6d59d01..6a78fb5 100644 --- a/routes/web.php +++ b/routes/web.php @@ -31,6 +31,14 @@ use App\Http\Livewire\CustomLogin; [PdfController::class, 'generate'] )->name('stickers.pdf'); + Route::get( + '/stickers/{stickerId}/pdf/{plantId?}/{itemCharacteristicId?}', + [PdfController::class, 'generatePdf'] + )->name('stickers1.pdf'); + + // Route::get('/stickers/pdf', [PdfController::class, 'generatePdf'])->name('stickers1.pdf'); + + Route::get('/part-validation-image/{plant}/{filename}', function ($plant, $filename) { $path = storage_path("app/private/uploads/PartValidation/{$plant}/{$filename}");