loadMissing(['fromCountry', 'toCountry', 'items']); $data = [ 'shipment' => $shipment, 'shipper' => $this->formatAddress($shipment, 'sender'), 'receiver' => $this->formatAddress($shipment, 'receiver'), 'items' => $shipment->items ?? collect(), 'barcode' => $this->generateBarcode($shipment->awb_no), ]; return $this->generatePdf(view('pdfs.awb', $data)->render(), 'A4', 'portrait'); } public function invoice(Shipment $shipment): string { $shipment->loadMissing(['fromCountry', 'toCountry', 'items']); $data = [ 'shipment' => $shipment, 'shipper' => $this->formatAddress($shipment, 'sender'), 'receiver' => $this->formatAddress($shipment, 'receiver'), 'items' => $shipment->isParcel() ? ($shipment->items ?? collect()) : collect(), 'invoice_total_usd' => $shipment->invoice_total_usd, 'barcode' => $this->generateBarcode($shipment->awb_no), ]; return $this->generatePdf(view('pdfs.invoice', $data)->render(), 'A4', 'portrait'); } public function label(Shipment $shipment): string { $shipment->loadMissing(['fromCountry', 'toCountry']); $data = [ 'shipment' => $shipment, 'shipper' => $this->formatAddress($shipment, 'sender'), 'receiver' => $this->formatAddress($shipment, 'receiver'), 'barcode' => $this->generateBarcode($shipment->awb_no), ]; return $this->generatePdf(view('pdfs.label', $data)->render(), 'A4', 'portrait'); } private function generatePdf(string $html, string $paper, string $orientation): string { $options = new Options(); $options->set('isHtml5ParserEnabled', true); $options->set('isRemoteEnabled', true); $options->set('defaultFont', 'DejaVu Sans'); $dompdf = new Dompdf($options); $dompdf->loadHtml($html); $dompdf->setPaper($paper, $orientation); $dompdf->render(); return $dompdf->output(); } private function formatAddress(Shipment $shipment, string $prefix): Collection { return collect([ 'name' => $shipment->{"{$prefix}_name"}, 'company' => $shipment->{"{$prefix}_company"}, 'phone' => $shipment->{"{$prefix}_phone"}, 'email' => $shipment->{"{$prefix}_email"}, 'address' => $shipment->{"{$prefix}_address"}, 'city' => $shipment->{"{$prefix}_city"}, 'state' => $shipment->{"{$prefix}_state"}, 'zip' => $shipment->{"{$prefix}_zip"}, 'id_number' => $shipment->{"{$prefix}_id_number"}, ]); } private function generateBarcode(string $code): string { try { $generator = new BarcodeGeneratorHTML(); return $generator->getBarcode($code, $generator::TYPE_CODE_128, 2, 60); } catch (\Throwable $e) { return '
Barcode unavailable
'; } } }