Changed logic in sticker pdf service
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Gemini PR Review / Gemini PR Review (pull_request) Has been cancelled
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Has been cancelled
Laravel Larastan / larastan (pull_request) Has been cancelled
Laravel Pint / pint (pull_request) Has been cancelled

This commit is contained in:
dhanabalan
2026-04-27 14:08:17 +05:30
parent f66839131e
commit ab86517fc1

View File

@@ -2,11 +2,16 @@
namespace App\Services;
use App\Models\Item;
use App\Models\ItemCharacteristic;
use App\Models\ProductionQuantity;
use App\Models\StickerDetail;
use App\Models\StickerMappingMaster;
use App\Models\StickerStructureDetail;
use App\Models\StickerValidation;
use Illuminate\Http\Response;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use TCPDF;
@@ -447,11 +452,10 @@ class StickerPdfService
}
// return $pdf->Output('sticker.pdf', 'S');
$pdfContent = $pdf->Output('sticker.pdf', 'S');
$pdfContent = $pdf->Output('', 'S'); // 'S' returns string
return (new Response($pdfContent, 200))
->header('Content-Type', 'application/pdf')
->header('Content-Disposition', 'inline; filename="sticker.pdf"');
// Encode as base64
return base64_encode($pdfContent);
}
@@ -788,10 +792,11 @@ class StickerPdfService
// ->header('Content-Disposition', 'inline; filename="sticker.pdf"');
}
public function generatePdf1(string $stickerId, Collection $dynamicElements, ?ItemCharacteristic $itemCharacteristic, ?string $serialNumber)
public function generatePdf1(string $stickerId, Collection $dynamicElements, ?ItemCharacteristic $itemCharacteristic, ?string $serialNumber, $serNo)
{
$dynamicValueMap = [];
$itemCode = $itemCharacteristic?->item?->code ?? '';
foreach ($dynamicElements as $element) {
@@ -827,42 +832,29 @@ class StickerPdfService
$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,
(float) $structure->sticker_bmargin,
);
$pdf->SetAutoPageBreak(false, 0);
$pdf->AddPage();
if (!empty($serialNumber)) {
$pdf->SetFont('helvetica', 'B', 10);
$pdf->SetTextColor(0, 0, 0);
// 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
// // HARD-CODED POSITION (mm)
// $x = 40; // change as needed
// $y = 60; // change as needed
$pdf->Text($x, $y, (string) $serialNumber);
}
// $pdf->Text($x, $y, (string) $serialNumber);
// }
$pdf->SetFont('helvetica', 'B', 10);
@@ -900,15 +892,32 @@ class StickerPdfService
break;
case 'QR':
$pdf->write2DBarcode(
if (
($row->element_type) == 'Dynamic'
) {
$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;
'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':
@@ -1009,22 +1018,123 @@ class StickerPdfService
}
}
// return $pdf->Output('sticker.pdf', 'S');
$pdfContent = $pdf->Output('sticker1.pdf', 'S');
$filename = "sticker_{$stickerId}_" . time() . ".pdf";
// $pdfContent = $pdf->Output('', 'S'); // 'S' returns string
// 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');
// // Encode as base64
// return base64_encode($pdfContent);
// $pdfContent = $pdf->Output('', 'S');
// return response($pdfContent)
// ->header('Content-Type', 'application/pdf')
// ->header('Content-Disposition', 'inline; filename="sticker.pdf"');
$pdfContent = $pdf->Output('', 'S'); // 'S' returns the PDF as a string
// 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 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, '#');
@@ -1034,6 +1144,7 @@ class StickerPdfService
// hexdec(substr($hex, 4, 2)),
// ];
// }
private function hexToRgb($hex)
{
if (! is_string($hex) || ($hex = trim($hex)) === '') {