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->setPrintHeader(false); $pdf->setPrintFooter(false); $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': if (str_contains($row->string_value, '₹')) { $pdf->SetFont( 'dejavusans', '', (int) ($row->string_size ?? 10) ); } else { $pdf->SetFont( $row->string_font ?? 'helvetica', '', (int) ($row->string_size ?? 10) ); } $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; } } $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('', 'S'); // 'S' returns string // Encode as base64 return base64_encode($pdfContent); } public function generate(string $stickerId, array $elements) { $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->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"'); } public function generateSticker(string $stickerId, array $elements) { $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->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('dejavusans', 'B', 10); // font, style, size in points foreach ($elements as $row) { switch ($row->design_element_type) { case 'Text': // $fontName = trim($row->string_font ?? 'helvetica'); // Built-in TCPDF fonts $font = $row->string_font ?? 'dejavusans'; // Built-in fonts $builtInFonts = [ 'helvetica', 'Times-Bold', 'courier', ]; if (!in_array(strtolower($font), $builtInFonts)) { $ttfPath = storage_path('app/fonts/' . $font . '.ttf'); if (file_exists($ttfPath)) { $font = TCPDF_FONTS::addTTFfont( $ttfPath, 'TrueTypeUnicode', '', 32 ); } else { $font = 'helvetica'; } } // $pdf->SetFont( // $font, // '', // (int) ($row->string_size ?? 10) // ); if (str_contains($row->string_value, '₹')) { $pdf->SetFont( 'dejavusans', '', (int) ($row->string_size ?? 10) ); } else { $pdf->SetFont( $font, '', (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; } } $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 generatePdf1(string $stickerId, Collection $dynamicElements, ?ItemCharacteristic $itemCharacteristic, $serNo, $itemCode, $plantId) { $dynamicValueMap = []; $itemCode = $itemCharacteristic?->item?->code ?? ''; $plantName = $itemCharacteristic?->plant?->name ?? ''; $plantAddress = $itemCharacteristic?->plant?->address ?? ''; 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, '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->setPrintHeader(false); $pdf->setPrintFooter(false); $pdf->SetMargins( (float) $structure->sticker_lmargin, (float) $structure->sticker_tmargin, (float) $structure->sticker_rmargin, (float) $structure->sticker_bmargin, ); $pdf->SetAutoPageBreak(false, 0); $pdf->AddPage(); $pdf->SetFont('helvetica', 'B', 10); foreach ($elements as $row) { switch ($row->design_element_type) { case 'Text': if (str_contains($row->string_value, '₹')) { $pdf->SetFont( 'dejavusans', '', (int) ($row->string_size ?? 10) ); } else { $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]) && $row->characteristics_type != 'item_code' && $row->characteristics_type != 'serial_number' ) { $textValue = $dynamicValueMap[$row->id]['value']; } if ($row->characteristics_type == 'item_code') { $textValue = $itemCode ?? ''; } if ($row->characteristics_type == 'serial_number') { $textValue = $serNo ?? ''; } if ($row->characteristics_type == 'plant_name') { $textValue = $plantName. ')' ?? ''; } if ($row->characteristics_type == 'plant_address') { $textValue = $plantAddress ?? ''; } if ($row->characteristics_type == 'current_date') { $textValue = date('M-y'); } $pdf->Text( (float) ($row->string_x_value ?? 0), (float) ($row->string_y_value ?? 0), (string) $textValue ); break; case 'QR': if ( ($row->element_type) == 'Dynamic' ) { // $qrContent = $serNo ?? ''; $qrContent = $itemCode . '|' . $serNo; $pdf->write2DBarcode( $qrContent, '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; } else{ $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; } } $pdfContent = $pdf->Output('', 'S'); // Return the PDF as a response try { $pdfContent = $pdf->Output('', 'S'); // 'S' returns the PDF as a string return response($pdfContent) ->header('Content-Type', 'application/pdf') ->header('Content-Disposition', 'inline; filename="sticker.pdf"'); } catch (\Exception $e) { Log::error('PDF generation failed: '.$e->getMessage()); abort(500, 'Failed to generate PDF'); } } public function generateTemplate(string $stickerId,Collection $dynamicElements,?ItemCharacteristic $itemCharacteristic,$itemCode,$plantId) { $dynamicValueMap = []; $itemCode = $itemCharacteristic?->item?->code ?? $itemCode ?? ''; $plantName = $itemCharacteristic?->plant?->name ?? ''; $plantAddress = $itemCharacteristic?->plant?->address ?? ''; foreach ($dynamicElements as $element) { $column = $element->characteristics_type; $value = ''; if ($itemCharacteristic && $column && Schema::hasColumn('item_characteristics', $column)) { $value = $itemCharacteristic->{$column}; } $dynamicValueMap[$element->id] = $value; } $structure = StickerStructureDetail::where('sticker_id',$stickerId)->firstOrFail(); $elements = StickerDetail::where('sticker_structure_detail_id',$structure->id)->get(); $templateElements = []; foreach ($elements as $row) { if ($row->design_element_type == 'Text') { $textValue = $row->string_value ?? ''; if ($row->element_type == 'Dynamic' && isset($dynamicValueMap[$row->id]) && !in_array($row->characteristics_type, [ 'item_code', 'serial_number', 'plant_name', 'plant_address', 'current_date', ]) ) { $textValue = $dynamicValueMap[$row->id]; } if ($row->characteristics_type == 'item_code') { $textValue = $itemCode; } if ($row->characteristics_type == 'serial_number') { $textValue = '__SERIAL_NUMBER__'; } if ($row->characteristics_type === 'plant_name') { $textValue = $plantName; } if ($row->characteristics_type === 'plant_address') { $textValue = $plantAddress; } if ($row->characteristics_type === 'current_date') { $textValue = date('M-y'); } $font = $row->string_font ?? 'helvetica'; if (str_contains($row->string_value ?? '', '₹')) { $font = 'dejavusans'; } $templateElements[] = [ 'type' => 'text', 'value' => $textValue, 'x' => (float) ($row->string_x_value ?? 0), 'y' => (float) ($row->string_y_value ?? 0), 'font' => $font, 'size' => (int) ($row->string_size ?? 10), 'color' => $row->element_colour ?? '#000000', ]; } elseif ($row->design_element_type == 'QR') { if ($row->element_type == 'Dynamic') { $qrValue = '__DYNAMIC_QR__'; } else { $qrValue = $row->qr_value ?? ''; } $templateElements[] = [ 'type' => 'qr', 'value' => $qrValue, 'x' => (float) ($row->qr_x_value ?? 0), 'y' => (float) ($row->qr_y_value ?? 0), 'size' => (float) ($row->qr_size ?? 10), ]; } elseif ($row->design_element_type == 'Image') { $imagePath = null; if ($row->element_type == 'Dynamic' && isset($dynamicValueMap[$row->id]) && !empty($dynamicValueMap[$row->id])) { $imageName = strtolower($dynamicValueMap[$row->id]) . '.png'; $imagePath = public_path('images/' . ltrim($imageName, '/')); } $templateElements[] = [ 'type' => 'image', 'path' => $imagePath, 'x' => (float) ($row->image_x ?? 0), 'y' => (float) ($row->image_y ?? 0), 'width' => (float) ($row->image_width ?? 0), 'height' => (float) ($row->image_height ?? 0), ]; } elseif ($row->design_element_type == 'Shape') { $templateElements[] = [ 'type' => 'shape', 'name' => $row->shape_name, 'pen_size' => (float) ($row->shape_pen_size ?? ''), 'color' => $row->element_colour ?? '#000000', 'x1' => (float) ($row->shape_x1_value ?? 0), 'y1' => (float) ($row->shape_y1_value ?? 0), 'x2' => (float) ($row->shape_x2_value ?? 0), 'y2' => (float) ($row->shape_y2_value ?? 0), 'radius' => (float) ($row->curve_radius ?? ''), ]; } } // Return prepared template. No PDF generation here. return [ 'width' => (float) $structure->sticker_width, 'height' => (float) $structure->sticker_height, 'lmargin' => (float) $structure->sticker_lmargin, 'tmargin' => (float) $structure->sticker_tmargin, 'rmargin' => (float) $structure->sticker_rmargin, 'bmargin' => (float) $structure->sticker_bmargin, 'itemCode' => $itemCode, 'elements' => $templateElements, ]; } public function generateBulkFromTemplate(array $template,$fromSerial,$toSerial,string $itemCode) { $stickerWidth = (float) $template['width']; $stickerHeight = (float) $template['height']; $gap = 5.0; $count = ($toSerial - $fromSerial) + 1; $totalHeight = ($stickerHeight * $count) + ($gap * ($count - 1)); $pdf = new TCPDF( 'P', 'mm', [ $stickerWidth, $totalHeight ], true, 'UTF-8', false ); $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); $pdf->SetMargins( 0, 0, 0, 0 ); $pdf->SetAutoPageBreak(false, 0); $pdf->AddPage(); $currentY = 0; // Generate every serial for ($serial = $fromSerial; $serial <= $toSerial; $serial++) { $qrContent = $itemCode . '|' . $serial; foreach ($template['elements'] as $element) { if ($element['type'] == 'text') { $textValue = $element['value']; if ($textValue == '__SERIAL_NUMBER__') { $textValue = (string) $serial; } $pdf->SetFont($element['font'],'',$element['size']); $rgb = $this->hexToRgb($element['color']); $pdf->SetTextColor( $rgb[0], $rgb[1], $rgb[2] ); $pdf->Text( $element['x'], $element['y'] + $currentY, (string) $textValue ); } elseif ($element['type'] == 'qr') { $qrValue = $element['value']; if ($qrValue == '__DYNAMIC_QR__') { $qrValue = $qrContent; } $pdf->write2DBarcode( $qrValue, 'QRCODE,H', $element['x'], $element['y'] + $currentY, $element['size'], $element['size'] ); } elseif ($element['type'] == 'image') { if (!empty($element['path']) && file_exists($element['path'])) { $pdf->Image( $element['path'], $element['x'], $element['y'] + $currentY, $element['width'], $element['height'] ); } } elseif ($element['type'] == 'shape') { $rgb = $this->hexToRgb( $element['color'] ); $pdf->SetDrawColor( $rgb[0], $rgb[1], $rgb[2] ); $pdf->SetLineWidth( $element['pen_size'] ); if ($element['name'] == 'Line') { $pdf->Line( $element['x1'], $element['y1'] + $currentY, $element['x2'], $element['y2'] + $currentY ); } elseif ($element['name'] == 'Rectangle') { $x = min($element['x1'],$element['x2']); $y = min($element['y1'],$element['y2']); $width = abs($element['x2'] - $element['x1']); $height = abs($element['y2'] - $element['y1']); $pdf->Rect( $x, $y + $currentY, $width, $height, 'D' ); } elseif ($element['name'] == 'CurvedRectangle') { $x = min($element['x1'],$element['x2']); $y = min($element['y1'],$element['y2']); $width = abs($element['x2'] - $element['x1']); $height = abs($element['y2'] - $element['y1']); $pdf->RoundedRect( $x, $y + $currentY, $width, $height, $element['radius'], '1111', 'D' ); } } } $currentY += $stickerHeight + $gap; } return $pdf->Output('', 'S'); } public function generatePdfBySerial($item, $serNo, $plantId, $refNumber) { $recFound = ProductionQuantity::where('plant_id', $plantId) ->where('production_order', $refNumber) ->where('serial_number', $serNo) ->first(); if (!$recFound) { abort(404, 'Serial not found'); } $duplicate = StickerValidation::where('plant_id', $plantId) ->where('production_order', $refNumber) ->where('serial_number', $serNo) ->first(); $itemC = Item::where('code', $item) ->where('plant_id', $plantId) ->first(); if (!$itemC) { abort(404, 'Item not found'); } $item = ItemCharacteristic::where('item_id', $itemC->id) ->where('plant_id', $plantId) ->first(); if (!$item) { abort(404, 'Item characteristic not found'); } $mapping = StickerMappingMaster::where('plant_id', $plantId) ->where('item_characteristic_id', $item->id) ->first(); if (!$mapping) { abort(404, 'Sticker mapping not found'); } $structure = StickerStructureDetail::findOrFail($mapping->sticker_structure1_id); $dynamicElements = StickerDetail::where( 'sticker_structure_detail_id', $structure->id )->where('element_type', 'Dynamic')->get(); return $this->generatePdf1( $structure->sticker_id, $dynamicElements, $item, $serNo, $serNo ); // return response($pdf) // ->header('Content-Type', 'application/pdf') // ->header('Content-Disposition', 'inline; filename="sticker.pdf"'); } // public function printStickersToUSB(array $stickers, int $plantId, ?string $serialNumber) // { // $printerPort = 'USB001'; // foreach ($stickers as $sticker) { // $dynamicElements = StickerDetail::where('sticker_structure_detail_id', $sticker['sticker_id']) // ->get(); // $itemCharacteristic = ItemCharacteristic::find( // $sticker['item_characteristic'] // ); // $pdfContent = $this->generatePdf1( // $sticker['sticker_id'], // $dynamicElements, // $itemCharacteristic, // $serialNumber // ); // $handle = fopen("{$printerPort}:", "wb"); // if (! $handle) { // throw new \Exception("Cannot open printer port {$printerPort}"); // } // fwrite($handle, $pdfContent); // fclose($handle); // } // } // private function hexToRgb($hex) // { // $hex = ltrim($hex, '#'); // return [ // hexdec(substr($hex, 0, 2)), // hexdec(substr($hex, 2, 2)), // hexdec(substr($hex, 4, 2)), // ]; // } private function hexToRgb($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)) { return [0, 0, 0]; } return [ hexdec(substr($hex, 0, 2)), // integer hexdec(substr($hex, 2, 2)), // integer hexdec(substr($hex, 4, 2)), // integer ]; } }