loadMissing(['fromCountry', 'toCountry', 'items']); $data = [ 'shipment' => $shipment, 'shipper' => $this->formatAddress($shipment, 'sender'), 'receiver' => $this->formatAddress($shipment, 'receiver'), 'items' => $shipment->items ?? collect(), 'invoice_total_usd' => optional($shipment->items)->sum('total_usd') ?? 0, ]; $html = view('pdfs.awb', $data)->render(); return $this->generatePdf($html, '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->items ?? collect(), 'invoice_total_usd' => optional($shipment->items)->sum('total_usd') ?? 0, ]; $html = view('pdfs.invoice', $data)->render(); return $this->generatePdf($html, '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'), ]; $html = view('pdfs.label', $data)->render(); return $this->generatePdf($html, 'A4', 'portrait'); } private function generatePdf(string $html, array|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); if (is_array($paper)) { $dompdf->setPaper($paper, $orientation); } else { $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'}, 'zip' => $shipment->{$prefix . '_zip'}, 'id_number' => $shipment->{$prefix . '_id_number'}, ]); } }