path('ETN-BOL2.pdf'); $newFile = 'BOL_' . ($data['bol_number'] ?? 'unknown') . '.pdf'; $outputPath ??= Storage::disk('public')->path($newFile); $pdf = new Fpdi; $pdf->setSourceFile($templatePath); $this->renderPage($pdf, $data, 1, true); $this->renderPage($pdf, $data, 2, false); $pdf->Output('F', $outputPath); return $outputPath; } private function renderPage(Fpdi $pdf, array $data, int $pageNumber, bool $includeLineDetails): void { $template = $pdf->importPage($pageNumber); $pdf->AddPage(); $pdf->useTemplate($template); $pdf->SetFont('Helvetica', '', 10); $pdf->SetTextColor(0, 0, 0); foreach ($this->headerFields() as $field => [$x, $y]) { $this->writeField($pdf, $data, $field, $x, $y); } if ($includeLineDetails) { foreach ($this->lineFields() as $field => [$x, $y]) { $this->writeField($pdf, $data, $field, $x, $y); } $this->writeDescription($pdf, $data); } } private function writeField(Fpdi $pdf, array $data, string $field, float $x, float $y): void { if (! array_key_exists($field, $data) || $data[$field] === null || $data[$field] === '') { return; } $pdf->SetXY($x, $y); $pdf->Write(0, (string) $data[$field]); } private function writeDescription(Fpdi $pdf, array $data): void { $description = $data['description'] ?? null; if ($description === null || $description === '') { return; } $pdf->SetXY(28, 140); $pdf->MultiCell(67, 4.5, (string) $description, 0, 'L'); } private function headerFields(): array { return [ 'bol_number' => [126, 44], 'date' => [171, 44], 'shipper_no' => [126, 53], 'master_bill_no' => [172, 54], 'master_bill_yes' => [183, 54], 'master_bill_drops' => [194, 55], 'consignor_name' => [21, 44], 'consignor_address1' => [21, 48], 'consignor_address2' => [21, 52], 'consignor_address3' => [21, 56], 'consignee_name' => [21, 72], 'consignee_address1' => [21, 76], 'consignee_address2' => [21, 80], 'consignee_address3' => [21, 84], 'freight_collect' => [127, 90], 'carrier_name' => [10, 100], ]; } private function lineFields(): array { return [ 'pieces' => [6, 142], 'line_class' => [145, 142], 'line_weight' => [177, 142], 'total_pieces' => [6, 244], 'total_line_weight' => [177, 244], ]; } }