From 0c7e55595ab1248c1c5e01c0213940972028bf69 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 23 Dec 2025 17:33:48 +0530 Subject: [PATCH] Added show preview method in sticker pdf service --- app/Services/StickerPdfService.php | 468 ++++++++++++++++++++++++++--- 1 file changed, 425 insertions(+), 43 deletions(-) diff --git a/app/Services/StickerPdfService.php b/app/Services/StickerPdfService.php index 504e158..b4fdbcb 100644 --- a/app/Services/StickerPdfService.php +++ b/app/Services/StickerPdfService.php @@ -5,15 +5,14 @@ namespace App\Services; use App\Models\ItemCharacteristic; use App\Models\StickerDetail; use App\Models\StickerStructureDetail; -use Illuminate\Support\Collection; -use TCPDF; use Illuminate\Http\Response; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Schema; +use TCPDF; class StickerPdfService { - - public function generate1(string $stickerId, Collection $dynamicElements, ?ItemCharacteristic $itemCharacteristic) + public function generateStickerItem(string $stickerId, Collection $dynamicElements, ?ItemCharacteristic $itemCharacteristic) { $dynamicValueMap = []; @@ -38,7 +37,6 @@ class StickerPdfService ]; } - $structure = StickerStructureDetail::where('sticker_id', $stickerId) ->first(); @@ -48,7 +46,7 @@ class StickerPdfService $structureId )->get(); - $width = (float) $structure->sticker_width; + $width = (float) $structure->sticker_width; $height = (float) $structure->sticker_height; $pdf = new TCPDF('P', 'mm', [$width, $height], true, 'UTF-8', false); @@ -65,10 +63,10 @@ class StickerPdfService $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); // $pdf->setCellPaddings(0, 0, 0, 0); - // $pdf->setCellMargins(5, 5, 5, 5); + // $pdf->setCellMargins(5, 5, 5, 5); // Set margins - //$pdf->SetMargins(5, 5, 5); // left, top, right + // $pdf->SetMargins(5, 5, 5); // left, top, right $pdf->SetMargins( (float) $structure->sticker_lmargin, @@ -81,7 +79,6 @@ class StickerPdfService $pdf->SetFont('helvetica', 'B', 10); - foreach ($elements as $row) { switch ($row->design_element_type) { @@ -113,7 +110,7 @@ class StickerPdfService (string) $textValue ); - break; + break; case 'QR': $pdf->write2DBarcode( @@ -133,15 +130,13 @@ class StickerPdfService if ( $row->element_type == 'Dynamic' && isset($dynamicValueMap[$row->id]) && - !empty($dynamicValueMap[$row->id]['value']) - ) - { - $imageName = strtolower($dynamicValueMap[$row->id]['value']) . '.png'; + ! empty($dynamicValueMap[$row->id]['value']) + ) { + $imageName = strtolower($dynamicValueMap[$row->id]['value']).'.png'; - $imagePath = public_path('images/' . ltrim($imageName, '/')); + $imagePath = public_path('images/'.ltrim($imageName, '/')); } - $pdf->Image( $imagePath, (float) ($row->image_x ?? 0), @@ -184,12 +179,11 @@ class StickerPdfService $x = min($x1, $x2); $y = min($y1, $y2); - $width = abs($x2 - $x1); + $width = abs($x2 - $x1); $height = abs($y2 - $y1); $pdf->Rect($x, $y, $width, $height, 'D'); - } - elseif ($row->shape_name == 'CurvedRectangle') { + } elseif ($row->shape_name == 'CurvedRectangle') { $pdf->SetLineWidth((float) ($row->shape_pen_size ?? 0.3)); if (isset($row->element_colour)) { @@ -206,7 +200,7 @@ class StickerPdfService $x = min($x1, $x2); $y = min($y1, $y2); - $width = abs($x2 - $x1); + $width = abs($x2 - $x1); $height = abs($y2 - $y1); // radius in mm @@ -228,11 +222,236 @@ class StickerPdfService } } - //return $pdf->Output('sticker.pdf', 'S'); - $pdfContent = $pdf->Output('sticker.pdf', 'S'); - return (new Response($pdfContent, 200)) - ->header('Content-Type', 'application/pdf') - ->header('Content-Disposition', 'inline; filename="sticker.pdf"'); + $pdfContent = $pdf->Output('', 'S'); // 'S' returns string + + // Encode as base64 + return base64_encode($pdfContent); + + // // return $pdf->Output('sticker.pdf', 'S'); + // $pdfContent = $pdf->Output('sticker.pdf', 'S'); + + // return (new Response($pdfContent, 200)) + // ->header('Content-Type', 'application/pdf') + // ->header('Content-Disposition', 'inline; filename="sticker.pdf"'); + + } + + public function generate1(string $stickerId, Collection $dynamicElements, ?ItemCharacteristic $itemCharacteristic) + { + + $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(); + + $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('sticker.pdf', 'S'); + + return (new Response($pdfContent, 200)) + ->header('Content-Type', 'application/pdf') + ->header('Content-Disposition', 'inline; filename="sticker.pdf"'); } @@ -252,7 +471,7 @@ class StickerPdfService $structureId )->get(); - $width = (float) $structure->sticker_width; + $width = (float) $structure->sticker_width; $height = (float) $structure->sticker_height; $pdf = new TCPDF('P', 'mm', [$width, $height], true, 'UTF-8', false); @@ -282,11 +501,10 @@ class StickerPdfService (float) $structure->sticker_bmargin, ); - //$pdf->SetAutoPageBreak(false, (float) $structure->sticker_bmargin); - //$pdf->SetAutoPageBreak(false, (float) $structure->sticker_bmargin); + // $pdf->SetAutoPageBreak(false, (float) $structure->sticker_bmargin); + // $pdf->SetAutoPageBreak(false, (float) $structure->sticker_bmargin); - - //dd($pdf->getMargins()); + // dd($pdf->getMargins()); $pdf->AddPage(); @@ -300,7 +518,6 @@ class StickerPdfService // $pdf->Text(45, 45, 'Batch: X1'); // $pdf->Text(3, 20, '✓ Valid'); - foreach ($elements as $row) { switch ($row->design_element_type) { @@ -335,10 +552,10 @@ class StickerPdfService break; } - $imagePath = storage_path('app/public/' . ltrim($row->image_path, '/')); + $imagePath = storage_path('app/public/'.ltrim($row->image_path, '/')); // Optional: check if file actually exists (avoid TCPDF crash) - if (!file_exists($imagePath)) { + if (! file_exists($imagePath)) { // Log or skip — or use a placeholder break; } @@ -385,7 +602,7 @@ class StickerPdfService $x = min($x1, $x2); $y = min($y1, $y2); - $width = abs($x2 - $x1); + $width = abs($x2 - $x1); $height = abs($y2 - $y1); $pdf->Rect($x, $y, $width, $height, 'D'); @@ -394,11 +611,176 @@ class StickerPdfService } } - //return $pdf->Output('sticker.pdf', 'S'); - $pdfContent = $pdf->Output('sticker.pdf', 'S'); - return (new Response($pdfContent, 200)) - ->header('Content-Type', 'application/pdf') - ->header('Content-Disposition', 'inline; filename="sticker.pdf"'); + // return $pdf->Output('sticker.pdf', 'S'); + $pdfContent = $pdf->Output('sticker.pdf', 'S'); + + return (new Response($pdfContent, 200)) + ->header('Content-Type', 'application/pdf') + ->header('Content-Disposition', 'inline; filename="sticker.pdf"'); + } + + public function generateSticker(string $stickerId, array $elements) + { + $structure = StickerStructureDetail::where('sticker_id', $stickerId) + ->first(); + + // $stickerId = $structure->sticker_id; + + // $elements = StickerDetail::where('sticker_structure_detail_id', $stickerId)->get(); + + // dd($elements); + $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->SetAutoPageBreak(false, 0); // bottom = 0 + + $pdf->SetMargins( + (float) $structure->sticker_lmargin, + (float) $structure->sticker_tmargin, + (float) $structure->sticker_rmargin, + (float) $structure->sticker_bmargin, + ); + + // $pdf->SetAutoPageBreak(false, (float) $structure->sticker_bmargin); + // $pdf->SetAutoPageBreak(false, (float) $structure->sticker_bmargin); + + // dd($pdf->getMargins()); + + $pdf->AddPage(); + + $pdf->SetFont('helvetica', 'B', 10); // font, style, size in points + + // // 📍 ADD TEXT: 3mm from left, 10mm from top + // $pdf->Text(3, 10, 'Sticker ID: ' . $stickerId); + + // // Optional: add more text + // $pdf->SetFont('helvetica', '', 8); + // $pdf->Text(45, 45, 'Batch: X1'); + // $pdf->Text(3, 20, '✓ Valid'); + + 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')); + $pdf->Text( + (float) ($row->string_x_value ?? 0), + (float) ($row->string_y_value ?? 0), + $row->string_value ?? '' + ); + 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': + if (empty($row->image_path)) { + break; + } + + $imagePath = storage_path('app/public/'.ltrim($row->image_path, '/')); + + // Optional: check if file actually exists (avoid TCPDF crash) + if (! file_exists($imagePath)) { + // Log or skip — or use a placeholder + break; + } + + $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'); + } + break; + } + } + + // return $pdf->Output('sticker.pdf', 'S'); + $pdfContent = $pdf->Output('sticker.pdf', 'S'); + + return (new Response($pdfContent, 200)) + ->header('Content-Type', 'application/pdf') + ->header('Content-Disposition', 'inline; filename="sticker.pdf"'); } // private function hexToRgb($hex) @@ -412,21 +794,21 @@ class StickerPdfService // } private function hexToRgb($hex) { - if (!is_string($hex) || ($hex = trim($hex)) === '') { + if (! is_string($hex) || ($hex = trim($hex)) === '') { return [0, 0, 0]; } $hex = ltrim($hex, '#'); if (strlen($hex) == 3) { $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2]; } - if (strlen($hex) !== 6 || !ctype_xdigit($hex)) { + if (strlen($hex) !== 6 || ! ctype_xdigit($hex)) { return [0, 0, 0]; } + return [ hexdec(substr($hex, 0, 2)), // integer hexdec(substr($hex, 2, 2)), // integer - hexdec(substr($hex, 4, 2)) // integer + hexdec(substr($hex, 4, 2)), // integer ]; } - } -- 2.49.1