barryvdh / laravel-dompdf

A DOMPDF Wrapper for Laravel
MIT License
6.66k stars 966 forks source link

Images are not loading in the generated PDF #1061

Closed chanakaDe closed 1 month ago

chanakaDe commented 1 month ago

I'm trying to load this image in a PDF. <img src="{{ asset('images/justrent-logo.png') }}" alt="justrent logo">

Controller code:

$invoice_name = "invoice_".$id;
$pdf = Pdf::loadView('invoice.invoice',['invoice'=>$patient_invoice,'patient'=>$patient,'items'=>$items]);
return $pdf->download($invoice_name);

But it seems images are not rendered in the PDF. How to fix this?PLease advice.

chanakaDe commented 1 month ago

Found a solution on my own after playing few mins. I have to say code gpt helped me a lot to debug this PDF generation process. This code is generated by code gpt. VSCode codegpt plugin.

<!DOCTYPE html>
<html>
<head>
    <title>Invoice</title>
</head>
<body>
    <h1>Invoice</h1>
    <!-- Base64 Encoded Image -->
    @php
        $imagePath = public_path('images/my-logo.png');
        $imageData = base64_encode(file_get_contents($imagePath));
        $imageSrc = 'data:image/png;base64,' . $imageData;
    @endphp
    <img src="{{ $imageSrc }}" alt="my logo">
</body>
</html>